Reputation: 242
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.
Upvotes: 1
Views: 1179
Reputation: 161
Encryption has always been a secondary function of zip archive software, while it's excellent at archiving, encryption is not it's strong point. As an alternate approach, zip the file without encryption and then use some software specifically made for encryption for that part, for example have a look at https://github.com/xecrets/xecrets-file-cli . It's a stand-alone command line utility that will encrypt any file in tried, proven and secure manner. It's also quite easy to call and use programmatically as it can provide log output in easily interpreted JSON format, and it can accept the data to encrypt as a stream on stdin which may remove the need for a temporary file. There are other similar options. Full disclosure: I am the author this software.
Upvotes: 1
Reputation: 5644
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.
Upvotes: 4