Zip Component, Email Component, Encryption Component ActiveX Control for Zip Compression .NET Components for ASP.NET
ActiveX and .NET Components for Zip Compression, Encryption, Email, XML, S/MIME, HTML Email, Character Encoding, Digital Certificates, FTP, and more ASP Email ActiveX Component


Index of Chilkat Blog Posts

October 15, 2007

Efficient Append to Existing Zip

The latest Chilkat Zip pre-release includes a new method: QuickAppend, that allows a new file to be appended to an existing .zip archive in the most efficient way possible — so that the contents of the existing .zip are not re-written. Examples in ASP, VB6, C#, VB.NET, C++, Perl, Java, Ruby, Python, etc., can be found on example-code.com.

What is inefficient about the following code:?

    zip.OpenZip("preExisting.zip");
    zip.AppendOneFileOrDir("hamlet.xml");
    zip.WriteZipAndClose();

The WriteZipAndClose method rewrites all the entries in the zip object. In this case, the entries are "pointers" to compressed data within preExisting.zip, except for the last entry which is a "pointer" to a file on the filesystem. Therefore, when WriteZipAndClose is called, it will rewrite the entire zip (to a temp file) and when complete, will move it over the existing .zip. The compressed data from already-existing entries is streamed directly from the existing .zip to the new .zip, and the newly added file is read/compressed in streaming mode into the new .zip.

To get a better understanding of the power of WriteZipAndClose, examine this example:

    // Open a WinZip compatible AES encrypted .zip
    zip.put_Encryption(4);
    zip.put_EncryptKeyLength(128);
    zip.SetPassword("oldPassword");
	
    zip.OpenZip("preExisting.zip");
	
    // Add a directory tree to the .zip
    zip.AppendFiles("c:/temp/abc123/*",true);
	
    // Change the password
    zip.SetPassword("newPassword");
	
    // Write the new Zip
    zip.WriteZipAndClose();

When WriteZipAndClose is called, entries that point to compressed entries in the existing .zip need to be decrypted with the old password and re-encrypted with the new password. This is exactly what happens. If the file (within the existing .zip) is small enough to fit in memory, the decrypt/re-encrypt happens entirely in memory. If the file is too large for memory, the file is streaming decrypted to a temp file, and streaming re-encrypted into the new .zip. The entries that are "pointers" to files in the filesystem are streaming compressed/encrypted into the new .zip.

The QuickAppend is different, it leaves all entries in the existing .zip untouched. It operates by appending new entries and updating the internal "central directory" in the zip file format. If you append new encrypted entries (using QuickAppend) to an existing .zip using a different password, you’ll have a .zip where some entries can be decrypted with one password, whereas the new entries require a different password.


Privacy Statement. Copyright 2000-2011 Chilkat Software, Inc. All rights reserved.
Send feedback to support@chilkatsoft.com

Components for Microsoft Windows XP, 2000, 2003 Server, Vista, Windows 7, and Windows 95/98/NT4.