Skip to content
Andrew Lambert edited this page Mar 11, 2022 · 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 then the file 2 metadata block is read into the CurrentName, etc. properties. This positions the input BinaryStream at the beginning of the file 2 filedata block, 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)
  Dim outputfile As FolderItem = SpecialFolder.Desktop.Child("file.txt")
  Dim output As BinaryStream = BinaryStream.Create(outputfile)
  Do 
    If zip.CurrentName = "path/to/the/file.txt" Then
      ' we found the file. Extract it into the output stream
      Call zip.MoveNext(output)
      Exit Do ' done
    End If
  Loop Until Not zip.MoveNext(Nil) ' skip extraction and load the next entry
  output.Close
  zip.Close

Methods

Properties

Shared Methods

Entry-level points of interest denoted by "☜"



Clone this wiki locally