Skip to content

Introduction

compressionz is a fast, ergonomic compression library for Zig that provides a unified API across multiple compression algorithms.

When working with compression in most languages, you face several challenges:

  1. Fragmented APIs — Each codec has its own interface, options, and error handling
  2. System dependencies — Libraries like zstd or brotli require system installation
  3. Performance trade-offs — Choosing between speed, ratio, and memory usage
  4. Missing features — Many bindings lack streaming, dictionaries, or proper error handling

compressionz solves all of these with:

// Same interface for every codec
const 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.

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.

LZ4 and Snappy are implemented in pure Zig with explicit SIMD optimizations:

// 16-byte vectorized comparison
const 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.

  • 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

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.)
CodecBest ForSpeedRatio
ZstdGeneral purposeFastExcellent
LZ4Maximum speedFastestGood
SnappyReal-time applicationsVery fastModerate
GzipHTTP compatibilityModerateGood
BrotliStatic web assetsSlowBest

See Choosing a Codec for detailed guidance.

Ready to get started?

  1. Install compressionz
  2. Follow the Quick Start guide
  3. Choose the right codec for your use case