Sample code for 30+ languages & platforms
Zig

Chunked Compression Using CompressBd2 with FirstChunk and LastChunk

See more Compression Examples

This example demonstrates how to compress data in multiple segments using the CompressBd2 method together with the FirstChunk and LastChunk properties. Instead of compressing all data in a single call, the input is divided into smaller chunks and processed sequentially, which is useful when handling streaming data or large inputs.

The example shows how to correctly mark the first, middle, and final chunks so that the compression stream is properly initialized and finalized. The compressed output is accumulated in a separate BinData object without modifying the input chunks. Finally, the example decompresses the combined result to verify that the original data is restored correctly.

This approach is ideal for scenarios where data is received incrementally, such as reading from a network stream, processing large files in parts, or handling real-time data feeds.

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();

    // This example assumes the Chilkat API has already been unlocked.
    // See Global Unlock Sample for sample code.

    const compress = try chilkat.Compression.init();
    defer compress.deinit();
    compress.setAlgorithm("zlib");

    // This will accumulate the compressed output.
    const bd_out = try chilkat.BinData.init();
    defer bd_out.deinit();

    // ------------------------------------------------------------------
    // Simulate input arriving in chunks.
    // ------------------------------------------------------------------

    const part1 = "The quick brown fox ";
    const part2 = "jumps over the lazy dog. ";
    const part3 = "This text is split into chunks.";

    // ------------------------------------------------------------------
    // Compress the first chunk
    // ------------------------------------------------------------------

    compress.setFirstChunk(true);
    compress.setLastChunk(false);

    const bd_in = try chilkat.BinData.init();
    defer bd_in.deinit();
    bd_in.appendString(part1, "utf-8") catch {};

    compress.compressBd2(bd_in, bd_out) catch {
        std.debug.print("{s}\n", .{try compress.getLastErrorText(alloc)});
        return;
    };

    // ------------------------------------------------------------------
    // Compress a middle chunk
    // ------------------------------------------------------------------

    compress.setFirstChunk(false);
    compress.setLastChunk(false);

    bd_in.clear() catch {};
    bd_in.appendString(part2, "utf-8") catch {};

    compress.compressBd2(bd_in, bd_out) catch {
        std.debug.print("{s}\n", .{try compress.getLastErrorText(alloc)});
        return;
    };

    // ------------------------------------------------------------------
    // Compress the final chunk
    // ------------------------------------------------------------------

    compress.setFirstChunk(false);
    compress.setLastChunk(true);

    bd_in.clear() catch {};
    bd_in.appendString(part3, "utf-8") catch {};

    compress.compressBd2(bd_in, bd_out) catch {
        std.debug.print("{s}\n", .{try compress.getLastErrorText(alloc)});
        return;
    };

    // Get the final compressed result as base64 for display
    const compressed_base64 = try bd_out.getEncoded(alloc, "base64");

    std.debug.print("Compressed data (base64):\n", .{});
    std.debug.print("{s}\n", .{compressed_base64});

    // ------------------------------------------------------------------
    // Decompress to verify correctness
    // ------------------------------------------------------------------

    const bd_decompressed = try chilkat.BinData.init();
    defer bd_decompressed.deinit();

    compress.setFirstChunk(true);
    compress.setLastChunk(true);

    compress.decompressBd2(bd_out, bd_decompressed) catch {
        std.debug.print("{s}\n", .{try compress.getLastErrorText(alloc)});
        return;
    };

    // Convert decompressed bytes back to a string
    const result_text = try bd_decompressed.getString(alloc, "utf-8");

    std.debug.print("Decompressed text:\n", .{});
    std.debug.print("{s}\n", .{result_text});
}