8

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

hudi
  • 15,555
  • 47
  • 142
  • 246
  • Possible duplicate : http://stackoverflow.com/questions/166340/write-a-password-protected-zip-file-in-java – benzonico Feb 26 '13 at 09:23
  • 4
    I dont think so. In your thread there are list of libraries which you can use for creating zip but I choose one and I need help with it – hudi Feb 26 '13 at 09:30

2 Answers2

5

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);
kinjelom
  • 6,105
  • 3
  • 35
  • 61
  • 1
    setPassword is deprecated. New version is: ZipFile zipFile = new ZipFile("filename.zip", "password".toCharArray()); – Baris LaPaz Apr 08 '21 at 07:10
1

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.

sinclair
  • 2,812
  • 4
  • 24
  • 53