简体   繁体   中英

How to encrypt zip file using zip4j

I want to create password protected 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

Zip4j supports the encryption of the file list...

Key features :

  • Create, Add, Extract, Update, Remove files from a Zip file
  • Read/Write password protected Zip files
  • Supports AES 128/256 Encryption
  • Supports Standard Zip Encryption
  • Supports Zip64 format
  • Supports Store (No Compression) and Deflate compression method
  • Create or extract files from Split Zip files (Ex: z01, z02,...zip)
  • Supports Unicode file names
  • Progress Monitor

Take a look at this example code 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.

See: 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. To hide the filenames you can create a zip-file including your files encapsulate it by zip it again. Hence if you open zip2.zip you will only see "zip1.zip" and not the original filenames.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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