首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用zip4j解压带有密码保护的压缩文件

如何使用zip4j解压带有密码保护的压缩文件
EN

Stack Overflow用户
提问于 2012-06-24 11:58:06
回答 3查看 31.4K关注 0票数 15

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

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

zip4j website

非常感谢!

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-06-24 16:25:02

尝试以下操作,并确保您使用的是最新的Zip4j库(1.3.1):

代码语言:javascript
复制
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();
}
票数 35
EN

Stack Overflow用户

发布于 2020-11-29 14:00:39

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

代码语言:javascript
复制
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
}

在执行此操作之前,在build Gradle (应用程序级别)中添加依赖项

代码语言:javascript
复制
dependencies{
 implementation 'net.lingala.zip4j:zip4j:2.6.4'
} // for lastest version check the link below

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

代码语言:javascript
复制
// 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

票数 1
EN

Stack Overflow用户

发布于 2021-11-06 07:50:04

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

this dependency添加到构建管理器。或者,从hereadd it下载最新的JAR文件到您的项目构建路径。下面的class可以压缩和解压任何有或没有密码保护的文件或文件夹-

代码语言:javascript
复制
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,请确保您已在清单文件中添加了存储权限。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11174851

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档