Search code examples
javazip4j

Password protected zip file not the file inside the zip


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.This is not what i want i want to make the zip password protected not the file inside it.


Solution

  • Impossible, sorry.

    A file such as that would not conform to the .ZIP File Format Specification. It states that, while the actual files are encrypted, the metadata (including file names and sizes) is not encrypted. This has been a topic of debate, but this is the way it is. One of the uses of it is that you get your nice "Password was incorrect" rather than junk files.

    Either way - don't rely on .zip file encryption. It's bad. No really - it's very, very bad. It's vulnerable to an entire host of attacks.

    If you want a properly encrypted .zip file - do just that. Make a .zip file, and then use proper encryption to make an encrypted one. Java has quite a few encryption facilities for that.