Learning Center · Compression · Intermediate

Choosing a compression algorithm: gzip, xz, Zstandard, Brotli

Compare gzip, bzip2, xz, Zstandard and Brotli by size, speed and memory, then pick one for web assets, backups, archives or files other people must open.

By listme.name Updated 6 min read 1:26 video Share

1:26 video with captions. The text below has the detail and the sources.
Read the video transcript

The trade

Every compressor trades compressed size against speed and memory. Which one fits your job?

Five common formats

gzip is the long-standing baseline. bzip2 is usually smaller than gzip, but slower. xz gives small output, and compresses slowly. Zstandard spans many speeds, with fast decompression. Brotli carries a built-in dictionary for text.

Speed against size

Plot each format by speed and output size. gzip sits in the middle. bzip2 gives smaller output than gzip, but takes longer. xz gives small output, and compresses slowest. Zstandard is a line, from fast settings to small output.

Choose by job

Web assets are compressed once and served often, so Brotli or gzip. Backups and logs favor speed, so Zstandard. Archives and releases favor size, so xz. Files others must open get zip or gzip, for the widest reach.

When it will not help

A better algorithm cannot shrink data that has no redundancy left. Test on a sample before you pick a slower setting.

What to remember

Start from the job, not from the name of the algorithm. Read the full guide for the choices and the desktop tools that use them.

Choose a compression algorithm by the job it has to do. gzip is the long-standing baseline, xz makes small files slowly, Zstandard lets you pick a point on a speed scale, and Brotli suits text such as web pages. Every choice trades compressed size against compression speed and decompression speed, and some also need more memory. The video above plots the main options against each other.

Three things you trade

A compressor is judged on three measurements: how small the output is, how fast it compresses, and how fast it decompresses. Memory use comes next, because some settings need a large working buffer. A single algorithm rarely wins all three, so the question is which one your job cares about most.

Most jobs care about one measurement more than the others. A nightly backup is compressed once a day but restored rarely, so compression speed matters more than the last few percent of size. A release archive is compressed once and downloaded many times, so size matters more. The Zstandard project notes that decompression speed stays roughly the same at all of its settings, a property it shares with most LZ compression algorithms such as zlib and LZMA. In other words, a faster setting mostly speeds up compression, and restores stay fast.

gzip and bzip2: the established options

gzip is the GNU program first released in 1992, and its format is specified in RFC 1952. It uses DEFLATE, the same method inside ZIP, which is explained in LZ77 and DEFLATE.

bzip2 takes a different route. It compresses with the Burrows-Wheeler block sorting transform followed by Huffman coding, and the bzip2 manual says its compression is generally considerably better than that of LZ77 and LZ78-based compressors. The price is speed: Wikipedia notes that bzip2 compresses most files more effectively than LZW and Deflate but is considerably slower, although its decompression is relatively fast. The bzip2 home page calls it "not the world's fastest compressor" and adds that it supports limited recovery of undamaged parts of a file after media errors.

xz: small output, slow compression

xz stores its data with LZMA2, which the LZMA article describes as a container format that allows multi-threaded compression and decompression. The xz file format specification defines LZMA2 as one of its filters. Wikipedia's XZ Utils article says that in most cases xz achieves higher compression rates than zip, gzip and bzip2. It also says that decompression is faster than bzip2 but slower than gzip, and that compression "can be much slower than gzip". Its best use is a file that will be read many times after it is compressed once.

Memory is the other cost. The LZMA article notes that the RAM needed for decompression depends mainly on the sliding window used during compression, and that dictionary sizes can be, and usually are, much larger than in DEFLATE. High settings can therefore need a great deal of memory, and XZ Utils includes a memory-usage limit option.

Zstandard: a dial from fast to small

Zstandard, or zstd, describes itself as a fast compression algorithm that provides high compression ratios. Its reference library offers a very wide range of speed and compression trade-offs, from fast negative levels to the strongest settings, and it has an extremely fast decoder. The format is documented in RFC 8878, whose authors, Y. Collet and M. Kucherawy, were at Facebook when it was published in 2021. The RFC is informational, so it is a description of the format rather than an Internet Standard.

Zstandard also has a dictionary mode for small data. Compression algorithms learn from earlier data, so a short file has little to learn from. Zstandard can be trained on sample files of one type, such as many small records with the same fields, and the resulting dictionary is loaded before compressing and decompressing. The project notes that one dictionary per type of data gives the greatest benefit.

The reference command-line tool can also decode .gz and .xz files, according to the project's README, which makes it easy to try on existing archives.

Brotli: a built-in dictionary for text

Brotli is specified in RFC 7932. It combines LZ77 with Huffman coding, and it carries a static dictionary of words and fragments that the decompressor already knows. The RFC describes that dictionary in its section on static dictionaries, and Wikipedia describes it as a predefined set of common words, phrases and substrings. Text that uses those words compresses better, which is why Brotli suits web pages and other text. The IANA registry for HTTP content codings lists br for Brotli, as RFC 7932 registers it.

Choose by job

Start from the job, then pick the format.

  • Web assets. Compress once at build time and serve the compressed copy many times. Brotli and gzip are the common choices. MDN's example of a browser's default Accept-Encoding header lists gzip, deflate, br and zstd, so browsers advertise Zstandard support as well. Check your audience and server before relying on it.
  • Backups and logs. These are compressed often, so favor speed. Zstandard at a fast setting keeps compression cheap, and fast decompression helps restores.
  • Long-term archives and software releases. These are compressed once and size matters most, so xz is a good fit. Accept the slow compression, and check that the machine doing the restore has the memory for the settings you used.
  • Files other people must open. Use ZIP or gzip. A recipient without a newer tool cannot open a .zst or .br file, so these formats suit the widest audience.

The desktop tools that create these formats are compared in the file archivers ranking, and the backup software ranking covers backup tools. The guide how to choose a file archiver explains the same trade-offs for desktop use.

When a better algorithm will not help

A better algorithm cannot shrink data that has no redundancy left. Wikipedia notes that no lossless compression algorithm can shrink the size of all possible data, by the pigeonhole principle, and that compression algorithms cannot shrink random data with no redundancy. Compressed files behave the same way. A JPEG, an MP3, an encrypted file or a ZIP of photos has little to gain from a stronger algorithm. Test on a sample of your data before you choose a slower setting for a small saving. The lossless vs lossy compression lesson explains which data is already compressed by design.

Sources