Skip to content

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-examples

Example Directory ​

ExampleFileCommandDescription
Basic Compressionexamples/basic_compression.zigzig build run-basic_compressionOne-shot framed and raw block compression and decompression with allocator.
Caller Buffersexamples/caller_buffer.zigzig build run-caller_bufferZero-allocation compression & decompression into pre-allocated memory slices (compressInto, decompressInto).
File Compressionexamples/compress_file.zigzig build run-compress_file
zig build run-decompress_file
Compressing and decompressing disk files with Zig 0.17 std.Io.
Streaming Compressionexamples/streaming_compression.zigzig build run-streaming_compressionPush-based incremental chunk compression using StreamingCompressor.
Streaming Decompressionexamples/streaming_decompression.zigzig build run-streaming_decompressionPull-based incremental chunk decompression using StreamingDecompressor.
Reusable Contextexamples/reusable_context.zigzig build run-reusable_contextAmortizing allocation costs across thousands of operations with Compressor and Decompressor.
Error Handlingexamples/error_handling.zigzig build run-error_handlingRobust handling of corrupted streams, invalid chunk headers, and CRC-32C checksum mismatches.
Format Introspectionexamples/format_introspection.zigzig build run-format_introspectionInspecting uncompressed lengths, magic identifiers, and block bounds.
Bit-Level & Tag Encodingexamples/bit_level.zigzig build run-bit_levelUnderstanding Snappy varints, literal tags, and 1/2/4-byte copy offset tags.
I/O Pipelineexamples/io_pipeline.zigzig build run-io_pipelineIntegrating directly with Zig 0.17 std.Io.Reader and std.Io.Writer.

Snappy Format & Feature Matrix ​

ExampleFormat TypeHeader EmittedIntegrity CheckMemory Model
basic_compressionFramed Stream (.sz)\xff\x06\x00\x00sNaPpYMasked CRC-32CDynamic & Stack buffer
caller_bufferFramed & Raw BlockAuto-detectedMasked CRC-32C / NoneZero heap allocation
file_compressionFramed Stream (.sz)\xff\x06\x00\x00sNaPpYMasked CRC-32CZig 0.17.0 std.Io threaded filesystem
streaming_compressionFramed Stream (.sz)\xff\x06\x00\x00sNaPpYMasked CRC-32C64KB staging chunk buffer
streaming_decompressionFramed Stream (.sz)\xff\x06\x00\x00sNaPpYMasked CRC-32CPush/pull streaming ring buffer
reusable_contextFramed & Raw BlockConfigurableMasked CRC-32C / NoneReused hash tables & scratch memory
error_handlingFramed & Raw BlockValidatedMasked CRC-32C checkedSafe deterministic error returns
format_introspectionFramed & Raw BlockInspectedPre-validation$O(1)$ zero-allocation inspection
bit_levelLEB128 & Tag bytesByte-level layoutPolynomial 0x82f63b78Bit-level primitives
io_pipelineFramed Stream (.sz)\xff\x06\x00\x00sNaPpYMasked CRC-32C*std.Io.Reader $\rightarrow$ *std.Io.Writer

Released under the MIT License.