【问题标题】:How to use zip4j to extract an zip file with password protection如何使用 zip4j 提取具有密码保护的 zip 文件
【发布时间】:2012-06-25 20:29:08
【问题描述】:

我正在尝试解压缩带有密码保护的 zip 文件。我知道有一个名为“zip4j”的 java 库可以帮助我。但是我无法打开 zip4j 网站来查看教程。

我已经用另一个镜像下载了 zip4j 库,但我不知道如何使用它。有没有人可以粘贴使用 zip4j 解压缩密码保护 zip 文件的示例代码?

zip4j website

非常感谢!

【问题讨论】:

    标签: java passwords unzip


    【解决方案1】:

    尝试以下操作并确保您使用的是最新的 Zip4j 库 (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();
    }
    

    【讨论】:

    • 如果您输入错误的密码,程序将创建一个空文件。
    【解决方案2】:

    我们在安卓手机的下载文件夹中有一个文件game.zip,我们使用下面给出的密码解压它:

    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
    }
    
    

    在构建 Gradle(应用级别)之前添加依赖项

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

    确保你有存储权限,这些愚蠢的错误会占用你宝贵的时间

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

    通过手动解压确保您的 zip 文件不是损坏的文件。

    如果你想做一些复杂的压缩工作,你应该从这里获得帮助:https://github.com/srikanth-lingala/zip4j

    【讨论】:

      【解决方案3】:

      使用zip4j 压缩/解压缩文件夹/文件的完整实现


      this dependency 添加到您的构建管理器。或者,将最新的 JAR 文件从 hereadd it 下载到您的项目构建路径。 class 波纹管可以压缩和提取任何文件或文件夹,无论是否有密码保护-

      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);
          }/**/
      }
      

      更详细的用法见here

      如果您使用的是 android,请确保您已在清单文件中添加存储权限。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-12-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-01-06
        • 2012-12-10
        • 1970-01-01
        相关资源
        最近更新 更多