How to encrypt zip file using zip4j

后端 未结 2 1545
伪装坚强ぢ
伪装坚强ぢ 2020-12-20 16:18

I want to create password protected ZIP:

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

    // Se         


        
相关标签:
2条回答
  • 2020-12-20 16:30

    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);
    
    0 讨论(0)
  • 2020-12-20 16:34

    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.

    0 讨论(0)
提交回复
热议问题