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

前端 未结 2 979
醉话见心
醉话见心 2020-12-31 09:45

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

相关标签:
2条回答
  • 2020-12-31 10:15

    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();
    }
    
    0 讨论(0)
  • 2020-12-31 10:22

    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

    0 讨论(0)
提交回复
热议问题