Skip to content

Examples ​

Practical examples showing how to use zstd.zig. All 20 examples live in examples/ with _ names and are run via zig build run-<name>.

All 20 Examples ​

ExampleFileDescriptionRun Command
basic_compressionexamples/basic_compression.zigBasic compress/decompress round tripzig build run-basic_compression
custom_levelexamples/custom_level.zigNumeric compression levels 1-22 via compressWithLevelzig build run-custom_level
advanced_paramsexamples/advanced_params.zigCustom window, checksum, strategy via CompressionOptionszig build run-advanced_params
custom_strategyexamples/custom_strategy.zigAll nine match-finding strategies comparedzig build run-custom_strategy
compression_boundexamples/compression_bound.zigSizing an output buffer with compressBoundzig build run-compression_bound
window_limitexamples/window_limit.zigBounding the declared window a decoder acceptszig build run-window_limit
frame_iterationexamples/frame_iteration.zigWalking frames in a buffer with FrameIteratorzig build run-frame_iteration
long_distance_matchingexamples/long_distance_matching.zigLong-distance matching, and when it does not helpzig build run-long_distance_matching
parallel_compressionexamples/parallel_compression.zigApplication-managed threads, one context per workerzig build run-parallel_compression
dictionary_compressionexamples/dictionary_compression.zigTrain a dictionary, compress against it, decode with itzig build run-dictionary_compression
dictionary_trainingexamples/dictionary_training.zigTraining via DictionaryBuilder.train, trainCover, trainFastCoverzig build run-dictionary_training
prepared_dictionaryexamples/prepared_dictionary.zigPreparing dictionary state once and reusing itzig build run-prepared_dictionary
streaming_compressionexamples/streaming_compression.zigStreaming compression with StreamingCompressor + EndDirectivezig build run-streaming_compression
streaming_decompressionexamples/streaming_decompression.zigStreaming decompression with StreamingDecompressorzig build run-streaming_decompression
custom_allocatorexamples/custom_allocator.zigCustom TrackingAllocator trackingzig build run-custom_allocator
error_handlingexamples/error_handling.zigCorruption, truncation, and buffer error caseszig build run-error_handling
legacy_decompressionexamples/legacy_decompression.zigHistoric frame detection for v01-v07 and skippable frameszig build run-legacy_decompression
file_compressionexamples/file_compression.zigReal file compression, write to .zst archive, and decompressionzig build run-file_compression
explicit_io_streamingexamples/explicit_io_streaming.zigChunked streaming file compression, decompression, and Encoder/Decoder contextszig build run-explicit_io_streaming
large_file_compressionexamples/large_file_compression.zigMulti-megabyte (4 MiB) compression, ratio inspection, and disk preservationzig build run-large_file_compression

Run all at once:

bash
zig build run-all-examples

Per-Example Guides ​

Every example above has a page here with its source, its real captured output, and what the output means.

GuideExample FileFocus
Basic Compressionbasic_compression.zigcompress/decompress round-trip, ratio
Custom Levelcustom_level.zigcompressWithLevel, and why every level ties here
Advanced Paramsadvanced_params.ziggetCompressionParameters, checksum, windowLog, strategy
Custom Strategycustom_strategy.zigAll nine Strategy values, round-tripped
Compression Boundcompression_bound.zigcompressBound as an upper bound, compressInto
Window Limitwindow_limit.zigmaxWindowSize boundary, one-shot and streaming
Frame Iterationframe_iteration.zigFrameIterator, skippable frames, truncation
Long-Distance Matchinglong_distance_matching.zigLDM options, and why it currently saves nothing
Parallel Compressionparallel_compression.zigOne context per thread, and one frame across a worker pool
Dictionary Compressiondictionary_compression.zigTrain, compress against, decode with
Dictionary Trainingdictionary_training.zigtrain, trainCover, trainFastCover
Prepared Dictionaryprepared_dictionary.zigprepareDictionary, reuse, wrong-dictionary refusal
Streaming Compressionstreaming_compression.zigStreamingCompressor, EndDirective, remaining
Streaming Decompressionstreaming_decompression.zigStreamingDecompressor, needsMore
Custom Allocatorcustom_allocator.zigA caller-supplied counting allocator
Error Handlingerror_handling.zigPrefixUnknown, SrcSizeWrong, DstSizeTooSmall
Legacy Decompressionlegacy_decompression.zigHistoric detection, v0.1-v0.5 decode, v0.6-v0.7 refusal
File & Directory Compressionfile_compression.zigFile and directory tree to .zst and back, with std.Io.Dir
Explicit IO Streamingexplicit_io_streaming.zigstd.Io chunk streaming, StreamingCompressor, StreamingDecompressor, Encoder, Decoder
Large File Compressionlarge_file_compression.zig4 MiB buffer compression, ratio, throughput, and disk preservation

Validate ​

bash
zig build test --summary all
zig build run-all-examples
zig build check
zig build -Dtarget=aarch64-linux
zig build -Dtarget=x86_64-windows
zig build -Dtarget=aarch64-macos
zig build -Dtarget=x86-windows

Released under the MIT License.