Sample code for 30+ languages & platforms
C++

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 C++ Downloads

C++
#include <CkZip.h>
#include <CkZipEntry.h>
#include <CkStream.h>
#include <CkTask.h>
#include <CkFileAccess.h>
#include <CkBinData.h>

void ChilkatSample(void)
    {
    bool success = false;

    success = false;

    CkZip zip;

    // Open an existing ZIP archive.
    success = zip.OpenZip("qa_data/zips/big.zip");
    if (success == false) {
        std::cout << zip.lastErrorText() << "\r\n";
        return;
    }

    // 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.
    CkZipEntry entry;

    success = zip.EntryAt(0,entry);
    if (success == false) {
        std::cout << zip.lastErrorText() << "\r\n";
        return;
    }

    // Create the stream that will receive the uncompressed data.
    CkStream dataStream;

    // 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.
    CkTask *unzipTask = entry.UnzipToStreamAsync(dataStream);
    if (entry.get_LastMethodSuccess() == false) {
        std::cout << entry.lastErrorText() << "\r\n";
        return;
    }

    // Start the background unzip thread.
    unzipTask->Run();

    // Open the output file that will receive the uncompressed data.
    CkFileAccess fac;

    success = fac.OpenForWrite("c:/temp/qa_output/out.dat");
    if (success == false) {
        std::cout << fac.lastErrorText() << "\r\n";
        return;
    }

    // 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.
    CkBinData bd;

    while ((dataStream.get_EndOfStream() != true)) {

        // Read the next available chunk of uncompressed bytes.
        dataStream.ReadBd(bd);

        // Write this chunk to the output file.
        fac.FileWriteBd(bd,0,0);

        // Clear the BinData object so it can be reused for the next chunk.
        bd.Clear();
    }

    // Close the output file.
    fac.FileClose();

    // The background unzip task has completed when the stream reaches EOF.
    // Delete the task object when no longer needed.
    delete unzipTask;

    // Close the ZIP archive.
    zip.CloseZip();

    std::cout << "Success." << "\r\n";
    }