如何使用zip4j加密zip文件

8

我想要创建一个有密码保护的ZIP文件:

    // Set the compression level
    parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);

    // Set the encryption flag to true
    // If this is set to false, then the rest of encryption properties are ignored
    parameters.setEncryptFiles(true);

    // Set the encryption method to Standard Zip Encryption
    parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_STANDARD);

    // Set password
    parameters.setPassword(password);

但这只是对zip文件内部的文件进行加密,我仍然可以打开这个zip文件并查看其中的文件。

可能是重复问题:https://dev59.com/rXVC5IYBdhLWcg3w1E_w - benzonico
4
我不这么认为。在您的讨论串中列出了可以用于创建zip文件的库,但我选择了其中一个并需要帮助。 - hudi
2个回答

5

Zip4j支持加密文件列表...

主要特点:

  • 创建、添加、提取、更新、删除Zip文件中的文件
  • 读写密码保护的Zip文件
  • 支持AES 128/256加密
  • 支持标准Zip加密
  • 支持Zip64格式
  • 支持存储(无压缩)和Deflate压缩方法
  • 从分割的Zip文件(例如z01、z02...zip)创建或提取文件
  • 支持Unicode文件名
  • 进度监控

请查看这个示例代码AddFilesWithAESEncryption.java:

// Initiate ZipFile object with the path/name of the zip file.
ZipFile zipFile = new ZipFile("c:\\ZipTest\\AddFilesWithAESZipEncryption.zip");

// Build the list of files to be added in the array list
// Objects of type File have to be added to the ArrayList
ArrayList filesToAdd = new ArrayList();
filesToAdd.add(new File("c:\\ZipTest\\sample.txt"));
filesToAdd.add(new File("c:\\ZipTest\\myvideo.avi"));
filesToAdd.add(new File("c:\\ZipTest\\mysong.mp3"));

// Initiate Zip Parameters
ZipParameters parameters = new ZipParameters();
// set compression method to deflate compression
parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE); 
parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_ULTRA); 

// Set the encryption flag to true
parameters.setEncryptFiles(true);

// Set the encryption method to AES Zip Encryption
parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_AES);

// Set AES Key strength. Key strengths available for AES encryption are:
parameters.setAesKeyStrength(Zip4jConstants.AES_STRENGTH_256);

// Set password
parameters.setPassword("test123!");

// Now add files to the zip file
zipFile.addFiles(filesToAdd, parameters);

1
setPassword已被弃用。新版本是:ZipFile zipFile = new ZipFile("filename.zip", "password".toCharArray()); - Baris LaPaz

1
Zip4j不支持对文件列表进行加密,因为存在专利问题。
请参见:http://www.lingala.net/zip4j/forum/index.php?topic=104.0 更新: 如链接中所述。zip规范不包括对文件列表的加密。要隐藏文件名,您可以创建一个包含文件的zip文件,并将其封装为另一个zip文件。因此,如果您打开zip2.zip,您只会看到“zip1.zip”,而不是原始文件名。

PKWare提供了自己的工具SecureZIP。它支持文件名加密。 - oleg.cherednik

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接