Search code examples
javapasswordsunzip

How to use zip4j to extract an zip file with password protection


I am trying to unzip a zipfile with password protection. I know there is a java library named "zip4j" that could help me. But I am failing to open the zip4j website to see the tutorial.

I had download zip4j library with another mirror but I don't know how to use it. Is there anyone that could paste example code for using zip4j unzip password protection zip file?

zip4j website

thanks so much!


Solution

  • Try the following and make sure you are using the most recent Zip4j library (1.3.1):

    String source = "folder/source.zip";
    String destination = "folder/source/";
    String password = "password";
    
    try {
        ZipFile zipFile = new ZipFile(source);
        if (zipFile.isEncrypted()) {
            zipFile.setPassword(password);
        }
        zipFile.extractAll(destination);
    } catch (ZipException e) {
        e.printStackTrace();
    }