Zip Code Example: Real time addition of files to an archive

The Zip component supports adding files to an archive as they become available.

Date Entered: 02/21/2014    Last Updated: 02/21/2014

Typically when using the Zip component you must specify all the files to be included in the archive and then call Compress to create the archive. The component also supports building the archive as files become available. This allows you to specify one or more files, call Compress, and then specify additional files and so on until all the desired files have been added. Creating the archive on the fly like this offers better overall performance than waiting for all files are available before calling Compress.

Note: This is only applicable to Java and .NET editions at this time.

To enable this functionality set the CloseArchiveAfterCompress setting to False. This setting determines whether the archive is closed after calling Compress. When set to False the archive remains open and additional files can be added at any time. This allows files to be added to the archive incrementally as they become available. For instance:

zip.Config("CloseArchiveAfterCompress=false"); //Add one file zip.Files.Add(new ZIPFile("..\\..\\files\\test.txt")); zip.Compress(); //Add another file zip.Files.Clear(); zip.Files.Add(new ZIPFile("..\\..\\files\\test2.txt")); zip.Compress(); //Ready to finish creating the archive zip.Config("CloseArchiveAfterCompress=true"); zip.Files.Clear(); zip.Compress(); //Must be called to finish archive creation

As illustrated in the code above this setting must be set to True after all files are added to the archive and Compress must be called a final time to complete the archive creation.


We appreciate your feedback.  If you have any questions, comments, or suggestions about this entry please contact our support team at kb@nsoftware.com.