Sample code for 30+ languages & platforms
Zig

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 Zig Downloads

Zig
const std = @import("std");
const chilkat = @import("chilkat");

pub fn main(init: std.process.Init) !void {
    const alloc = init.arena.allocator();

    var success: bool = false;

    const zip = try chilkat.Zip.init();
    defer zip.deinit();

    // Open an existing ZIP archive.
    success = if (zip.openZip("qa_data/zips/documents.zip")) true else |_| false;
    if (!success) {
        std.debug.print("{s}\n", .{try zip.getLastErrorText(alloc)});
        return;
    }

    // Locate the PDF entry within the ZIP archive.
    const entry = try chilkat.ZipEntry.init();
    defer entry.deinit();

    success = zip.entryOf("docs/report.pdf", entry);
    if (!success) {
        std.debug.print("ZIP entry not found.\n", .{});
        zip.closeZip();
        return;
    }

    // ------------------------------------------------------------
    // Inflate the ZIP entry directly into a BinData object.
    //
    // The uncompressed bytes are stored entirely in memory.
    //
    const pdf_data = try chilkat.BinData.init();
    defer pdf_data.deinit();

    entry.unzipToBd(pdf_data) catch {
        std.debug.print("{s}\n", .{try entry.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("Uncompressed size = {d}\n", .{pdf_data.getNumBytes()});
    std.debug.print("\n", .{});

    // ------------------------------------------------------------
    // Optionally save the uncompressed bytes to a file.
    //
    pdf_data.writeFile("qa_output/report.pdf") catch {
        std.debug.print("{s}\n", .{try pdf_data.getLastErrorText(alloc)});
        return;
    };

    zip.closeZip();

    std.debug.print("PDF extracted successfully.\n", .{});
}