Sample code for 30+ languages & platforms
Unicode C

Unzip a ZIP Entry Directly into a BinData Object Using ZipEntry.UnzipToBd

See more Zip Examples

This example demonstrates how to use the ZipEntry.UnzipToBd method to inflate a ZIP entry directly into a BinData object.

The entry contents are uncompressed entirely in memory without creating a file on disk.

This is useful when:

  • Processing ZIP entry data entirely in memory
  • Avoiding temporary filesystem files
  • Working with binary files such as images, PDFs, or certificates
  • Passing uncompressed ZIP data directly to other APIs

The example opens a ZIP archive, locates a PDF entry, inflates it into a BinData object, and then writes the uncompressed bytes to a new file.

Suppose the ZIP archive contains:

docs/report.pdf

The entry is uncompressed directly into memory before optionally being saved to:

qa_output/report.pdf

Chilkat Unicode C Downloads

Unicode C
#include <C_CkZipW.h>
#include <C_CkZipEntryW.h>
#include <C_CkBinDataW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkZipW zip;
    HCkZipEntryW entry;
    HCkBinDataW pdfData;

    success = FALSE;

    zip = CkZipW_Create();

    // Open an existing ZIP archive.
    success = CkZipW_OpenZip(zip,L"qa_data/zips/documents.zip");
    if (success == FALSE) {
        wprintf(L"%s\n",CkZipW_lastErrorText(zip));
        CkZipW_Dispose(zip);
        return;
    }

    // Locate the PDF entry within the ZIP archive.
    entry = CkZipEntryW_Create();

    success = CkZipW_EntryOf(zip,L"docs/report.pdf",entry);
    if (success == FALSE) {
        wprintf(L"ZIP entry not found.\n");
        CkZipW_CloseZip(zip);
        CkZipW_Dispose(zip);
        CkZipEntryW_Dispose(entry);
        return;
    }

    // ------------------------------------------------------------
    // Inflate the ZIP entry directly into a BinData object.
    // 
    // The uncompressed bytes are stored entirely in memory.
    // 
    pdfData = CkBinDataW_Create();

    success = CkZipEntryW_UnzipToBd(entry,pdfData);
    if (success == FALSE) {
        wprintf(L"%s\n",CkZipEntryW_lastErrorText(entry));
        CkZipW_Dispose(zip);
        CkZipEntryW_Dispose(entry);
        CkBinDataW_Dispose(pdfData);
        return;
    }

    wprintf(L"Uncompressed size = %d\n",CkBinDataW_getNumBytes(pdfData));
    wprintf(L"\n");

    // ------------------------------------------------------------
    // Optionally save the uncompressed bytes to a file.
    // 
    success = CkBinDataW_WriteFile(pdfData,L"qa_output/report.pdf");
    if (success == FALSE) {
        wprintf(L"%s\n",CkBinDataW_lastErrorText(pdfData));
        CkZipW_Dispose(zip);
        CkZipEntryW_Dispose(entry);
        CkBinDataW_Dispose(pdfData);
        return;
    }

    CkZipW_CloseZip(zip);

    wprintf(L"PDF extracted successfully.\n");


    CkZipW_Dispose(zip);
    CkZipEntryW_Dispose(entry);
    CkBinDataW_Dispose(pdfData);

    }