Overview
zstd wraps Facebook's Zstandard library — the modern compression algorithm that achieves bzip2-level ratios at near-LZ4 speeds. It is the compression standard for Linux kernels, databases (RocksDB, MySQL), and container formats. Levels range from 1 (ultra-fast) to 22 (maximum ratio), plus a unique dictionary mode for compressing many small similar buffers.
CLib package
Requires libzstd-dev. Ubuntu/Debian: sudo apt install libzstd-dev
Quick Start
uses zstd;
var data := ReadFile('/data/events.json');
var compressed := Compress(data); // default level 3
var restored := Decompress(compressed);
WriteLn('Original: ', Length(data));
WriteLn('Compressed: ', Length(compressed));
WriteLn('Version: ', ZSTDVersion);
Compression Levels
var fastest := CompressLevel(data, ZSTD_LEVEL_FAST); // 1
var balanced := CompressLevel(data, ZSTD_LEVEL_DEFAULT); // 3
var good := CompressLevel(data, ZSTD_LEVEL_GOOD); // 9
var best := CompressLevel(data, ZSTD_LEVEL_BEST); // 19
var ultra := CompressLevel(data, ZSTD_LEVEL_ULTRA); // 22
Reusable Contexts (high throughput)
// Create once, reuse for many compressions — avoids per-call allocation
var cctx := NewCompressor(ZSTD_LEVEL_DEFAULT);
var dctx := NewDecompressor;
for I := 0 to Length(records) - 1 do begin
var packed := CompressCtx(cctx, records[I]);
// store packed...
end;
FreeCompressor(cctx);
FreeDecompressor(dctx);
Dictionary Mode
When compressing many small, similar buffers (e.g. JSON records, log lines), train a dictionary from samples to dramatically improve ratio:
// Train once
var dict := TrainDictionary(samples, 112 * 1024); // 112 KB dict
var dictBytes := DictToBytes(dict);
SaveFile('/data/zstd.dict', dictBytes);
// Compress with dictionary
var cctx := NewCompressor(3);
var packed := CompressWithDict(cctx, record, dict);
// Decompress with same dictionary
var dctx := NewDecompressor;
var back := DecompressWithDict(dctx, packed, dict);
FreeDict(dict);
File Compression
CompressFile('/data/dump.sql', '/data/dump.sql.zst');
CompressFileLevel('/data/large.bin', '/data/large.bin.zst', 9);
DecompressFile('/data/dump.sql.zst', '/data/dump.sql');
Package Info
Version1.0.0
Typeclib
C Librarylibzstd
Authorgustavo
Functions
- Compress
- CompressLevel
- Decompress
- NewCompressor
- CompressCtx
- FreeCompressor
- NewDecompressor
- DecompressCtx
- FreeDecompressor
- TrainDictionary
- CompressWithDict
- DecompressWithDict
- DictToBytes
- DictFromBytes
- FreeDict
- CompressFile
- CompressFileLevel
- DecompressFile
- GetDecompressedSize
- CompressBound
- ZSTDVersion
See Also
- lz4 (faster)
- lzma (better ratio)
- bzip2
- compression (zlib)