Skip to content

Options Reference

This page documents all configuration options available in compressionz.

Options for compression operations.

pub const CompressOptions = struct {
/// Compression level
level: Level = .default,
/// Include content size in header (LZ4 frame)
include_size: bool = true,
/// Include content checksum (LZ4 frame)
include_checksum: bool = true,
/// Dictionary for improved small-data compression
dictionary: ?[]const u8 = null,
};

Controls the speed/ratio trade-off.

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
};

Usage:

const fast = try cz.compressWithOptions(.zstd, data, allocator, .{
.level = .fast,
});
const best = try cz.compressWithOptions(.zstd, data, allocator, .{
.level = .best,
});

Performance by Level (Zstd):

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%

LZ4 frame only. Include original content size in frame header.

const with_size = try cz.compressWithOptions(.lz4, data, allocator, .{
.include_size = true, // Default
});
const without_size = try cz.compressWithOptions(.lz4, data, allocator, .{
.include_size = false, // Smaller header
});

Benefits of including size:

  • Decompressor can allocate exact buffer size
  • Enables size validation on decompression
  • Small overhead (8 bytes in header)

LZ4 frame only. Include content checksum in frame footer.

const with_checksum = try cz.compressWithOptions(.lz4, data, allocator, .{
.include_checksum = true, // Default
});
const without_checksum = try cz.compressWithOptions(.lz4, data, allocator, .{
.include_checksum = false, // Faster, no verification
});

Trade-offs:

With ChecksumWithout Checksum
Data integrity verificationNo verification
~4 bytes overheadSlightly smaller
Recommended for storage/networkOK for trusted pipelines

Pre-trained dictionary for improved small-data compression.

const dictionary = @embedFile("my_dictionary.bin");
const compressed = try cz.compressWithOptions(.zstd, data, allocator, .{
.dictionary = dictionary,
});

Supported codecs: .zstd, .zlib, .deflate

See Dictionary Compression for details.


Options for decompression operations.

pub const DecompressOptions = struct {
/// Expected output size (required for lz4_raw)
expected_size: ?usize = null,
/// Maximum output size (decompression bomb protection)
max_output_size: ?usize = null,
/// Dictionary (must match compression)
dictionary: ?[]const u8 = null,
};

Required for raw block formats that don’t encode size.

// Required for lz4_raw
const decompressed = try cz.decompressWithOptions(.lz4_raw, data, allocator, .{
.expected_size = 1024, // Must match original size
});

When required:

CodecRequired?
lz4_raw✅ Yes
All others❌ No

Check programmatically:

if (codec.requiresExpectedSize()) {
return cz.decompressWithOptions(codec, data, allocator, .{
.expected_size = known_size,
});
}

Limits decompressed output size. Protection against decompression bombs.

const safe = try cz.decompressWithOptions(.gzip, untrusted_data, allocator, .{
.max_output_size = 100 * 1024 * 1024, // 100 MB limit
});

Error on exceed: Returns error.OutputTooLarge

Recommended limits:

ContextLimit
User uploads10-100 MB
API responses1-10 MB
Config files1 MB
Trusted internalNone or large

Dictionary for decompression. Must match the dictionary used for compression.

const dictionary = @embedFile("my_dictionary.bin");
// Must use same dictionary as compression
const decompressed = try cz.decompressWithOptions(.zstd, data, allocator, .{
.dictionary = dictionary,
});

Warning: Using the wrong dictionary produces garbage output or errors.


When using cz.lz4.frame directly:

const cz = @import("compressionz");
const compressed = try cz.lz4.frame.compress(data, allocator, .{
.level = .fast,
.content_checksum = true,
.block_checksum = false,
.content_size = data.len, // null to omit
.block_size = .max64KB, // .max64KB, .max256KB, .max1MB, .max4MB
.independent_blocks = false,
});
OptionTypeDefaultDescription
levelLevel.defaultCompression level
content_checksumbooltrueXXH32 checksum of content
block_checksumboolfalseXXH32 checksum per block
content_size?usizenullOriginal size in header
block_sizeBlockSize.max64KBMaximum block size
independent_blocksboolfalseBlocks don’t reference previous

When using cz.zstd directly:

// Simple
const compressed = try cz.zstd.compress(data, allocator, .fast);
// With dictionary
const dict_compressed = try cz.zstd.compressWithDict(
data,
allocator,
.default,
dictionary,
);

Brotli level affects both speed and ratio significantly:

LevelCompressRatioUse Case
fast1.3 GB/sGoodDynamic content
default1.3 GB/sBetterGeneral use
best86 MB/sBestStatic assets

Summary of all defaults:

OptionDefaultReason
level.defaultBalance of speed/ratio
include_sizetrueEnables optimized decompression
include_checksumtrueData integrity
dictionarynullNot commonly needed
expected_sizenullOnly needed for lz4_raw
max_output_sizenullNo limit (set for untrusted data)

Check what a codec supports:

const codec: cz.Codec = .zstd;
// Feature queries
codec.supportsStreaming(); // true
codec.supportsDictionary(); // true
codec.hasBuiltinChecksum(); // true
codec.isFramed(); // true
codec.requiresExpectedSize(); // false
// Metadata
codec.name(); // "Zstandard"
codec.extension(); // ".zst"

See Codec Detection for auto-detection.