Archived Forum Post

Index of archived forum posts


How to add a zipped file into a password-protected zip object

Jan 14 '15 at 22:30

I tried to zip several files including an already zipped file into a zip object and set a password to it. For example, I'd like to create a new zip file with a text file wTxtToZip and a zipped file Azippedfile in it. And I need this new zip file to be password-protected. I’m using

objZip.AppendFiles(wTxtToZip, true);

But I found that with objZip.AppendZip(Azippedfile), the password is no longer set to the zip file. It works fine without it. I would love to know a solution to set a password to the zip file in this situation.

Thank anybody who would help very much in advance!

Accepted Answer

The zip file format is such that there is no single header that defines the settings for all of the entries within a zip. The settings for each entry (file or directory), such as compression algorithm, compression level, encryption, and even the password*, are completely contained within the entry itself. (for example, see this: )

The new files being written into a zip are those specified by AppendFiles (or AppendData). For these yet-to-be-created zip archive entries, the characteristics, such as encryption, compression level, etc. are determined by the properties of the CkZip object. When you append the already-existing entries of an already-existing Zip archive via AppendZip, the compression level and encryption settings for those entries won't change. Those entries are simply merged into the new zip that is created via WriteZipAndClose. Therefore, your code snippet above, the result would be a zip where the new entries are password protected, but the existing entries added via AppendZip are not.

The only solution would be to unzip the "Azippedfile" and then add the files.

*The password is not actually stored within the .zip. Instead, a checksum/hash of the password is saved in a header within the zip file format.