Skip to content
Andrew Lambert edited this page Mar 11, 2023 · 19 revisions

PKZip.ZipReader

Class Declaration

 Protected Class ZipReader

Remarks

Note: For basic archive reading/writing see the ReadZip and WriteZip utility methods.

This class reads a Zip archive. To create a new instance pass a MemoryBlock, BinaryStream, or FolderItem containing the archive to the Constructor method. The archive is processed one entry at a time, from the first entry to the last.

Create a new instance, and access the CurrentName, etc. properties to read metadata of the current item in the archive. Call the MoveNext method to process the current item and then advance the selection to the next item in the archive. MoveNext will return True until an error occurs; note that reaching the end of the archive is technically an error, so check LastError for details.

What?

OK, so a little explanation is in order. A zip file is a sequence of files; each file is composed of a metadata block and a filedata block. The metadata block of file 1 is automatically read by the Constructor method so that the CurrentName, etc. properties can be populated. This positions the input BinaryStream at the beginning of the filedata block of file 1.

When MoveNext is called the filedata block of file 1 is read into the the output parameter and the metadata block of file 2 is read into the CurrentName, etc. properties. This positions the input BinaryStream at the beginning of the filedata block of file 2, all set for the next call to MoveNext.

Structure of a zip file

Example

This example scans through the archive until it finds a file with a specific name and then it extracts only that file:

  Dim zipfile As FolderItem = GetOpenFolderItem("") ' the zip archive to read
  Dim zip As New PKZip.ZipReader(zipfile) ' read the metadata of the first entry
  Do
    If zip.CurrentName = "path/to/the/file.txt" Then
      ' the current entry is the one we want
      Dim outputfile As FolderItem = SpecialFolder.Desktop.Child("file.txt")
      Dim output As BinaryStream = BinaryStream.Create(outputfile)
      ' extract the filedata of the current item
      Call zip.MoveNext(output)
      output.Close()
      Exit Do ' done
    End If
    ' the current entry is not the one we want. Skip the filedata and load the next entry's metadata
  Loop Until Not zip.MoveNext(Nil)
  zip.Close()

Methods

Properties

Shared Methods

Entry-level points of interest denoted by "☜"



Clone this wiki locally