compress API reference Changelog View on GitHub

Compression codecs in pure Zig: Snappy, LZ4, DEFLATE, gzip, zlib, Zstandard, and Brotli.

One module, a namespace per codec, the same buffer discipline everywhere: the caller owns every buffer, the default APIs do not allocate, and every failure has a name. Streaming goes through std.Io. The encoders target the speed class — the zstd encoder is byte-identical to zstd -1 at level 1 and zstd -3 at level 3 — and the zstd decoder verifies what the C library does not.

This guide is the recipes. The API reference has the contract for every symbol. Each codec's README documents its API, error sets, and buffer sizes in the same terms.

Getting started

Install

zig fetch --save https://baskingspot.dev/compress/v0.4.0.tar.gz

Add the module to your build in build.zig:

const compress = b.dependency("compress", .{ .target = target, .optimize = optimize });
exe.root_module.addImport("compress", compress.module("compress"));

compress builds with Zig 0.17, and minimum_zig_version is 0.17.0.

Import it

Bind a codec from the module, once per file:

const compress = @import("compress");
const gzip = compress.gzip;

If the file uses only one codec, import it directly:

const gzip = @import("compress").gzip;

std has a std.compress of its own. In a file that uses both, bind this module as compressx:

const compressx = @import("compress");
const zstd = compressx.zstd; // this package
const std_zstd = std.compress.zstd; // std, decode only

Read the examples

Every example in this guide is a complete file in docs/examples/. The test step compiles and runs each one, so the code in this guide builds:

just test

Codecs

NamespaceFormatSupport
snappySnappy blocksencode, decode, streaming
lz4LZ4 blocksencode, decode
flateraw DEFLATE, RFC 1951encode, decode, streaming, preset dictionary
gzipgzip, RFC 1952encode, decode, streaming
zlibzlib, RFC 1950encode, decode, streaming, preset dictionary
zstdZstandard, RFC 8878encode, decode, streaming, dictionaries
brotliBrotli, RFC 7932encode (quality 3), decode, streaming

flate is the kernel: gzip and zlib are containers over it, each adding its framing, its checksum, and its own error names. snappy, lz4, zstd, and brotli are self-contained.

Choose std or compress

std.compress in Zig 0.17 ships flate with both directions, zstd with decode only, and the LZMA family. It does not ship gzip, zlib, Snappy, LZ4, or Brotli at all.

Formatstd.compress 0.17this package
DEFLATEboth directionsboth, one-shot and streaming, preset history
gzipnot presentboth, one-shot and streaming
zlibnot presentboth, one-shot and streaming, preset history
Snappynot presentboth, one-shot and streaming
LZ4not presentboth, raw blocks only
zstddecode onlyboth, byte-identical to zstd -1 and zstd -3, dictionaries
Brotlinot presentboth, one-shot and streaming

Use std when it has the direction you need and its shape fits. Use this package when you need the missing formats, a zstd encoder, or the same buffer discipline across every codec you touch. The decode paths here also check what std's zstd decoder does not: the frame checksum, the content size, and the dictionary ID, each with a named error.

One-shot encode and decode

The one-shot shape is identical in every codec:

const std = @import("std");
const compress = @import("compress");
const flate = compress.flate;
const gzip = compress.gzip;
const lz4 = compress.lz4;

// The one-shot shape is the same across codecs: encode returns the
// compressed length, decode returns the decoded length, and both take
// caller-owned buffers.
test "a one-shot round trip across codecs" {
    const data = "the same buffer shape in every codec";

    var member: [64]u8 = undefined;
    const member_len = try gzip.encode.compress(data, &member, .{});

    var plain: [64]u8 = undefined;
    const plain_len = try gzip.decode.decompress(member[0..member_len], &plain);
    try std.testing.expectEqualSlices(u8, data, plain[0..plain_len]);

    var raw: [64]u8 = undefined;
    const raw_len = try flate.encode.compress(data, &raw, .{});

    var back: [64]u8 = undefined;
    const back_len = try flate.decode.decompress(raw[0..raw_len], &back, null);
    try std.testing.expectEqualSlices(u8, data, back[0..back_len]);

    var block: [lz4.encode.maxCompressedLength(data.len)]u8 = undefined;
    const block_len = try lz4.encode.compressBlock(data, &block);
    var block_plain: [data.len]u8 = undefined;
    const block_plain_len = try lz4.decode.decompressBlock(
        block[0..block_len],
        &block_plain,
    );
    try std.testing.expectEqualSlices(u8, data, block_plain[0..block_plain_len]);
}

// Decode never writes past the returned length, so a bigger buffer
// keeps its tail untouched.
test "decode leaves the bytes past the decoded length alone" {
    const data = "sentinel rule";
    var member: [64]u8 = undefined;
    const member_len = try gzip.encode.compress(data, &member, .{});

    var plain: [64]u8 = undefined;
    @memset(&plain, 0xAA);
    const plain_len = try gzip.decode.decompress(member[0..member_len], &plain);
    try std.testing.expectEqualSlices(u8, data, plain[0..plain_len]);
    for (plain[plain_len..]) |b| try std.testing.expectEqual(@as(u8, 0xAA), b);
}

Encode takes source and target and returns the compressed length. Decode takes the compressed bytes and a target cap and returns the decoded length. target on the decode side is a cap, not a promise: the decode may fill the cap on its way to a refusal, but a result that does not fit fails error.BufferTooSmall and nothing past the cap is ever written. LZ4 is the exception: its raw block has no decoded-length field, so target.len is the exact expected length and a mismatch is error.DecompressionFailed. For the cap-style codecs, decode never writes past the returned length, so a larger target keeps its tail untouched.

Size the buffers

Never guess a size. Every encoder exports its exact worst case. Snappy's block form tells the decoder its exact decoded length up front, while an LZ4 caller supplies it through the output buffer:

const std = @import("std");
const compress = @import("compress");
const zstd = compress.zstd;
const zlib = compress.zlib;
const gzip = compress.gzip;
const snappy = compress.snappy;
const lz4 = compress.lz4;

// Sizing the target is the encoder's job. Each codec exports its own exact
// worst case, so no sizing is a guess.
test "the encoders size their own targets" {
    const data = "a target sized by the encoder, not by a guess";

    var member: [gzip.encode.maxCompressedLength(data.len)]u8 = undefined;
    const member_len = try gzip.encode.compress(data, &member, .{});
    try std.testing.expect(member_len <= member.len);

    var stream: [zlib.encode.maxCompressedLength(data.len)]u8 = undefined;
    const stream_len = try zlib.encode.compress(data, &stream, .{});
    try std.testing.expect(stream_len <= stream.len);

    var frame: [zstd.encode.maxCompressedLength(data.len)]u8 = undefined;
    const frame_len = try zstd.encode.compress(data, &frame, .{});
    try std.testing.expect(frame_len <= frame.len);

    // snappy's raw-block form: with the exact size, `compressBlock` cannot
    // fail, and its error set drops to `error{}` at the call site.
    var block: [snappy.encode.maxCompressedLength(data.len)]u8 = undefined;
    const block_len = try snappy.encode.compressBlock(data, &block);
    try std.testing.expect(block_len <= block.len);

    var lz4_block: [lz4.encode.maxCompressedLength(data.len)]u8 = undefined;
    const lz4_block_len = try lz4.encode.compressBlock(data, &lz4_block);
    try std.testing.expect(lz4_block_len <= lz4_block.len);
}

// Sizing the decode target differs by format. Snappy tells the caller the
// decoded length up front: the block header is a varint. An LZ4 caller supplies
// the exact length as the target's length. The other codecs take a target cap.
test "block decoded lengths come from the header or the caller" {
    const data = "how big is the result?";
    var block: [snappy.encode.maxCompressedLength(data.len)]u8 = undefined;
    const block_len = try snappy.encode.compressBlock(data, &block);

    try std.testing.expectEqual(
        data.len,
        try snappy.decode.decompressedBlockLength(block[0..block_len]),
    );

    var lz4_block: [lz4.encode.maxCompressedLength(data.len)]u8 = undefined;
    const lz4_block_len = try lz4.encode.compressBlock(data, &lz4_block);
    var lz4_plain: [data.len]u8 = undefined;
    const lz4_plain_len = try lz4.decode.decompressBlock(
        lz4_block[0..lz4_block_len],
        &lz4_plain,
    );
    try std.testing.expectEqualSlices(u8, data, lz4_plain[0..lz4_plain_len]);

    // The other formats keep no header field the decoder trusts for this:
    // gzip and zlib carry the decoded size only in their trailers, and a
    // zstd frame the caller produced may name it in the header, but the
    // decode still takes a target cap and returns the decoded length.
    var frame: [zstd.encode.maxCompressedLength(data.len)]u8 = undefined;
    const frame_len = try zstd.encode.compress(data, &frame, .{});
    var plain: [64]u8 = undefined;
    const plain_len = try zstd.decode.decompress(frame[0..frame_len], &plain, null);
    try std.testing.expectEqualSlices(u8, data, plain[0..plain_len]);
}

With the exact worst case, snappy.encode.compressBlock and lz4.encode.compressBlock cannot fail. Decode sizing is the caller's: Snappy carries the length in its block header, LZ4 takes it as the exact target length, and gzip and zlib carry the decoded size only in their trailers. For the cap-style codecs, decode never writes past the returned length, so a larger target keeps its tail untouched. Every codec's test suite pins its output boundary with sentinels.

Streaming

A Writer compresses into an Io.Writer, and a Reader decompresses from an Io.Reader. finish is required on the encode side: it writes the trailer. One-call pumps move a whole stream:

const std = @import("std");
const compress = @import("compress");
const gzip = compress.gzip;

// A Writer compresses into an Io.Writer, and a Reader decompresses from an
// Io.Reader. `finish` is required on the encode side: it writes the trailer.
fn streamingRoundTrip(gpa: std.mem.Allocator, input: []const u8) !void {
    var member: std.Io.Writer.Allocating = .init(gpa);
    defer member.deinit();

    var wbuf: gzip.Writer.Buffer = undefined;
    var w: gzip.Writer = .init(&member.writer, &wbuf, .{});
    try w.writer.writeAll(input);
    try w.finish();

    var in: std.Io.Reader = .fixed(member.written());
    var rbuf: gzip.Reader.Buffer = undefined;
    var r: gzip.Reader = .init(&in, &rbuf);
    const plain = try r.reader.allocRemaining(gpa, .unlimited);
    defer gpa.free(plain);
    try std.testing.expectEqualSlices(u8, input, plain);
}

test "a streaming round trip through Io" {
    try streamingRoundTrip(std.testing.allocator, "streamed through the Io interfaces");
}

// The one-call forms pump a whole input through. On the Reader side the
// gzip framing is self-delimiting, so one `streamAll` walks every member
// in a multi-member stream.
test "streamAll pumps a whole stream in one call" {
    const input = "one call, both directions";

    var member: std.Io.Writer.Allocating = .init(std.testing.allocator);
    defer member.deinit();
    var in: std.Io.Reader = .fixed(input);
    _ = try gzip.Writer.streamAll(&in, &member.writer, .{});

    var back: std.Io.Writer.Allocating = .init(std.testing.allocator);
    defer back.deinit();
    var source: std.Io.Reader = .fixed(member.written());
    _ = try gzip.Reader.streamAll(&source, &back.writer);
    try std.testing.expectEqualSlices(u8, input, back.written());
}

// The buffers are named types the caller owns. gzip's are small enough for
// a stack local; zstd's are not, and its `*Alloc` variants take an
// allocator for them.
test "the caller owns the streaming buffer" {
    var out: std.Io.Writer.Allocating = .init(std.testing.allocator);
    defer out.deinit();

    var wbuf: gzip.Writer.Buffer = undefined; // 426,636 bytes: a stack local is fine
    var in: std.Io.Reader = .fixed("caller-owned, stack-sized");
    var w: gzip.Writer = .init(&out.writer, &wbuf, .{});
    try w.writer.writeAll("caller-owned, stack-sized");
    try w.finish();
    _ = &in;
}

Who allocates

The codec holds no allocator, and every call that allocates says so in its name. This table is the whole contract:

CallAllocates
One-shot encode and decodeNothing. The caller passes both buffers.
Writer.init and Reader.initNothing. The caller passes the named Buffer type.
Writer.streamAllAlloc and the Reader *Alloc pumpsOne DefaultBuffer from the caller's allocator. It is freed before the call returns.
Io.Writer.Allocating and allocRemaining in examplesCaller-side scaffolding. These belong to the example, not to the codec.

A Buffer is storage that the codec writes into, not state that it owns. The type's size decides where it lives. gzip.Writer.Buffer is 426,636 bytes, so a stack local is fine on a main thread's stack. The zstd types are too large for that: zstd.Reader.DefaultBuffer is 8 MiB + 256 KiB and zstd.Writer.DefaultBuffer is 2.2 MiB, so keep those in static or heap storage. The *Alloc pumps take an allocator for exactly that buffer.

A failed allocation from a *Alloc pump is error.OutOfMemory.

The container codecs are self-delimiting at the member boundary: gzip.Reader.streamAll walks every member in a multi-member stream, and the exact boundary stays visible when a caller needs to stop there. Snappy's streaming layer uses Google's framed format: the sNaPpY identifier, per-chunk masked CRC-32C, and skippable chunks, so its streams interoperate with other framed-snappy tools. LZ4 has block functions only; its example CLI supplies an example-local record header.

Dictionaries

A dictionary is caller bytes, and both sides must supply the same bytes. zstd takes a parsed Dictionary: parse reads the formatted form that zstd --train writes, and initRaw takes any bytes at all, used as history with no entropy tables. The zlib preset is raw bytes in both directions, with FDICT and the DICTID check the RFC describes:

const std = @import("std");
const compress = @import("compress");
const zstd = compress.zstd;

// A dictionary is caller bytes. `Dictionary.parse` takes the formatted form
// that `zstd --train` writes; `initRaw` takes any bytes at all, used as
// history with no entropy tables. The value borrows the bytes: keep them
// alive as long as the dictionary is.
test "a raw dictionary round-trips both directions" {
    const dict_file = "a small dictionary of bytes the input will resemble";
    const data = "the input resembles the dictionary of bytes";

    const dict: zstd.Dictionary = .initRaw(dict_file);

    var frame: [zstd.encode.maxCompressedLength(data.len)]u8 = undefined;
    const frame_len = try zstd.encode.compress(data, &frame, .{ .level = .@"1", .dict = &dict });

    var plain: [64]u8 = undefined;
    const plain_len = try zstd.decode.decompress(frame[0..frame_len], &plain, &dict);
    try std.testing.expectEqualSlices(u8, data, plain[0..plain_len]);
}

// A raw dictionary carries no ID: `initRaw` content has no magic, so the
// frame the encoder writes cannot name it. A decode without the
// dictionary proceeds until a match reaches back into history that is not
// there, and fails OffsetTooFar — the honest refusal for the raw form.
// A formatted dictionary (the `zstd --train` output `parse` reads) carries
// a DICTID: its frames fail DictionaryRequired with none supplied and
// DictionaryMismatch for the wrong one.
test "a raw dictionary frame is honest about what it needs" {
    const right: zstd.Dictionary = .initRaw("the dictionary the frame was made with");
    const data = "the dictionary the frame was made with, and the data";

    var frame: [zstd.encode.maxCompressedLength(data.len)]u8 = undefined;
    const frame_len = try zstd.encode.compress(data, &frame, .{ .level = .@"1", .dict = &right });

    var plain: [128]u8 = undefined;
    try std.testing.expectError(
        error.OffsetTooFar,
        zstd.decode.decompress(frame[0..frame_len], &plain, null),
    );
}
const std = @import("std");
const compress = @import("compress");
const zlib = compress.zlib;

// The zlib preset dictionary is raw bytes (RFC 1950 §2.2 FDICT): the last
// 32 KiB are the decoder's history, and both directions must supply the
// same bytes. A dictionary longer than 32 KiB contributes only its tail.
test "a preset dictionary round-trips both directions" {
    const dict = "the same bytes to both sides of the stream";
    const data = "the same bytes appear in the data, and the matches reach back into the dictionary";

    var stream: [128]u8 = undefined;
    const stream_len = try zlib.encode.compress(data, &stream, .{ .level = .@"1", .dict = dict });

    var plain: [128]u8 = undefined;
    const plain_len = try zlib.decode.decompress(stream[0..stream_len], &plain, dict);
    try std.testing.expectEqualSlices(u8, data, plain[0..plain_len]);
}

// FDICT is part of the stream, so the decoder refuses to guess: a stream
// that says a dictionary is present fails DictionaryRequired when none is
// supplied, and the wrong dictionary fails DictionaryMismatch.
test "the wrong dictionary fails by name" {
    const dict = "the dictionary the stream was made with";
    const data = "some data that matches the dictionary";

    var stream: [128]u8 = undefined;
    const stream_len = try zlib.encode.compress(data, &stream, .{ .level = .@"1", .dict = dict });

    var plain: [128]u8 = undefined;
    try std.testing.expectError(
        error.DictionaryRequired,
        zlib.decode.decompress(stream[0..stream_len], &plain, null),
    );
    try std.testing.expectError(
        error.DictionaryMismatch,
        zlib.decode.decompress(stream[0..stream_len], &plain, "not the dictionary"),
    );
}

The value borrows the file bytes: keep them alive as long as the dictionary is. A raw dictionary carries no ID — a frame made with one decodes without it only until a match reaches back into history that is not there, and fails error.OffsetTooFar. A formatted dictionary carries a DICTID, and its frames refuse by name: DictionaryRequired when none is supplied, DictionaryMismatch for the wrong one. The zlib preset fails the same way. gzip has no dictionary concept (RFC 1952 defines none), and Brotli's static dictionary already ships inside the decoder — RFC 7932 defines no way to supply your own.

There is no trainer in this package, on purpose. Training is a tooling decision the zstd CLI already makes well; this package is the consumer side.

Choose a codec

Pick the format for the job, not the fastest name:

You needUseThe trade
The smallest payloadszstdThe heaviest format here; also the best ratio
Interop with everythinggzipDEFLATE's ratio and speed, universal acceptance
A zlib-shaped wire formatzlibgzip without the file framing, plus a preset dictionary
In-memory blocks, latency-boundsnappyNo ratio to speak of; the framing here is this package's own
Existing raw LZ4 block recordslz4The decoded length stays out of band; no frame or dictionary
Web text, either directionbrotliEncoder at quality 3; the static dictionary ships inside

The zstd encoder is byte-identical to zstd -1 at level 1 and zstd -3 at level 3, which pins it to the fast class; its Level enum has @"1" and @"3", and another number is not representable. The DEFLATE encoder's Level has @"1" (fixed-Huffman and stored blocks) and @"6" (dynamic-Huffman, byte-identical to klauspost/compress's level 6); gzip and zlib take the same type.

Errors and corrupt input

Every failure has a name, the sets are closed per codec, and the names say what was wrong with the bytes rather than which layer noticed:

const std = @import("std");
const compress = @import("compress");
const flate = compress.flate;
const gzip = compress.gzip;
const zstd = compress.zstd;

// Every failure has a name. The error sets are closed per codec, so a
// switch can be exhaustive, and the names say what was wrong with the
// bytes rather than which layer noticed.
test "corrupt input fails with a named error" {
    // A gzip header with the wrong magic: ID1, ID2, or CM wrong (§2.3.1).
    const not_gzip = "not a gzip member at all, just text";
    var plain: [64]u8 = undefined;
    try std.testing.expectError(error.BadHeader, gzip.decode.decompress(not_gzip, &plain));

    // A raw-deflate stream cut before its final block: the input ended
    // early, and the error says so.
    const data = "a stream that will be cut short";
    var raw: [64]u8 = undefined;
    const raw_len = try flate.encode.compress(data, &raw, .{});
    try std.testing.expectError(
        error.Truncated,
        flate.decode.decompress(raw[0 .. raw_len - 2], &plain, null),
    );

    // A zstd frame without zstd's magic: the first four bytes are the
    // frame's whole identity (§3.1.1.1).
    try std.testing.expectError(error.BadMagic, zstd.decode.decompress("no magic here", &plain, null));
}

// Decode treats the target as a cap, not a promise: a result that does
// not fit returns error.BufferTooSmall. The decode may fill the cap on
// its way to the refusal — the guarantee is that nothing past the cap
// is ever written.
test "a target too small fails before writing past it" {
    const data = "a decoded stream that will not fit";
    var member: [64]u8 = undefined;
    const member_len = try gzip.encode.compress(data, &member, .{});

    var tiny: [4]u8 = @splat(0xAA);
    try std.testing.expectError(
        error.BufferTooSmall,
        gzip.decode.decompress(member[0..member_len], &tiny),
    );
}

// The returned length is the other half of the rule: decode never writes
// past the length it returns, so a larger target keeps its tail untouched.
// Every codec's test suite pins this with sentinels.

Hostile input gets error returns, never assertions: the decoders treat every input as untrusted, and the fuzz suites pin the error names per mutation class. Truncated input fails by name too — error.Truncated for a DEFLATE stream cut before its final block, and each container adds its own names for its framing (error.BadHeader for gzip's magic, error.BadMagic for zstd's).

Design

Performance

The chart is one-shot throughput on 64 KiB inputs, in GB/s: the median across eight corpus shapes and seven EC2 targets. Encode and decode use the default classes: flate, gzip, zlib, and zstd at level 1, brotli at quality 3.

Bar chart of one-shot throughput on 64 KiB inputs, median across eight corpus shapes and seven EC2 targets: snappy encodes at 18 GB/s and decodes at 9, zstd at 5 and 12, brotli at 0.1 and 10.

The input's shape matters as much as the codec: each codec slows by 10x or more between the run-heavy and the prose shapes. The median encode rate spans 0.1 GB/s for brotli's q3 to 18 for snappy; decode stays between 1.7 for gzip and 12 for zstd.

The run is five interleaved rounds with an A/A control on each target, on the seven EC2 targets in bench/README.md. Local just bench numbers are not claims. The chart is generated by just plot from the recorded run.

Limits