Zig
Zig
Compressing StringBuilder Data Using CompressSb (Single Call and Chunked)
See more Compression Examples
This example demonstrates how to compress text stored in aStringBuilder using the CompressSb method in two ways:
- Single-call compression, where the entire input is compressed in one operation.
- Chunked compression, where the input is split into multiple parts and processed sequentially using the
FirstChunkandLastChunkproperties.
The example shows how compressed output is appended to a BinData object, and how the result can be verified by decompressing back to the original text. This is useful for both simple use cases and scenarios where data is processed incrementally (such as streaming or large inputs).
Key Points
CompressSbconverts text to bytes using theCharsetproperty (UTF-8 recommended).- Compressed data is appended to a
BinDataobject. - When both
FirstChunkandLastChunkaretrue, compression is done in a single call. - For chunked processing:
- First chunk →
FirstChunk = true,LastChunk = false - Middle chunks → both
false - Final chunk →
LastChunk = true
- First chunk →
- Chunked compression is useful when processing data incrementally.
Chilkat Zig Downloads
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");
// ================================================================
// 1) Single-call compression (entire data in one call)
// ================================================================
const sb = try chilkat.StringBuilder.init();
defer sb.deinit();
sb.append("The quick brown fox jumps over the lazy dog. ") catch {};
sb.append("This is a simple example using CompressSb.") catch {};
const bd_compressed = try chilkat.BinData.init();
defer bd_compressed.deinit();
// When both FirstChunk and LastChunk are true (the defaults),
// the entire compression happens in a single call.
compress.setFirstChunk(true);
compress.setLastChunk(true);
compress.compressSb(sb, bd_compressed) catch {
std.debug.print("{s}\n", .{try compress.getLastErrorText(alloc)});
return;
};
const compressed_base64 = try bd_compressed.getEncoded(alloc, "base64");
std.debug.print("Single-call compressed (base64):\n", .{});
std.debug.print("{s}\n", .{compressed_base64});
// ================================================================
// 2) Chunked compression using FirstChunk / LastChunk
// ================================================================
const bd_chunked_out = try chilkat.BinData.init();
defer bd_chunked_out.deinit();
// First chunk
compress.setFirstChunk(true);
compress.setLastChunk(false);
const sb_part = try chilkat.StringBuilder.init();
defer sb_part.deinit();
sb_part.append("The quick brown fox ") catch {};
compress.compressSb(sb_part, bd_chunked_out) catch {
std.debug.print("{s}\n", .{try compress.getLastErrorText(alloc)});
return;
};
// Middle chunk
compress.setFirstChunk(false);
compress.setLastChunk(false);
sb_part.clear();
sb_part.append("jumps over the lazy dog. ") catch {};
compress.compressSb(sb_part, bd_chunked_out) catch {
std.debug.print("{s}\n", .{try compress.getLastErrorText(alloc)});
return;
};
// Final chunk
compress.setFirstChunk(false);
compress.setLastChunk(true);
sb_part.clear();
sb_part.append("This is a chunked CompressSb example.") catch {};
compress.compressSb(sb_part, bd_chunked_out) catch {
std.debug.print("{s}\n", .{try compress.getLastErrorText(alloc)});
return;
};
const chunked_base64 = try bd_chunked_out.getEncoded(alloc, "base64");
std.debug.print("Chunked compressed (base64):\n", .{});
std.debug.print("{s}\n", .{chunked_base64});
// ================================================================
// 3) Decompress to verify correctness
// ================================================================
const sb_decompressed = try chilkat.StringBuilder.init();
defer sb_decompressed.deinit();
// Decompress in a single call (entire data already assembled)
compress.setFirstChunk(true);
compress.setLastChunk(true);
compress.decompressSb(bd_chunked_out, sb_decompressed) catch {
std.debug.print("{s}\n", .{try compress.getLastErrorText(alloc)});
return;
};
std.debug.print("Decompressed text:\n", .{});
std.debug.print("{s}\n", .{try sb_decompressed.getAsString(alloc)});
}