zstd Documentation

Zstandard - Fast real-time compression algorithm

27,506 stars C C 1 file ยท ~5,178 tokens
RAW Doc

Docker & Self-Hosted Deployment Verified OCI

Deploy zstd on any server, homelab, or cloud container platform.

1-Click Terminal Command
bash (docker run)
docker run -d --name zstd --restart always facebook/zstd:latest

File: README.md

Zstandard, or zstd as short version, is a fast lossless compression algorithm,
targeting real-time compression scenarios at zlib-level and better compression ratios.
It's backed by a very fast entropy stage, provided by Huff0 and FSE library.

Zstandard's format is stable and documented in RFC8878. Multiple independent implementations are already available.
This repository represents the reference implementation, provided as an open-source dual BSD OR GPLv2 licensed C library,
and a command line utility producing and decoding .zst, .gz, .xz and .lz4 files.
Should your project require another programming language,
a list of known ports and bindings is provided on Zstandard homepage.

Development branch status:

Build Status
Build status
Build status
Fuzzing Status

Benchmarks

For reference, several fast compression algorithms were tested and compared
on a desktop featuring a Core i7-9700K CPU @ 4.9GHz
and running Ubuntu 24.04 (Linux 6.8.0-53-generic),
using lzbench, an open-source in-memory benchmark by @inikep
compiled with gcc 14.2.0,
on the Silesia compression corpus.

Compressor name Ratio Compression Decompress.
zstd 1.5.7 -1 2.896 510 MB/s 1550 MB/s
brotli 1.1.0 -1 2.883 290 MB/s 425 MB/s
zlib 1.3.1 -1 2.743 105 MB/s 390 MB/s
zstd 1.5.7 --fast=1 2.439 545 MB/s 1850 MB/s
quicklz 1.5.0 -1 2.238 520 MB/s 750 MB/s
zstd 1.5.7 --fast=4 2.146 665 MB/s 2050 MB/s
lzo1x 2.10 -1 2.106 650 MB/s 780 MB/s
lz4 1.10.0 2.101 675 MB/s 3850 MB/s
snappy 1.2.1 2.089 520 MB/s 1500 MB/s
lzf 3.6 -1 2.077 410 MB/s 820 MB/s

The negative compression levels, specified with --fast=#,
offer faster compression and decompression speed
at the cost of compression ratio.

Zstd can also offer stronger compression ratios at the cost of compression speed.
Speed vs Compression trade-off is configurable by small increments.
Decompression speed is preserved and remains roughly the same at all settings,
a property shared by most LZ compression algorithms, such as zlib or lzma.

The following tests were run
on a server running Linux Debian (Linux version 4.14.0-3-amd64)
with a Core i7-6700K CPU @ 4.0GHz,
using lzbench, an open-source in-memory benchmark by @inikep
compiled with gcc 7.3.0,
on the Silesia compression corpus.

Compression Speed vs Ratio Decompression Speed

A few other algorithms can produce higher compression ratios at slower speeds, falling outside of the graph.
For a larger picture including slow modes, click on this link.

The case for Small Data compression

Previous charts provide results applicable to typical file and stream scenarios (several MB). Small data comes with different perspectives.

The smaller the amount of data to compress, the more difficult it is to compress. This problem is common to all compression algorithms, and reason is, compression algorithms learn from past data how to compress future data. But at the beginning of a new data set, there is no "past" to build upon.

To solve this situation, Zstd offers a training mode, which can be used to tune the algorithm for a selected type of data.
Training Zstandard is achieved by providing it with a few samples (one file per sample). The result of this training is stored in a file called "dictionary", which must be loaded before compression and decompression.
Using this dictionary, the compression ratio achievable on small data improves dramatically.

The following example uses the github-users sample set, created from github public API.
It consists of roughly 10K records weighing about 1KB each.

Compression Ratio Compression Speed Decompression Speed

These compression gains are achieved while simultaneously providing faster compression and decompression speeds.

Training works if there is some correlation in a family of small data samples. The more data-specific a dictionary is, the more efficient it is (there is no universal dictionary).
Hence, deploying one dictionary per type of data will provide the greatest benefits.
Dictionary gains are mostly effective in the first few KB. Then, the compression algorithm will gradually use previously decoded content to better compress the rest of the file.

Dictionary compression How To:

  1. Create the dictionary

    zstd --train FullPathToTrainingSet/* -o dictionaryName

  2. Compress with dictionary

    zstd -D dictionaryName FILE

  3. Decompress with dictionary

    zstd -D dictionaryName --decompress FILE.zst

Build instructions

make is the main build system of this project.
It is the reference, and other build systems are periodically updated to stay compatible.
However, small drifts and feature differences can be present, since perfect synchronization is difficult.
For this reason, when your build system allows it, prefer employing make.

Makefile

Assuming your system supports standard make (or gmake),
just invoking make in root directory generates zstd cli at root,
and also generates libzstd into lib/.

Other standard targets include:

  • make install : install zstd cli, library and man pages
  • make check : run zstd, test its essential behavior on local platform

The Makefile follows the GNU Standard Makefile conventions,
allowing staged install, standard compilation flags, directory variables and command variables.

For advanced use cases, specialized flags which control binary generation and installation paths are documented
in lib/README.md for the libzstd library
and in programs/README.md for the zstd CLI.

cmake

A cmake project generator is available for generating Makefiles or other build scripts
to create the zstd binary as well as libzstd dynamic and static libraries.
The repository root now contains a minimal CMakeLists.txt that forwards to build/cmake,
so you can configure the project with a standard cmake -S . invocation,
while the historical cmake -S build/cmake entry point remains fully supported.

bash
cmake -S . -B build-cmake
cmake --build build-cmake

By default, CMAKE_BUILD_TYPE is set to Release.

Support for Fat (Universal2) Output

zstd can be built and installed with support for both Apple Silicon (M1/M2) as well as Intel by using CMake's Universal2 support.
To perform a Fat/Universal2 build and install use the following commands:

bash
cmake -S . -B build-cmake-debug -G Ninja -DCMAKE_OSX_ARCHITECTURES="x86_64;x86_64h;arm64"
cd build-cmake-debug
ninja
sudo ninja install

Meson

A Meson project is provided within build/meson. Follow
build instructions in that directory.

You can also take a look at .travis.yml file for an
example about how Meson is used to build this project.

Note that default build type is release.

VCPKG

You can build and install zstd vcpkg dependency manager:

text
git clone https://github.com/Microsoft/vcpkg.git
cd vcpkg
./bootstrap-vcpkg.sh
./vcpkg integrate install
./vcpkg install zstd

The zstd port in vcpkg is kept up to date by Microsoft team members and community contributors.
If the version is out of date, please create an issue or pull request on the vcpkg repository.

Conan

You can install pre-built binaries for zstd or build it from source using Conan. Use the following command:

bash
conan install --requires="zstd/[*]" --build=missing

The zstd Conan recipe is kept up to date by Conan maintainers and community contributors.
If the version is out of date, please create an issue or pull request on the ConanCenterIndex repository.

Visual Studio (Windows)

Going into build directory, you will find additional possibilities:

  • Projects for Visual Studio 2008 and 2010.
    • VS2010 project is compatible with VS2012, VS2013, VS2015 and VS2017.
  • Automated build scripts for Visual compiler by @KrzysFR, in build/VS_scripts,
    which will build zstd cli and libzstd library without any need to open Visual Studio solution.
  • It is now recommended to generate Visual Studio solutions from cmake

Buck

You can build the zstd binary via buck by executing: buck build programs:zstd from the root of the repo.
The output binary will be in buck-out/gen/programs/.

Bazel

You can integrate zstd into your Bazel project by using the module hosted on the Bazel Central Repository.

Testing

You can run quick local smoke tests by running make check.
If you can't use make, execute the playTest.sh script from the src/tests directory.
Two env variables $ZSTD_BIN and $DATAGEN_BIN are needed for the test script to locate the zstd and datagen binary.
For information on CI testing, please refer to TESTING.md.

Status

Zstandard is deployed within Meta and many other large cloud infrastructures,
to compress humongous amounts of data in various formats and use cases.
It is also continuously fuzzed for security issues by Google's oss-fuzz program.

License

Zstandard is dual-licensed under BSD OR GPLv2.

Contributing

The dev branch is the one where all contributions are merged before reaching release.
Direct commit to release are not permitted.
For more information, please read CONTRIBUTING.


File: contrib/docker/README.md

Requirement

The Dockerfile script requires a version of docker >= 17.05

Installing docker

The official docker install docs use a ppa with a modern version available:
https://docs.docker.com/install/linux/docker-ce/ubuntu/

How to run

docker build -t zstd .

test

text
echo foo | docker run -i --rm zstd | docker run -i --rm zstd zstdcat
foo

File: contrib/externalSequenceProducer/README.md

externalSequenceProducer

externalSequenceProducer is a test tool for the Block-Level Sequence Producer API.
It demonstrates how to use the API to perform a simple round-trip test.

A sample sequence producer is provided in sequence_producer.c, but the user can swap
this out with a different one if desired. The sample sequence producer implements
LZ parsing with a 1KB hashtable. Dictionary-based parsing is not currently supported.

Command line :

text
externalSequenceProducer filename

File: contrib/gen_html/README.md

gen_html - a program for automatic generation of zstd manual

Introduction

This simple C++ program generates a single-page HTML manual from zstd.h.

The format of recognized comment blocks is following:

  • comments of type /*! mean: this is a function declaration; switch comments with declarations
  • comments of type /** and /*- mean: this is a comment; use a <H2> header for the first line
  • comments of type /*= and /**= mean: use a <H3> header and show also all functions until first empty line
  • comments of type /*X where X is different from above-mentioned are ignored

Moreover:

  • ZSTDLIB_API is removed to improve readability
  • typedef are detected and included even if uncommented
  • comments of type /**< and /*!< are detected and only function declaration is highlighted (bold)

Usage

The program requires 3 parameters:

text
gen_html [zstd_version] [input_file] [output_html]

To compile program and generate zstd manual we have used:

text
make
./gen_html.exe 1.1.1 ../../lib/zstd.h zstd_manual.html

File: contrib/largeNbDicts/README.md

largeNbDicts

largeNbDicts is a benchmark test tool
dedicated to the specific scenario of
dictionary decompression using a very large number of dictionaries.
When dictionaries are constantly changing, they are always "cold",
suffering from increased latency due to cache misses.

The tool is created in a bid to investigate performance for this scenario,
and experiment mitigation techniques.

Command line :

text
largeNbDicts [Options] filename(s)

Options : 
-z          : benchmark compression (default) 
-d          : benchmark decompression 
-r          : recursively load all files in subdirectories (default: off) 
-B#         : split input into blocks of size # (default: no split) 
-#          : use compression level # (default: 3) 
-D #        : use # as a dictionary (default: create one) 
-i#         : nb benchmark rounds (default: 6) 
--nbBlocks=#: use # blocks for bench (default: one per file) 
--nbDicts=# : create # dictionaries for bench (default: one per block) 
-h          : help (this text) 
 
Advanced Options (see zstd.h for documentation) : 
--dedicated-dict-search
--dict-content-type=#
--dict-attach-pref=#

File: contrib/linux-kernel/README.md

Zstd in the Linux Kernel

This directory contains the scripts needed to transform upstream zstd into the version imported into the kernel. All the transforms are automated and tested by our continuous integration.

Upgrading Zstd in the Linux Kernel

  1. cd into this directory.
  2. Run make libzstd and read the output. Make sure that all the diffs printed and changes made by the script are correct.
  3. Run make test and ensure that it passes.
  4. Import zstd into the Linux Kernel make import LINUX=/path/to/linux/repo
  5. Inspect the diff for sanity.
  6. Check the Linux Kernel history for zstd. If any patches were made to the kernel version of zstd, but not to upstream zstd, then port them upstream if necessary.
  7. Test the diff. Benchmark if necessary. Make sure to test multiple architectures: At least x86, i386, and arm.
  8. Submit the patch to the LKML.

File: contrib/match_finders/README.md

Edit Distance Match Finder

text
/* Detailed source-code truncated for AI context efficiency. */

File: contrib/pzstd/README.md

Parallel Zstandard (PZstandard)

Parallel Zstandard is a Pigz-like tool for Zstandard.
It provides Zstandard format compatible compression and decompression that is able to utilize multiple cores.
It breaks the input up into equal sized chunks and compresses each chunk independently into a Zstandard frame.
It then concatenates the frames together to produce the final compressed output.
Pzstandard will write a 12 byte header for each frame that is a skippable frame in the Zstandard format, which tells PZstandard the size of the next compressed frame.
PZstandard supports parallel decompression of files compressed with PZstandard.
When decompressing files compressed with Zstandard, PZstandard does IO in one thread, and decompression in another.

Usage

PZstandard supports the same command line interface as Zstandard, but also provides the -p option to specify the number of threads.
Dictionary mode is not currently supported.

Basic usage

text
pzstd input-file -o output-file -p num-threads -#          # Compression
pzstd -d input-file -o output-file -p num-threads          # Decompression

PZstandard also supports piping and fifo pipes

text
cat input-file | pzstd -p num-threads -# -c > /dev/null

For more options

text
pzstd --help

PZstandard tries to pick a smart default number of threads if not specified (displayed in pzstd --help).
If this number is not suitable, during compilation you can define PZSTD_NUM_THREADS to the number of threads you prefer.

Benchmarks

As a reference, PZstandard and Pigz were compared on an Intel Core i7 @ 3.1 GHz, each using 4 threads, with the Silesia compression corpus.

Compression Speed vs Ratio with 4 Threads Decompression Speed with 4 Threads

The test procedure was to run each of the following commands 2 times for each compression level, and take the minimum time.

text
time pzstd -# -p 4    -c silesia.tar     > silesia.tar.zst
time pzstd -d -p 4    -c silesia.tar.zst > /dev/null

time pigz  -# -p 4 -k -c silesia.tar     > silesia.tar.gz
time pigz  -d -p 4 -k -c silesia.tar.gz  > /dev/null

PZstandard was tested using compression levels 1-19, and Pigz was tested using compression levels 1-9.
Pigz cannot do parallel decompression, it simply does each of reading, decompression, and writing on separate threads.

Tests

Tests require that you have gtest installed.
Set GTEST_INC and GTEST_LIB in Makefile to specify the location of the gtest headers and libraries.
Alternatively, run make googletest, which will clone googletest and build it.
Run make tests && make check to run tests.


File: contrib/seekable_format/README.md

Zstandard Seekable Format

The seekable format splits compressed data into a series of independent "frames",
each compressed individually,
so that decompression of a section in the middle of an archive
only requires zstd to decompress at most a frame's worth of extra data,
instead of the entire archive.

The frames are appended, so that the decompression of the entire payload
still regenerates the original content, using any compliant zstd decoder.

On top of that, the seekable format generates a jump table,
which makes it possible to jump directly to the position of the relevant frame
when requesting only a segment of the data.
The jump table is simply ignored by zstd decoders unaware of the seekable format.

The format is delivered with an API to create seekable archives
and to retrieve arbitrary segments inside the archive.

Maximum Frame Size parameter

When creating a seekable archive, the main parameter is the maximum frame size.

At compression time, user can manually select the boundaries between segments,
but they don't have to: long segments will be automatically split
when larger than selected maximum frame size.

Small frame sizes reduce decompression cost when requesting small segments,
because the decoder will nonetheless have to decompress an entire frame
to recover just a single byte from it.

A good rule of thumb is to select a maximum frame size roughly equivalent
to the access pattern when it's known.
For example, if the application tends to request 4KB blocks,
then it's a good idea to set a maximum frame size in the vicinity of 4 KB.

But small frame sizes also reduce compression ratio,
and increase the cost for the jump table,
so there is a balance to find.

In general, try to avoid really tiny frame sizes (<1 KB),
which would have a large negative impact on compression ratio.


File: contrib/VS2005/README.md

Project Support Notice

The VS2005 Project directory has been moved to the contrib directory in order to indicate that it will no longer be supported.