Examples Overview
The examples/ directory contains complete, standalone example programs demonstrating real-world compression patterns with snappy.zig on Zig 0.17.0.
Every example is self-contained, adheres to the official Google Snappy format specification, and can be compiled and executed directly using the Zig build system.
Running the Examples
bash
# Run a specific example
zig build run-basic_compression
zig build run-caller_buffer
zig build run-reusable_context
zig build run-streaming_compression
zig build run-streaming_decompression
zig build run-io_pipeline
zig build run-error_handling
zig build run-format_introspection
zig build run-bit_level
zig build run-compress_file
zig build run-decompress_file
# Run all registered examples sequentially
zig build run-all-examplesExample Directory
| Example | File | Command | Description |
|---|---|---|---|
| Basic Compression | examples/basic_compression.zig | zig build run-basic_compression | One-shot framed and raw block compression and decompression with allocator. |
| Caller Buffers | examples/caller_buffer.zig | zig build run-caller_buffer | Zero-allocation compression & decompression into pre-allocated memory slices (compressInto, decompressInto). |
| File Compression | examples/compress_file.zig | zig build run-compress_filezig build run-decompress_file | Compressing and decompressing disk files with Zig 0.17 std.Io. |
| Streaming Compression | examples/streaming_compression.zig | zig build run-streaming_compression | Push-based incremental chunk compression using StreamingCompressor. |
| Streaming Decompression | examples/streaming_decompression.zig | zig build run-streaming_decompression | Pull-based incremental chunk decompression using StreamingDecompressor. |
| Reusable Context | examples/reusable_context.zig | zig build run-reusable_context | Amortizing allocation costs across thousands of operations with Compressor and Decompressor. |
| Error Handling | examples/error_handling.zig | zig build run-error_handling | Robust handling of corrupted streams, invalid chunk headers, and CRC-32C checksum mismatches. |
| Format Introspection | examples/format_introspection.zig | zig build run-format_introspection | Inspecting uncompressed lengths, magic identifiers, and block bounds. |
| Bit-Level & Tag Encoding | examples/bit_level.zig | zig build run-bit_level | Understanding Snappy varints, literal tags, and 1/2/4-byte copy offset tags. |
| I/O Pipeline | examples/io_pipeline.zig | zig build run-io_pipeline | Integrating directly with Zig 0.17 std.Io.Reader and std.Io.Writer. |
Snappy Format & Feature Matrix
| Example | Format Type | Header Emitted | Integrity Check | Memory Model |
|---|---|---|---|---|
basic_compression | Framed Stream (.sz) | \xff\x06\x00\x00sNaPpY | Masked CRC-32C | Dynamic & Stack buffer |
caller_buffer | Framed & Raw Block | Auto-detected | Masked CRC-32C / None | Zero heap allocation |
file_compression | Framed Stream (.sz) | \xff\x06\x00\x00sNaPpY | Masked CRC-32C | Zig 0.17.0 std.Io threaded filesystem |
streaming_compression | Framed Stream (.sz) | \xff\x06\x00\x00sNaPpY | Masked CRC-32C | 64KB staging chunk buffer |
streaming_decompression | Framed Stream (.sz) | \xff\x06\x00\x00sNaPpY | Masked CRC-32C | Push/pull streaming ring buffer |
reusable_context | Framed & Raw Block | Configurable | Masked CRC-32C / None | Reused hash tables & scratch memory |
error_handling | Framed & Raw Block | Validated | Masked CRC-32C checked | Safe deterministic error returns |
format_introspection | Framed & Raw Block | Inspected | Pre-validation | $O(1)$ zero-allocation inspection |
bit_level | LEB128 & Tag bytes | Byte-level layout | Polynomial 0x82f63b78 | Bit-level primitives |
io_pipeline | Framed Stream (.sz) | \xff\x06\x00\x00sNaPpY | Masked CRC-32C | *std.Io.Reader $\rightarrow$ *std.Io.Writer |