srikanth-lingala / zip4j

A Java library for zip files and streams
Apache License 2.0
2.01k stars 307 forks source link

Question: best way to test a password is ok for an encrypted zip file #479

Closed genxen closed 1 year ago

genxen commented 1 year ago

Hello,

Do you have any recommendation on how to check that a password is ok for an encrypted zip archive without extracting the full content? I have seen some discussion about reading each fileheader without triggering exception but this technique seems to be a bit heavy...

Thank you for your answer

srikanth-lingala commented 1 year ago

I have seen some discussion about reading each fileheader without triggering exception but this technique seems to be a bit heavy...

With the current zip specification, unfortunately this is the only way to test the password. Zip specification allows for each entry in a zip to have different passwords. Even though this is not a commonly used feature, but still this is technically possible and valid according to zip specification. So, to be sure that all the entries in the zip file are right, you have to test each fileheader. However, if you are sure that the zip file will use only one password for all the contents in the zip, you can just test the first fileheader.