How to use zip4j to extract an zip file with password protection
Asked Answered
A

3

17

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!

Anility answered 24/6, 2012 at 3:58 Comment(0)
O
36

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();
}
Ozmo answered 24/6, 2012 at 8:25 Comment(1)
If you enter the wrong password the program is going to create an empty file.Halberd
S
1

Here we have a file game.zip in Downloads folder in android phone and we are extracting it with the password given below:

String unzipFileAddress = Environment.DIRECTORY_DOWNLOADS "/Game.zip";
String filePassword = "2222"; // password of the file
String destinationAddress = Environment.DIRECTORY_DOWNLOADS + "/Game";

ZipFile zipFile = new ZipFile(unzipFileAddress, filePassword.toCharArray());
        
try {
     zipFile.extractAll(destinationAddress);
} catch (Exception e) {
  // if crashes print the message or Toast
}

Add in dependencies in build Gradle (app level) before doing it

dependencies{
 implementation 'net.lingala.zip4j:zip4j:2.6.4'
} // for lastest version check the link below

Make sure you have storage permission, these silly mistakes can take your valuable time

// Add in AndroidManifest.xml
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

Make sure your zip file is not a corrupt file by extracting it manually.

If you want to do some complex work with compression, you should take help from here: https://github.com/srikanth-lingala/zip4j

Snotty answered 29/11, 2020 at 6:0 Comment(0)
A
1

Full Implementation to Zip/Unzip a Folder/File with zip4j


Add this dependency to your build manager. Or, download the latest JAR file from here and add it to your project build path. The class bellow can compress and extract any file or folder with or without password protection-

import java.io.File;
import net.lingala.zip4j.model.ZipParameters;
import net.lingala.zip4j.util.Zip4jConstants;
import net.lingala.zip4j.core.ZipFile;  

public class Compressor {
    public static void zip (String targetPath, String destinationFilePath, String password) {
        try {
            ZipParameters parameters = new ZipParameters();
            parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
            parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);

            if (password.length() > 0) {
                parameters.setEncryptFiles(true);
                parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_AES);
                parameters.setAesKeyStrength(Zip4jConstants.AES_STRENGTH_256);
                parameters.setPassword(password);
            }
                
            ZipFile zipFile = new ZipFile(destinationFilePath);
                
            File targetFile = new File(targetPath);
            if (targetFile.isFile()) {
                zipFile.addFile(targetFile, parameters);
            } else if (targetFile.isDirectory()) {
                zipFile.addFolder(targetFile, parameters);
            } else {
                //neither file nor directory; can be symlink, shortcut, socket, etc.
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
        
    public static void unzip(String targetZipFilePath, String destinationFolderPath, String password) {
        try {
            ZipFile zipFile = new ZipFile(targetZipFilePath);
            if (zipFile.isEncrypted()) {
                zipFile.setPassword(password);
            }
            zipFile.extractAll(destinationFolderPath);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    /**/ /// for test
    public static void main(String[] args) {
        
        String targetPath = "target\\file\\or\\folder\\path";
        String zipFilePath = "zip\\file\\Path"; 
        String unzippedFolderPath = "destination\\folder\\path";
        String password = "your_password"; // keep it EMPTY<""> for applying no password protection
            
        Compressor.zip(targetPath, zipFilePath, password);
        Compressor.unzip(zipFilePath, unzippedFolderPath, password);
    }/**/
}

For more detailed usage, see here.

If you are working on android, then please make sure that you have added storage permission in the manifest file.

Agadir answered 6/11, 2021 at 7:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.