Introduction
compressionz is a fast, ergonomic compression library for Zig that provides a unified API across multiple compression algorithms.
The Problem
Section titled “The Problem”When working with compression in most languages, you face several challenges:
- Fragmented APIs — Each codec has its own interface, options, and error handling
- System dependencies — Libraries like zstd or brotli require system installation
- Performance trade-offs — Choosing between speed, ratio, and memory usage
- Missing features — Many bindings lack streaming, dictionaries, or proper error handling
The Solution
Section titled “The Solution”compressionz solves all of these with:
Unified API
Section titled “Unified API”// Same interface for every codecconst compressed = try cz.compress(.zstd, data, allocator);const compressed = try cz.compress(.lz4, data, allocator);const compressed = try cz.compress(.gzip, data, allocator);const compressed = try cz.compress(.snappy, data, allocator);const compressed = try cz.compress(.brotli, data, allocator);Switch codecs by changing a single enum value. No API changes, no refactoring.
Zero System Dependencies
Section titled “Zero System Dependencies”All C libraries are vendored directly in the repository:
- zstd 1.5.7 — BSD licensed
- zlib 1.3.1 — zlib licensed
- brotli — MIT licensed
No brew install, no apt-get, no pkg-config. Just zig build.
Pure Zig Performance
Section titled “Pure Zig Performance”LZ4 and Snappy are implemented in pure Zig with explicit SIMD optimizations:
// 16-byte vectorized comparisonconst v1: @Vector(16, u8) = src[pos..][0..16].*;const v2: @Vector(16, u8) = src[match_pos..][0..16].*;const mask = @as(u16, @bitCast(v1 == v2));This achieves 36+ GB/s compression throughput — competitive with hand-optimized C.
Complete Feature Set
Section titled “Complete Feature Set”- Streaming — Process large files without loading into memory
- Dictionaries — Better ratios for small data with known patterns
- Zero-copy — Compress into pre-allocated buffers
- Auto-detection — Identify format from magic bytes
- Archives — ZIP and TAR support built-in
- Safety — Decompression bomb protection with
max_output_size
When to Use compressionz
Section titled “When to Use compressionz”Use compressionz when you need:
- Multiple compression algorithms in one project
- Predictable, cross-platform builds
- High performance without sacrificing ergonomics
- Streaming or dictionary compression
- ZIP/TAR archive handling
Consider alternatives when you need:
- Only one specific codec (use specialized bindings)
- Async I/O integration (compressionz is synchronous)
- Formats not yet supported (xz, lzma, etc.)
Codec Overview
Section titled “Codec Overview”| Codec | Best For | Speed | Ratio |
|---|---|---|---|
| Zstd | General purpose | Fast | Excellent |
| LZ4 | Maximum speed | Fastest | Good |
| Snappy | Real-time applications | Very fast | Moderate |
| Gzip | HTTP compatibility | Moderate | Good |
| Brotli | Static web assets | Slow | Best |
See Choosing a Codec for detailed guidance.
Next Steps
Section titled “Next Steps”Ready to get started?