DEV Community

Cover image for Zstandard Explained in 2 Episodes — Episode 2
Christian Ahrweiler
Christian Ahrweiler

Posted on Originally published at Medium

Zstandard Explained in 2 Episodes — Episode 2

Episode 2: How Zstandard Compares With Other Compression MethodsEpisode 2: How Zstandard Compares With Other Compression MethodsZST, ZIP, GZIP, Brotli, LZ4, and XZ Compared*In Episode 1, we saw how Zstandard detects repetitions and replaces them with shorter references.*Zstandard is only one of several compression methods. ZIP, GZIP, Brotli, LZ4, and XZ all pursue the same basic goal: reducing file size without changing the content.

However, they have different priorities.

Some compress particularly quickly. Others produce particularly small files. Some remain widely used primarily because they can be opened on almost any device.

File Format and Compression Method

Before comparing them, we need to distinguish between two things:

A file format defines how the compressed data is stored. The compression method defines how the data is reduced.For a ZST file, the relationship is straightforward:

File format .zst
Compression method ZstandardZIP and GZIP are slightly more complicated. Both commonly use the older Deflate compression method.

ZIP file Deflate
GZIP file DeflateThat does not mean ZIP and GZIP are the same format.

A ZIP file can store multiple files and folders in a single archive. Each file can be compressed separately within the archive.

GZIP, by contrast, normally compresses a single stream of data. If multiple files need to be stored together, they are therefore often combined with TAR first and then compressed with GZIP.

Multiple files

TAR combines them

GZIP compresses the TAR archive

Archive.tar.gzThe same principle is used to create .tar.xz and .tar.zst files.

What Does “Better Compression” Mean?

Suppose our original file is 100 megabytes.

Method A reduces it to 50 megabytes. Method B reduces it to 35 megabytes.

Method B compresses the file more effectively. However, that does not necessarily make it the better choice for every task.

A meaningful comparison must consider at least three properties:

File size after compression
Time required for compression
Time required for decompressionMemory usage and software compatibility also matter.

One method may produce a particularly small file but take a long time to compress it. Another may create a slightly larger file but decompress it much faster.

ZIP and GZIP

ZIP and GZIP commonly use Deflate. This compression method dates back to the early 1990s and combines two ideas:

First, repetitions are replaced with references. Frequently occurring sequences are then stored using shorter codes.

The basic principle is similar to the method introduced in Episode 1. Modern compressors can usually process repetitions more thoroughly or more quickly.

ZIP’s greatest strength is its widespread support.

ZIP files can be opened on practically every modern computer and smartphone without installing additional software. They can also store multiple files and folders directly in one archive.

GZIP is particularly common on web servers and Unix systems. It is frequently used to compress individual files, data streams, or TAR archives.

ZIP and GZIP therefore remain useful when broad compatibility is more important than maximum speed or the smallest possible file.

Brotli

Brotli was developed primarily for transferring web content.

Websites contain many recurring elements: HTML instructions, CSS properties, JavaScript expressions, and common words. Brotli also includes a built-in dictionary containing frequently occurring sequences.

This allows known elements to be represented using short references.

Brotli can produce particularly small files for text and web content. At higher compression levels, however, compression takes considerably longer.

That is often acceptable for a website. A JavaScript or CSS file may be compressed only once but subsequently transferred to visitors millions of times.

LZ4

LZ4 was designed for speed.

It also searches for data that has already occurred. However, it spends less processing time searching for the smallest possible representation.

As a result, LZ4 usually produces larger files than Zstandard, Brotli, or XZ. In return, it can compress very quickly and decompress even faster.

This is useful when data is compressed only temporarily or must become available again particularly quickly — for example in memory, databases, caches, or running computer systems.

LZ4 answers the question:

How can we reduce the data without noticeably slowing down access to it?### XZ

An XZ file normally uses the LZMA2 compression method.

LZMA2 searches very thoroughly for repetitions and can consider a large area of previously processed data. The information it finds is then encoded particularly efficiently.

This can produce very small files. However, the thorough search requires time and memory. Decompression is also usually slower than with Zstandard or LZ4.

XZ is therefore particularly suitable for files that are compressed once and then stored or distributed frequently.

Software packages are a typical example: creating the archive may take longer if this reduces the storage space and transfer volume required later.

Zstandard

Zstandard is designed to provide a balanced relationship between file size and speed. It combines effective compression with high compression speed and particularly fast decompression.

Before compression begins, the compression level can be selected according to the desired priority:

Low level faster compression
Medium level balanced result
High level smaller fileThe ZST file already contains all the information required for decompression. The decompressing application therefore does not need to know which level was used to create it.

Zstandard also supports dictionaries for many small files with similar structures. We introduced this method in Episode 1.

A Comparison Under Identical Conditions

Speed figures are meaningful only when all methods are tested using the same data and the same computer.

The following results come from a benchmark published by the Zstandard project. The mixed Silesia dataset was tested on a Core i7–9700K. All methods were measured on the same computer, using the same benchmark and a single processor core. The selected compression levels prioritize speed and do not represent the strongest compression each method can achieve.

Source: Official Zstandard benchmark

Method Ratio Compression Decompression

Zstandard, level 1 2.896 510 MB/s 1,550 MB/s
Brotli, fast setting 2.883 290 MB/s 425 MB/s
Deflate/zlib, level 1 2.743 105 MB/s 390 MB/s
LZ4 2.101 675 MB/s 3,850 MB/sLZ4 compresses and decompresses extremely quickly, but produces the largest file. Brotli achieves compression similar to Zstandard, but operates considerably more slowly. Deflate, which is commonly used by ZIP and GZIP, is slower during both compression and decompression and produces a slightly larger file. In this comparison, Zstandard provides the most balanced combination of file size, compression speed, and decompression speed.*XZ is intentionally absent from this table. It belongs to the slower group of compression methods and was not included in this comparison of fast compressors.*A compression ratio of 2.896 means:

Original size ÷ 2.896

Compressed sizeIn this test, a 100-megabyte file would therefore be reduced to approximately 34.5 megabytes.

Why Is There No Single Winner?

The result depends heavily on the content.

A text file contains different repetitions from a database. Application files differ from photos, music, and videos.

The selected compression level also affects the result. Two methods cannot be compared simply by looking at their highest level numbers. The levels have different meanings for each method.

The intended use is another important factor.

If a file is compressed only once but downloaded very frequently, compression may take longer. If a server continuously compresses new data, speed becomes more important.

Which Method Is Suitable for Which Purpose?

ZIP Simple exchange and broad compatibility
GZIP Individual files and TAR archives on Unix systems
Brotli HTML, CSS, and JavaScript on the web
LZ4 Extremely fast compression and decompression
XZ Particularly small archives when time matters less
Zstandard Effective compression at high speedThese are not fixed rules. They simply illustrate the usual priority of each method.

Why Is Zstandard Becoming More Common?

LZ4 is faster but produces larger files. XZ can produce smaller files but requires more time. ZIP and GZIP offer very broad compatibility but usually rely on the older Deflate method.

Zstandard lies between these extremes.

It can be fast enough for active systems, compress effectively enough for backups and archives, and make the data available again very quickly. Its compression levels allow applications to adjust this balance to their particular requirements.

The Zstandard format is openly documented and standardized as RFC 8878.

Summary

All the methods described here use lossless compression. After decompression, the exact original file is restored.

The difference is not whether they work correctly, but how they distribute their effort:

LZ4 maximum speed
Zstandard balanced performance
Brotli strong compression for web content
XZ small files at the cost of more time
ZIP/GZIP very broad compatibilityThe best format therefore cannot be determined by file size alone.

The real question is:

How small must the file become, how quickly must it be created, and how quickly do we need to access its content again?ZST files can be opened with unpackZST:

unpackZST for iPhone, iPad, and Mac

unpackzst.com

Top comments (0)