-
-
Notifications
You must be signed in to change notification settings - Fork 3
PKZip.ZipReader
PKZip.ZipReader
Protected Class ZipReader
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.
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
.
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()
Wiki home | Project page | Bugs | Become a sponsor
Text and code examples are Copyright ©2014-24 Andrew Lambert, offered under the CC BY-SA 3.0 License.