Skip to content

PKZip.ZipReader.CurrentDataOffset

Andrew Lambert edited this page Nov 26, 2022 · 3 revisions

PKZip.ZipReader.CurrentDataOffset

Property Declaration

 Dim CurrentDataOffset As UInt64

Remarks

Returns the offset in the stream where the current item's compressed data begins. Combined with the CurrentSize and CurrentMethod properties you could decompress an entry directly.

Example

This example generates a Dictionary representing a zip archive file. The Dictionary uses the full path of each entry as the key and stores another dictionary of metadata under each key. It then uses these metadata entries to decompress the actual entry and stores the results as a MemoryBlock back into the metadata Dictionary.

  Dim zipfile As FolderItem = GetOpenFolderItem("") ' the zip archive to examine
  Dim zip As New PKZip.ZipReader(zipfile)
  Dim entries As New Dictionary
  
  ' loop over the archive and collect the necessary metadata about each entry.
  Do
    Dim entry As New Dictionary
    entry.Value("offset") = zip.CurrentDataOffset
    entry.Value("method") = zip.CurrentMethod
    entry.Value("size") = zip.CurrentSize
    entries.Value(zip.CurrentName) = entry
  Loop Until Not zip.MoveNext(Nil)
  zip.Close()
  
  ' re-open the archive file for raw reading
  Dim binstream As BinaryStream = BinaryStream.Open(zipfile)
  binstream.LittleEndian = True
  
  ' loop over the list of entries and store the decompressed file data in the entry too.
  For Each entry As Dictionary In entries.Values
    ' move to the start of the compressed data for this entry
    binstream.Position = entry.Value("offset")
    
    ' create a decompression stream
    Dim zipstream As Readable
    Select Case entry.Value("method")
    Case 0 ' no compression
      zipstream = binstream 
    Case 8 ' deflate compression
      Dim decompressor As zlib.ZStream = zlib.ZStream.Open(binstream, zlib.RAW_ENCODING)
      decompressor.BufferedReading = False
      zipstream = decompressor
    Else
      Raise New UnsupportedFormatException ' unsupported compression type
    End Select
    
    ' now we can read the compressed file data
    Dim data As MemoryBlock = zipstream.Read(entry.Value("size"))
    ' and store it in the Dictionary
    entry.Value("content") = data
  Next
  binstream.Close()

Entry-level points of interest denoted by "☜"



Clone this wiki locally