Installation
compressionz can be added to any Zig project using the standard build system.
Requirements
Section titled “Requirements”- Zig 0.15.0 or later
- No additional system dependencies
Installation via build.zig.zon
Section titled “Installation via build.zig.zon”Step 1: Add the Dependency
Section titled “Step 1: Add the Dependency”Add compressionz to your build.zig.zon:
.{ .name = "my-project", .version = "0.1.0", .dependencies = .{ .compressionz = .{ .url = "https://github.com/NerdMeNot/compressionz/archive/refs/tags/v1.0.0-zig0.15.2.tar.gz", .hash = "1220...", // Zig will tell you this on first build }, }, .paths = .{"."},}For local development, use a path dependency:
.dependencies = .{ .compressionz = .{ .path = "../compressionz", },},Step 2: Configure build.zig
Section titled “Step 2: Configure build.zig”Add compressionz to your build configuration:
const std = @import("std");
pub fn build(b: *std.Build) void { const target = b.standardTargetOptions(.{}); const optimize = b.standardOptimizeOption(.{});
// Get compressionz dependency const compressionz = b.dependency("compressionz", .{ .target = target, .optimize = optimize, });
// Create your executable const exe = b.addExecutable(.{ .name = "my-app", .root_source_file = b.path("src/main.zig"), .target = target, .optimize = optimize, });
// Add compressionz module exe.root_module.addImport("compressionz", compressionz.module("compressionz"));
b.installArtifact(exe);}Step 3: Import and Use
Section titled “Step 3: Import and Use”const std = @import("std");const cz = @import("compressionz");
pub fn main() !void { var gpa = std.heap.GeneralPurposeAllocator(.{}){}; defer _ = gpa.deinit(); const allocator = gpa.allocator();
const data = "Hello, compressionz!";
const compressed = try cz.compress(.zstd, data, allocator); defer allocator.free(compressed);
std.debug.print("Compressed {} bytes to {} bytes\n", .{ data.len, compressed.len, });}Step 4: Build
Section titled “Step 4: Build”# Build your projectzig build
# Run your projectzig build runVerifying Installation
Section titled “Verifying Installation”Create a simple test file to verify everything works:
const std = @import("std");const cz = @import("compressionz");
pub fn main() !void { var gpa = std.heap.GeneralPurposeAllocator(.{}){}; defer _ = gpa.deinit(); const allocator = gpa.allocator();
const original = "The quick brown fox jumps over the lazy dog.";
// Test each codec inline for ([_]cz.Codec{ .lz4, .snappy, .zstd, .gzip, .brotli }) |codec| { const compressed = try cz.compress(codec, original, allocator); defer allocator.free(compressed);
const decompressed = try cz.decompress(codec, compressed, allocator); defer allocator.free(decompressed);
std.debug.print("{s}: {d} -> {d} bytes ✓\n", .{ codec.name(), original.len, compressed.len, });
std.debug.assert(std.mem.eql(u8, original, decompressed)); }
std.debug.print("\nAll codecs working correctly!\n", .{});}Expected output:
LZ4: 44 -> 63 bytes ✓Snappy: 44 -> 46 bytes ✓Zstandard: 44 -> 53 bytes ✓Gzip: 44 -> 64 bytes ✓Brotli: 44 -> 48 bytes ✓
All codecs working correctly!Build Optimization
Section titled “Build Optimization”For production builds, always use release optimizations:
# Development (debug)zig build
# Production (optimized)zig build -Doptimize=ReleaseFast
# Smaller binaryzig build -Doptimize=ReleaseSmall
# Safe release (keeps safety checks)zig build -Doptimize=ReleaseSafePerformance difference is significant:
| Mode | LZ4 Compression | Zstd Compression |
|---|---|---|
| Debug | ~500 MB/s | ~200 MB/s |
| ReleaseFast | ~36 GB/s | ~12 GB/s |
Troubleshooting
Section titled “Troubleshooting”Hash Mismatch
Section titled “Hash Mismatch”If you see a hash mismatch error:
error: hash mismatchexpected: 1220abc...found: 1220def...Update your build.zig.zon with the correct hash shown in the error.
Missing Dependency
Section titled “Missing Dependency”If the build can’t find compressionz:
error: unable to find dependency 'compressionz'- Check your
build.zig.zonspelling - Ensure the URL or path is correct
- Run
zig buildagain to fetch dependencies
C Compilation Errors
Section titled “C Compilation Errors”compressionz vendors C libraries. If you see C compilation errors:
- Ensure you’re using Zig 0.15.0+
- Check your target is supported
- File an issue with the full error output
Next Steps
Section titled “Next Steps”Now that compressionz is installed:
- Quick Start Guide — Learn basic usage
- Choosing a Codec — Pick the right algorithm
- API Reference — Full API documentation