How to use zip4j to extract an zip file with password protection
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();
}
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