简体   繁体   English

如何使用zip4j加密zip文件

[英]How to encrypt zip file using zip4j

I want to create password protected ZIP: 我想创建受密码保护的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);

but this just encrypt files inside of zip but I can open this zip and watch file inside it 但这只是加密zip中的文件,但我可以在其中打开此zip和监视文件

Zip4j supports the encryption of the file list... Zip4j支持文件列表的加密......

Key features : 主要特点

  • Create, Add, Extract, Update, Remove files from a Zip file 从Zip文件创建,添加,提取,更新,删除文件
  • Read/Write password protected Zip files 读/写密码保护的Zip文件
  • Supports AES 128/256 Encryption 支持AES 128/256加密
  • Supports Standard Zip Encryption 支持标准Zip加密
  • Supports Zip64 format 支持Zip64格式
  • Supports Store (No Compression) and Deflate compression method 支持存储(无压缩)和Deflate压缩方法
  • Create or extract files from Split Zip files (Ex: z01, z02,...zip) 从Split Zip文件创建或提取文件(例如:z01,z02,... zip)
  • Supports Unicode file names 支持Unicode文件名
  • Progress Monitor 进度监视器

Take a look at this example code AddFilesWithAESEncryption.java : 看一下这个示例代码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);

Zip4j does not support the encryption of the file list because of patent issues. 由于专利问题,Zip4j不支持文件列表的加密。

See: http://www.lingala.net/zip4j/forum/index.php?topic=104.0 请参阅: http//www.lingala.net/zip4j/forum/index.php?topic = 104.0

Update: 更新:

As stated in the link. 如链接中所述。 The zip-specification does not include the encryption of the filelist. zip规范不包括文件列表的加密。 To hide the filenames you can create a zip-file including your files encapsulate it by zip it again. 要隐藏文件名,您可以创建一个zip文件,包括您的文件再次通过zip封装它。 Hence if you open zip2.zip you will only see "zip1.zip" and not the original filenames. 因此,如果您打开zip2.zip,您将只看到“zip1.zip”而不是原始文件名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM