javazip4j

Creating password protected zip file using zip4j - the password is on the files level but not on zip file level


I'm trying to create password protected zip fileby the following code:

    ZipParameters zipParameters = new ZipParameters();
    zipParameters.setEncryptFiles(true);
    zipParameters.setEncryptionMethod(EncryptionMethod.AES);

    List<File> filesToAdd = Arrays.asList(new File("aFile.txt"), new File("bFile.txt"));

    ZipFile zipFile = new ZipFile("compressed.zip", "password".toCharArray());
    zipFile.addFiles(filesToAdd, zipParameters);

The problem is that the password is on the level of the files in created zip but not on the zip itself. Meaning after the zip created I can enter the zip but I have to enter password to open "aFile.txt" or "bFile.txt". What I want is when I try to open "compressed.zip", the password window will show up before I can see the file names in the zip. Any help is appreciated, thanks in advance.


Solution

  • That's a ZIP format limitation. Metadata like filenames and folder structure aren't encrypted. If you need to encrypt those too, either ZIP the file again (only the outer ZIP need to be encrypted) or use other compression formats.