Skip to content
Andrew Lambert edited this page Oct 28, 2023 · 18 revisions

PKZip.ZipWriter

Class Declaration

 Protected Class ZipWriter

Remarks

This class is used to create zip archives. It provides more granular control of the zipping process than the PKZip.WriteZip utility method does.

Create a new instance, and then use the AppendDirectory, AppendEntry, DeleteEntry, etc. methods to build a model of the archive. When ready, call the Commit method to generate the actual archive and write it to a file or memory stream.

Limitations

The zip file format limits archives to 4GB, both overall and for individual compressed files. File names (including the path), file comments, and file extra data fields are limited to 65535 bytes each.

The number of files in a single archive is technically limited to 65535, however this class does not enforce the limit. Most zip readers (including the ZipReader class) ignore this limit and can handle archives with any number of files.

The zip format does not forbid having two or more entries with identical names, however this class does not support creating such archives.

Example

  Dim zip As New PKZip.ZipWriter
  zip.ArchiveComment = "This is an archive comment."

  ' Add a file to the archive root
  Dim path As String = zip.AppendEntry(GetOpenFolderItem("")) 
  zip.SetEntryCompressionLevel(path, 9) ' use max compression for this entry
  zip.SetEntryComment(path, "This is an entry comment.")

  ' recursively add contents of directory to archive root
  Dim dir As FolderItem = SelectFolder()
  zip.AppendDirectory(dir)

  ' recursively add a directory and its contents to archive root
  zip.AppendDirectory(dir, dir.Parent)

  ' add a file from memory
  zip.AppendEntry("path/to/be/added.txt", "Hello, world! This is the content of the file.")

  ' delete a file
  zip.DeleteEntry("path/to/be/deleted.txt")

  ' delete a directory and its contents
  zip.DeleteEntry("path/to/be/deleted/")
  
  ' finish
  zip.Commit(SpecialFolder.Desktop.Child("test.zip"))

Methods

Properties

Constants

Protected Const OUTPUT_SEEKABLE = True

In cases where it's not possible to seek backwards in the output stream the zip spec supports storing certain metadata in an optional 'footer' that follows compressed data instead of in the regular 'header' that precedes it. This is not really useful to us, but if for some reason you need to generate archives with the optional entry footers you can set this constant to False

Entry-level points of interest denoted by "☜"



Clone this wiki locally