Sample code for 30+ languages & platforms
PowerBuilder

Stream a Large ZIP Entry While Uncompressing Using ZipEntry.UnzipToStreamAsync

See more Zip Examples

This example demonstrates how to use ZipEntry.UnzipToStreamAsync to asynchronously unzip a ZIP entry directly to a stream while simultaneously reading the uncompressed data from the foreground thread.

The example is especially useful for large ZIP entries because the entire uncompressed file does not need to be held in memory at once. Instead, the uncompressed data is processed incrementally in chunks as it becomes available.

The example performs the following steps:

  • Opens an existing ZIP archive
  • Gets a ZIP entry
  • Starts an asynchronous unzip operation to a Stream
  • Reads the uncompressed data chunk-by-chunk from the stream
  • Writes each chunk directly to an output file

This approach is useful when:

  • Processing very large ZIP entries
  • Avoiding large memory allocations
  • Streaming uncompressed data to another destination
  • Performing incremental processing while decompression occurs

The example demonstrates a producer/consumer pattern where:

  • The background thread inflates the ZIP entry into the Stream
  • The foreground thread reads the uncompressed bytes from the Stream as they become available

The uncompressed bytes are written incrementally to:

qa_output/out.dat

Note: The standard Zip.Unzip* methods that extract directly to filesystem files already perform decompression in streaming mode internally within Chilkat. Chilkat does not inflate the entire file into memory before writing the output file.

Therefore, it is not necessary to use a more complex streaming approach such as this example merely to efficiently unzip large files to disk.

The purpose of this example is instead to demonstrate how an application can directly access and process the uncompressed data incrementally in chunks as the unzip operation is occurring.

This technique is ideal when the application needs to inspect, analyze, transform, transmit, or otherwise process the uncompressed bytes while they are being produced.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Zip
oleobject loo_Entry
oleobject loo_DataStream
oleobject loo_UnzipTask
oleobject loo_Fac
oleobject loo_Bd

li_Success = 0

li_Success = 0

loo_Zip = create oleobject
li_rc = loo_Zip.ConnectToNewObject("Chilkat.Zip")
if li_rc < 0 then
    destroy loo_Zip
    MessageBox("Error","Connecting to COM object failed")
    return
end if

// Open an existing ZIP archive.
li_Success = loo_Zip.OpenZip("qa_data/zips/big.zip")
if li_Success = 0 then
    Write-Debug loo_Zip.LastErrorText
    destroy loo_Zip
    return
end if

// Get the ZIP entry to be unzipped.
// 
// This example uses the first entry in the ZIP archive.
// The entry may contain a large file, so we will unzip it
// to a stream instead of loading the entire file into memory.
loo_Entry = create oleobject
li_rc = loo_Entry.ConnectToNewObject("Chilkat.ZipEntry")

li_Success = loo_Zip.EntryAt(0,loo_Entry)
if li_Success = 0 then
    Write-Debug loo_Zip.LastErrorText
    destroy loo_Zip
    destroy loo_Entry
    return
end if

// Create the stream that will receive the uncompressed data.
loo_DataStream = create oleobject
li_rc = loo_DataStream.ConnectToNewObject("Chilkat.Stream")

// Start an asynchronous unzip operation.
// 
// UnzipToStreamAsync writes the uncompressed entry data to dataStream
// from a background thread. The foreground thread can read from the
// stream as data becomes available.
loo_UnzipTask = loo_Entry.UnzipToStreamAsync(loo_DataStream)
if loo_Entry.LastMethodSuccess = 0 then
    Write-Debug loo_Entry.LastErrorText
    destroy loo_Zip
    destroy loo_Entry
    destroy loo_DataStream
    return
end if

// Start the background unzip thread.
loo_UnzipTask.Run()

// Open the output file that will receive the uncompressed data.
loo_Fac = create oleobject
li_rc = loo_Fac.ConnectToNewObject("Chilkat.FileAccess")

li_Success = loo_Fac.OpenForWrite("c:/temp/qa_output/out.dat")
if li_Success = 0 then
    Write-Debug loo_Fac.LastErrorText
    destroy loo_Zip
    destroy loo_Entry
    destroy loo_DataStream
    destroy loo_Fac
    return
end if

// Read the uncompressed data from the stream in chunks.
// 
// This avoids holding the entire uncompressed file in memory.
// Each chunk is written immediately to the output file.
loo_Bd = create oleobject
li_rc = loo_Bd.ConnectToNewObject("Chilkat.BinData")

do while (loo_DataStream.EndOfStream <> 1)

    // Read the next available chunk of uncompressed bytes.
    loo_DataStream.ReadBd(loo_Bd)

    // Write this chunk to the output file.
    loo_Fac.FileWriteBd(loo_Bd,0,0)

    // Clear the BinData object so it can be reused for the next chunk.
    loo_Bd.Clear()
loop

// Close the output file.
loo_Fac.FileClose()

// The background unzip task has completed when the stream reaches EOF.
// Delete the task object when no longer needed.
destroy loo_UnzipTask

// Close the ZIP archive.
loo_Zip.CloseZip()

Write-Debug "Success."


destroy loo_Zip
destroy loo_Entry
destroy loo_DataStream
destroy loo_Fac
destroy loo_Bd