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.
DecompressionContext
A reusable decompression context for decompressing multiple buffers. Re-exported as zstd.DecompressionContext.
Definition
pub const DecompressionContext = struct {
allocator: std.mem.Allocator,
stream: StreamingDecompressor,
maxWindowSize: usize, // default 1<<27
// ...
};Methods
init
Create a new decompressor (no options struct):
pub fn init(allocator: std.mem.Allocator) DecompressionContextvar dctx = zstd.DecompressionContext.init(allocator);
defer dctx.deinit();deinit
Release streaming buffers:
pub fn deinit(self: *DecompressionContext) voiddecompress
Decompress into preallocated buffer:
pub fn decompress(self: *DecompressionContext, dst: []u8, src: []const u8) !usizevar out: [4096]u8 = undefined;
const n = try dctx.decompress(&out, compressed);decompressAlloc
Decompress with allocator:
pub fn decompressAlloc(self: *DecompressionContext, src: []const u8) anyerror![]u8const decompressed = try dctx.decompressAlloc(compressed);
defer allocator.free(decompressed);setMaxWindowSize
Set window size limit:
pub fn setMaxWindowSize(self: *DecompressionContext, size: usize) voiddctx.setMaxWindowSize(1 << 27);reset
Reset internal stream state:
pub fn reset(self: *DecompressionContext) voidExample
var dctx = zstd.DecompressionContext.init(allocator);
defer dctx.deinit();
dctx.setMaxWindowSize(1 << 26);
// Decompress multiple buffers
const d1 = try dctx.decompressAlloc(c1);
defer allocator.free(d1);
var buf: [4096]u8 = undefined;
const n = try dctx.decompress(&buf, c2);
dctx.reset();Decoder
zstd.Decoder is a high-level client-side decompression abstraction that accepts DecompressionOptions (safety window limit, checksum policies, dictionary binding) and provides clean buffer and allocation decompression:
pub const Decoder = struct {
pub fn init(allocator: std.mem.Allocator, options: DecompressionOptions) Decoder;
pub fn deinit(self: *Decoder) void;
pub fn reset(self: *Decoder) void;
pub fn setMaxWindowSize(self: *Decoder, size: usize) void;
pub fn setDictionary(self: *Decoder, dict: ?*const Dictionary) void;
pub fn decompress(self: *Decoder, dst: []u8, src: []const u8) !usize;
pub fn decompressAlloc(self: *Decoder, src: []const u8) anyerror![]u8;
};Usage
var decoder = zstd.Decoder.init(allocator, .{
.maxWindowSize = 1 << 27,
.forceIgnoreChecksum = false,
});
defer decoder.deinit();
const decompressed = try decoder.decompressAlloc(compressed_bytes);
defer allocator.free(decompressed);Decompressor
zstd.Decompressor is a direct alias for zstd.DecompressionContext matching client naming conventions.