Skip to content

Compression API

This page documents all compression-related functions in compressionz.

Compress data using the specified codec with default options.

pub fn compress(
codec: Codec,
input: []const u8,
allocator: std.mem.Allocator,
) Error![]u8
ParameterTypeDescription
codecCodecCompression algorithm to use
input[]const u8Data to compress
allocatorstd.mem.AllocatorAllocator for output buffer
  • []u8 — Compressed data. Caller owns and must free with allocator.
  • Error — On failure (see Error Handling)
const cz = @import("compressionz");
const compressed = try cz.compress(.zstd, data, allocator);
defer allocator.free(compressed);

Compress data with custom options.

pub fn compressWithOptions(
codec: Codec,
input: []const u8,
allocator: std.mem.Allocator,
options: CompressOptions,
) Error![]u8
ParameterTypeDescription
codecCodecCompression algorithm
input[]const u8Data to compress
allocatorstd.mem.AllocatorAllocator for output
optionsCompressOptionsCompression options
pub const CompressOptions = struct {
/// Compression level
level: Level = .default,
/// Include content size in header (LZ4 frame)
include_size: bool = true,
/// Include checksum (LZ4 frame)
include_checksum: bool = true,
/// Dictionary for better small-data compression
dictionary: ?[]const u8 = null,
};
// Maximum compression
const compressed = try cz.compressWithOptions(.zstd, data, allocator, .{
.level = .best,
});
// Fast compression
const fast = try cz.compressWithOptions(.zstd, data, allocator, .{
.level = .fast,
});
// With dictionary
const dict_compressed = try cz.compressWithOptions(.zstd, data, allocator, .{
.dictionary = my_dictionary,
});

Compress into a pre-allocated buffer (zero-copy).

pub fn compressInto(
codec: Codec,
input: []const u8,
output: []u8,
options: CompressOptions,
) Error![]u8
ParameterTypeDescription
codecCodecCompression algorithm
input[]const u8Data to compress
output[]u8Pre-allocated output buffer
optionsCompressOptionsCompression options
  • []u8 — Slice of output containing compressed data
  • error.OutputTooSmall — If buffer is too small

Only available for: .lz4, .lz4_raw, .snappy

var buffer: [65536]u8 = undefined;
const compressed = try cz.compressInto(.lz4, data, &buffer, .{});
// compressed is a slice of buffer, no allocation needed

Calculate maximum possible compressed size for buffer allocation.

pub fn maxCompressedSize(codec: Codec, input_size: usize) usize
ParameterTypeDescription
codecCodecCompression algorithm
input_sizeusizeSize of uncompressed data

Maximum possible size of compressed output. Actual compressed size will usually be smaller.

const max_size = cz.maxCompressedSize(.lz4, data.len);
const buffer = try allocator.alloc(u8, max_size);
defer allocator.free(buffer);
const compressed = try cz.compressInto(.lz4, data, buffer, .{});

pub const Level = enum {
fastest, // Maximum speed, lower ratio
fast, // Good speed, good ratio
default, // Balanced (recommended)
better, // Better ratio, slower
best, // Maximum ratio, slowest
};
LevelCompressDecompressRatio
fastest12+ GB/s11+ GB/s99.8%
fast12 GB/s11+ GB/s99.9%
default12 GB/s11+ GB/s99.9%
better5 GB/s11+ GB/s99.9%
best1.3 GB/s12 GB/s99.9%
  • Use default for most cases
  • Use fast for real-time applications
  • Use best only for archival or static content

For fine-grained control, access codec implementations directly:

const cz = @import("compressionz");
// LZ4 frame with all options
const compressed = try cz.lz4.frame.compress(data, allocator, .{
.level = .fast,
.content_checksum = true,
.block_checksum = false,
.content_size = data.len,
.block_size = .max64KB,
});
// LZ4 raw block (maximum speed)
const block = try cz.lz4.block.compress(data, allocator);
const compressed = try cz.snappy.compress(data, allocator);
// With level
const compressed = try cz.zstd.compress(data, allocator, .fast);
// With dictionary
const dict_compressed = try cz.zstd.compressWithDict(data, allocator, .default, dictionary);
const compressed = try cz.gzip.compress(data, allocator, .default);
const compressed = try cz.brotli.compress(data, allocator, .best);

pub const Codec = enum {
lz4, // LZ4 frame format
lz4_raw, // LZ4 block format (no framing)
snappy, // Snappy block format
zstd, // Zstandard
gzip, // Gzip (deflate + gzip headers)
zlib, // Zlib (deflate + zlib headers)
deflate, // Raw deflate
brotli, // Brotli
none, // No compression (passthrough)
};
const codec: cz.Codec = .zstd;
codec.name(); // "Zstandard"
codec.extension(); // ".zst"
codec.supportsStreaming(); // true
codec.supportsDictionary(); // true
codec.hasBuiltinChecksum(); // true
codec.isFramed(); // true
codec.requiresExpectedSize(); // false

See Codec Detection for auto-detection.