Spec conformance: zstd.zig implements the Zstandard 1.6.0 specification natively in Zig - every algorithm, frame element, and default table in this document follows that version.
CompressionContext
A reusable compression context. Create once, compress multiple buffers with the same settings. Re-exported as zstd.CompressionContext.
Definition
pub const CompressionContext = struct {
allocator: std.mem.Allocator,
options: CompressionOptions,
stream: StreamingCompressor,
// ...
};Methods
init
Create with default level (3):
pub fn init(allocator: std.mem.Allocator) CompressionContextvar cctx = zstd.CompressionContext.init(allocator);
defer cctx.deinit();initWithLevel
Create with numeric i32 level:
pub fn initWithLevel(allocator: std.mem.Allocator, level: i32) CompressionContextvar cctx = zstd.CompressionContext.initWithLevel(allocator, 9);
defer cctx.deinit();deinit
Release streaming buffer:
pub fn deinit(self: *CompressionContext) voidsetLevel
Change compression level:
pub fn setLevel(self: *CompressionContext, level: i32) voidcctx.setLevel(5);setChecksum
Enable/disable checksum:
pub fn setChecksum(self: *CompressionContext, flag: bool) voidsetWindowLog
Set window log override:
pub fn setWindowLog(self: *CompressionContext, log: u8) voidsetPledgedSrcSize
Set content size for frame header:
pub fn setPledgedSrcSize(self: *CompressionContext, size: ?u64) voidcctx.setPledgedSrcSize(@as(?u64, data.len));
cctx.setPledgedSrcSize(null); // unknowncompress
Compress into pre-allocated buffer:
pub fn compress(self: *CompressionContext, dst: []u8, src: []const u8) !usizevar buf: [4096]u8 = undefined;
const written = try cctx.compress(&buf, data);compressAlloc
Compress with allocator (convenience):
pub fn compressAlloc(self: *CompressionContext, src: []const u8) anyerror![]u8const compressed = try cctx.compressAlloc(data);
defer allocator.free(compressed);reset
Reset streaming state for reuse:
pub fn reset(self: *CompressionContext) voidcctx.reset();Example
var cctx = zstd.CompressionContext.init(allocator);
defer cctx.deinit();
// First compression (default 3)
const c1 = try cctx.compressAlloc(data1);
defer allocator.free(c1);
// Change level and compress again
cctx.setLevel(9);
cctx.setChecksum(true);
const c2 = try cctx.compressAlloc(data2);
defer allocator.free(c2);
// Into fixed buffer
var buf: [8192]u8 = undefined;
const n = try cctx.compress(&buf, data3);Encoder
zstd.Encoder is a high-level client-side compression abstraction that wraps CompressionContext and supports explicit concurrency and parameter control:
pub const Encoder = struct {
pub fn init(allocator: std.mem.Allocator, options: CompressionOptions) !Encoder;
pub fn deinit(self: *Encoder) void;
pub fn reset(self: *Encoder) void;
pub fn compress(self: *Encoder, src: []const u8) ![]u8;
pub fn compressInto(self: *Encoder, dst: []u8, src: []const u8) !usize;
pub fn setLevel(self: *Encoder, level: i32) void;
pub fn setChecksum(self: *Encoder, flag: bool) void;
pub fn setWorkers(self: *Encoder, count: usize) !void;
};Usage
var encoder = try zstd.Encoder.init(allocator, .{ .level = 5, .checksum = true });
defer encoder.deinit();
const compressed = try encoder.compress("Hello World");
defer allocator.free(compressed);
// Enable multithreaded compression explicitly
try encoder.setWorkers(4);
const mt_compressed = try encoder.compress(large_data);
defer allocator.free(mt_compressed);Compressor
zstd.Compressor is a direct alias for zstd.CompressionContext matching client naming conventions.