Linux Tutorials

Linux File Compression: gzip vs bzip2 vs xz vs zstd Tested

zstd compressed a 1.7 GB kernel source tar in 4.95 seconds on one core. GNU gzip needed 35.69 seconds for the same file and still produced a bigger one. That single result decides most of what follows. gzip and zip still matter for compatibility and xz still earns its place on ratio, but zstd is the tool you should reach for first.

Original content from computingforgeeks.com - post 2411

This guide covers zip, gzip, bzip2, xz and zstd on Linux, plus the parallel versions (pigz, pbzip2, lbzip2) and the tar flags that tie them together. We measured ratio, compress and decompress time and peak memory on two real datasets, then broke each tool on purpose to capture the errors you will actually hit. The benchmark and walkthrough ran on Ubuntu 26.04 LTS (gzip 1.14, xz 5.8.3, zstd 1.5.7) in November 2026, and the installs and error cases were reproduced on Ubuntu 24.04 LTS and Rocky Linux 10.

Pick the right compressor for the job

Use zstd for anything you control on both ends: backups, log archives, container layers, data you move between your own servers. Use xz when the file is written once and downloaded many times, because the extra minutes of compression buy you the smallest file. Keep gzip and zip for the cases where the other side decides the format.

SituationUseWhy
Backups, database dumps, data in transitzstd -3 -T0Smaller than gzip on source code in a fraction of the time
Log archiveszstd -9 -T0Beats every gzip level on logs in under 3 seconds per 700 MB
Release tarballs, ISO images, packagesxz -6Near-top ratio in half the time zstd -19 needs
Files for Windows or macOS userszipOpens natively everywhere with no extra software
HTTP, older scripts, anything that expects .gzgzip or pigzUniversal decoder; pigz output is plain gzip
Existing .bz2 pipelineslbzip2Same format, uses every core

bzip2 doesn’t appear as a first choice anywhere in that table. On our numbers it lost to xz on ratio and to zstd on speed in both datasets, so the only reason to produce new .bz2 files is a consumer that can’t read anything else. Even lbzip2 doesn’t change that, because it runs the same algorithm faster without improving the ratio (8.64 against 10.36 for xz on the kernel tar).

Compare the benchmark results

The test box was a Google Cloud c3-standard-8 (8 vCPU, 32 GB RAM) on Ubuntu 26.04 LTS. Both inputs sat on tmpfs so disk speed did not skew the timings, and every tool except zip read stdin and wrote stdout. Each configuration ran once, timed with /usr/bin/time -f "%e %M" for wall seconds and peak resident memory, and every round trip was checked against the original SHA-256.

The two inputs pull in opposite directions. The kernel dataset is linux-7.3.6.tar from kernel.org, 1,719,797,760 bytes of mixed source code. The log dataset is BGL.log from the Loghub collection, 743,185,031 bytes of repetitive supercomputer logs that every tool squeezes hard.

Kernel source tar (1.7 GB)

CommandRatioCompress (s)Decompress (s)Peak RAM compressPeak RAM decompress
zip -66.3740.429.492.4 MB2.9 MB
gzip -66.3735.694.092.1 MB1.9 MB
gzip -96.4977.134.012.1 MB1.9 MB
pigz -66.376.043.977.4 MB2.2 MB
bzip2 -98.70117.2032.898.5 MB4.9 MB
lbzip2 -98.6412.124.3458 MB55 MB
xz -610.3696.141.59990 MB490 MB
xz -6 -T110.54522.907.3395 MB10 MB
xz -9e -T011.19269.192.245329 MB2188 MB
zstd -3 -T17.074.951.4640 MB7.0 MB
zstd -3 -T07.071.451.4677 MB7.0 MB
zstd -9 -T08.586.341.36175 MB9.3 MB
zstd -12 -T08.7812.541.36296 MB9.6 MB
zstd -19 -T110.26786.761.43219 MB13 MB
zstd -19 -T010.26207.991.40583 MB13 MB
zstd -19 --long=27 -T010.62204.421.59915 MB134 MB

Read the kernel table by pairs, because each pair answers a real question. zstd at level 3 beat gzip on both size and time, and with -T0 it finished in 1.45 seconds. At level 9 it matched bzip2 on ratio (8.58 against 8.70) in 6.34 seconds instead of 117.20. At the top end, though, xz took the crown: xz -6 reached 10.36 in 96.14 seconds, while zstd -19 -T0 needed 207.99 seconds to land slightly lower. Somewhere above level 12, zstd stops being the efficient choice, and xz gets you the same ratio in half the time.

Log corpus (743 MB)

CommandRatioCompress (s)Decompress (s)Peak RAM compressPeak RAM decompress
zip -611.8111.463.322.6 MB2.9 MB
gzip -611.819.371.022.1 MB1.9 MB
gzip -912.9438.550.982.1 MB1.9 MB
pigz -611.821.740.936.6 MB2.1 MB
bzip2 -915.4660.3211.107.8 MB4.9 MB
lbzip2 -915.485.341.5759 MB66 MB
xz -622.2941.880.59957 MB449 MB
xz -6 -T122.50198.032.5695 MB10 MB
xz -9e -T026.16247.631.033282 MB974 MB
zstd -3 -T110.591.470.6138 MB6.9 MB
zstd -3 -T010.590.460.6171 MB7.1 MB
zstd -9 -T014.052.370.46166 MB9.2 MB
zstd -12 -T014.214.710.45287 MB9.3 MB
zstd -19 -T121.70438.450.40215 MB13 MB
zstd -19 -T021.70120.470.40560 MB13 MB
zstd -19 --long=27 -T022.17140.530.47893 MB133 MB

Logs changed one result, and it matters if you archive logs. Default zstd came out about 11% larger than gzip -6 here (ratio 10.59 against 11.81), so on this kind of data the zstd default trades size for speed. Raise zstd to level 9 and it jumps past every gzip setting to 14.05 in 2.37 seconds, so for log archives use zstd -9 -T0 rather than the default. xz again won the high end with 22.29 at 41.88 seconds, and xz -9e squeezed out 26.16 if you can afford the memory.

Plotting the kernel results as ratio against compress time makes the trade visible. The best options sit toward the top left, and the chart shows zstd owning the fast half while xz owns the top:

zstd -3 -T0 beats gzip -6 on ratio in 1.45 seconds against 35.69, zstd -9 -T0 matches bzip2 -9 in 6.34 seconds, and xz -6 reaches a ratio of 10.36 in 96.14 seconds where zstd -19 -T0 needs 207.99.zstd -3 -T0 beats gzip -6 on ratio in 1.45 seconds against 35.69, zstd -9 -T0 matches bzip2 -9 in 6.34 seconds, and xz -6 reaches a ratio of 10.36 in 96.14 seconds where zstd -19 -T0 needs 207.99.
Compression ratio against compress time, 1.7 GB kernel source tar

The memory columns hide the biggest surprise. Since the 5.6 line, xz runs multithreaded by default, as the xz release notes record. That is why plain xz -6 used 990 MB on the kernel tar while xz -6 -T1 used 95 MB and took 522.90 seconds instead of 96.14. Threaded mode also splits the output into blocks, which costs a little ratio (10.36 against 10.54) but lets decompression run in parallel. Watch xz -9e -T0 in particular: it peaked at 5329 MB to compress and 2188 MB to decompress, enough to push a small VM into the OOM killer. On a memory-tight box, pin -T1 or set --memlimit-compress.

Absolute seconds will move with your CPU, but the broad ordering held on both datasets, and that ordering is what you should base a default on. Still, time a sample of your own data before you change a fleet-wide default, because the log corpus already flipped one result here. A few minutes of /usr/bin/time runs is cheaper than recompressing a year of archives.

Install the compression tools

Don’t assume a fresh server has all five. The Ubuntu 24.04 cloud image we started from had gzip, xz and zstd but no bzip2, zip, unzip or pigz. Rocky Linux 10 shipped gzip, xz and pigz, and was missing bzip2, zstd, zip and unzip. On Ubuntu or Debian, install everything in one go:

sudo apt update
sudo apt install -y gzip bzip2 xz-utils zstd zip unzip pigz pbzip2 lbzip2

On Rocky Linux and AlmaLinux 10, the core tools come from BaseOS and pbzip2 from AppStream. lbzip2 lives in EPEL, so enable that repository first:

sudo dnf install -y zip unzip zstd bzip2 xz pigz
sudo dnf install -y epel-release
sudo dnf install -y pbzip2 lbzip2

Confirm what you ended up with. The last line is worth a look because the help text is wrong. It claims a default of one thread, but current zstd picks between 1 and 4 workers from the logical core count, and our 8 vCPU box used 2. That is why the benchmark passes -T1 or -T0 on every zstd run, and your scripts should do the same so results don’t change from one machine to the next.

gzip --version | head -1
xz --version | head -1
zstd --version
zip -v | sed -n 2p
zstd -H | grep -A1 -- '-T#'

Here is the full version check from the Ubuntu 26.04 benchmark machine, including the parallel tools and tar:

gzip, bzip2, xz, zstd, zip, pigz, lbzip2 and tar versions on Ubuntu 26.04 with zstd help text for -T

To see how many workers zstd really starts, compress a throwaway file in verbose mode. On the same machine it reported two:

seq 1 3000000 > t.log
zstd -vvvv -f t.log 2>&1 | grep -i thread

The first line of the output is the one that matters:

Compressing with 2 worker threads
--format=.zst --no-sparse --block-size=0 --memory=134217728 --threads=2 --content-size

Ubuntu 24.04 and Rocky Linux 10 ship older xz and zstd builds than 26.04, and the flags used here have been stable across those releases, so the commands carry over to scripts that target any of them. The one behaviour that differs is xz threading: the Ubuntu 24.04 xz still compresses on one thread by default, while the newer xz on Rocky Linux 10 and Ubuntu 26.04 runs multithreaded, as the benchmark notes above explain.

Create and test zip archives

zip is both an archiver and a compressor, so it takes a whole directory without tar. For the demos below we extracted the kernel/ directory of the kernel tree as src (17 MB, 793 files) and cut the first 50 MB of the log corpus into app.log. Compress the directory at the highest level and keep the paths with -r:

zip -9 -r -q src.zip src

Test the archive before you trust it, then list it to check the file count:

zip -T src.zip
unzip -l src.zip | tail -1

To extract into a different directory, pass -d. Comparing the restored tree with diff -r is the cheapest proof that nothing was lost:

unzip -q src.zip -d restore
diff -r src restore/src && echo identical

The 17 MB tree came out as a 4.3 MB zip, and zip -T reported test of src.zip OK:

zip -9 -r creating an archive, zip -T integrity test and unzip restore verified with diff on Ubuntu

zip compresses each file on its own, which is why it lost to every tar-based format on the same tree later in this guide. That design is also what lets you pull a single file out of a large zip instantly, so it is a fair trade for archives people browse rather than restore in full. Use zip when a person opens the archive by hand, and tar with a stream compressor when a machine restores it.

Compress single files with gzip, bzip2 and xz

These three compress one file at a time and replace the original unless you pass -k. All three take numeric levels up to 9 and the same -d to decompress and -t to test, so once you know one you know them all:

gzip -9 -k app.log
gzip -l app.log.gz
bzip2 -9 -k app.log
xz -vv -6 -k app.log
xz -l app.log.xz

Check each result with its own test flag. Each prints nothing on success, so chain an echo if you want confirmation in a script log:

gzip -t app.log.gz && echo "gzip test passed"
bzip2 -t app.log.bz2 && echo "bzip2 test passed"
xz -t app.log.xz && echo "xz test passed"

Read the xz -vv lines closely. On Ubuntu 26.04 it printed Using up to 8 threads and needed 1321 MiB to compress a 50 MB file, with no -T flag on the command line:

gzip -l, bzip2 -t and xz -vv output showing xz using up to 8 threads by default

From the same 50 MB log, gzip produced 3,925,233 bytes, bzip2 3,347,907 and xz 2,175,096. The xz file is also split into three blocks, which is what threaded mode does and what lets it decompress in parallel. Every xz decoder reads multi-block files, so this is safe to share. Add -T1 when you want output identical to single-threaded xz (reproducible builds), a slightly better ratio, or less memory on small VMs, because that 1321 MiB comes on top of whatever else the box is running.

Use zstd for speed and ratio

zstd keeps the source file by default, unlike gzip, and prints a one-line summary for each file. Levels run from 1 to 19, and --ultra unlocks 20 to 22. The default is 3, which is where most of the speed in the benchmark came from:

zstd app.log
zstd -19 -T0 app.log -o app.log.19.zst
zstd --ultra -22 app.log -o app.log.22.zst
zstd -l app.log.zst app.log.19.zst app.log.22.zst

Test and decompress work the same way as gzip. unzstd is just zstd -d under another name:

zstd -t app.log.22.zst
unzstd app.log.zst -o restored.log
cmp app.log restored.log && echo identical

Level 19 halved the default output (4.42 MiB down to 2.22 MiB), while level 22 saved only another 0.02 MiB on top of that. That curve is typical, so treat --ultra as a last resort for files you publish, not a setting for backups. The zstd manual also warns that levels 20 and up need more memory to decompress, which lands on every reader of the file:

zstd default, level 19 and --ultra -22 results listed with zstd -l on a 50 MB log

Three flags are worth knowing beyond levels:

  • -T0 uses all physical cores (3.4x faster at level 3 on the kernel tar)
  • --rsyncable keeps output friendly to delta transfers
  • --long=27 finds repeats far apart, with no decoder flag needed

The rsyncable option matters when you ship compressed dumps with the rsync command, because a small change in the input no longer rewrites the whole compressed file, so only the changed blocks travel. The zstd manual lists the rest.

Combine tar with each compressor

gzip, bzip2, xz and zstd compress a single stream, so directories go through tar first. GNU tar has a short flag for each, and -I lets you pass any compressor with its own options. If you need a refresher on the rest of tar and file handling, our Linux commands cheat sheet covers it.

FormatCreateExtension
gziptar -czf.tar.gz, .tgz
bzip2tar -cjf.tar.bz2
xztar -cJf.tar.xz
zstdtar --zstd -cf.tar.zst
Pick by suffixtar -cafany of the above
Custom optionstar -I 'zstd -19 -T0' -cf.tar.zst

Build one archive in each format from the same src tree so the sizes are comparable:

tar -czf src.tar.gz src
tar -cjf src.tar.bz2 src
tar -cJf src.tar.xz src
tar --zstd -cf src.tar.zst src
tar -I 'zstd -19 -T0' -cf src-19.tar.zst src

Extraction needs no format flag at all. GNU tar reads the magic bytes and calls the right decompressor, so tar -xf works on every file above:

tar -tzf src.tar.gz | head -3
mkdir -p x && tar -xf src-19.tar.zst -C x
diff -r src x/src && echo identical

On this small tree xz edged out zstd level 19 (2,972,572 against 3,012,214 bytes), and the default zstd landed next to gzip. The zip from the earlier section was the largest file in the listing:

tar archives compressed with gzip, bzip2, xz and zstd compared by size

Small inputs flatten the differences, which is why the 1.7 GB benchmark is the better guide for real backups.

Use every core with pigz, pbzip2 and lbzip2

gzip and bzip2 use a single core no matter how many you have. pigz, pbzip2 and lbzip2 split the input into chunks and compress them in parallel, and the output stays in the standard format, so any gzip or bzip2 can read it:

pigz -6 -c app.log > app-pigz.gz
pbzip2 -9 -p8 -c app.log > app-pbzip2.bz2
lbzip2 -9 -n 8 -c app.log > app-lbzip2.bz2

Inside tar, hand the parallel tool to -I. Testing the result with plain gzip proves the format did not change:

tar -I pigz -cf src-pigz.tar.gz src
gzip -t src-pigz.tar.gz && echo "standard gzip reads it"

We timed each one with the bash time keyword. pigz cut the 50 MB log from 0.594 to 0.141 seconds, and lbzip2 finished in half the time pbzip2 took while burning less than half the CPU (2.857 against 6.009 seconds of user time):

time output for gzip vs pigz and pbzip2 vs lbzip2 compressing the same log file

The full benchmark shows the same pattern on the big file. pigz compressed the kernel tar in 6.04 seconds where gzip took 35.69, and lbzip2 took bzip2 from 117.20 down to 12.12. The output sizes barely moved, because both tools run the same algorithm on smaller chunks. So if a .gz or .bz2 is a hard requirement, these are free speed. If it is not, look at zstd. On the kernel tar zstd -3 -T0 beat pigz on both time and size, while on logs it took level 9 to win on size (2.37 against 1.74 seconds). On the same kernel tar, level 12 matched the lbzip2 ratio at similar speed while decompressing about three times faster. On logs lbzip2 kept a clear ratio lead (15.48 against 14.21), so test before you migrate a .bz2 pipeline.

Fix common compression errors

Each error below was reproduced on the lab machines, so the messages match what you will see on a current Ubuntu or Rocky Linux system. Each heading quotes the error line the tool prints, because that is what you will paste into a search box when it happens.

tar: project.tar.gz: Cannot stat: No such file or directory

This comes from flag order. -f takes the next word as the archive name, so in tar -cfz project.tar.gz project tar writes an uncompressed archive called z and then tries to add project.tar.gz as an input file:

tar -cfz project.tar.gz project

Keep f last in the bundle, immediately before the file name. A related trap: the extension doesn’t choose the compressor unless you use -a, so tar -czf project.tar.zst quietly writes gzip data with a misleading name:

rm -f z
tar -czf project.tar.gz project
tar -czf project.tar.zst project && file project.tar.zst
tar -caf project.tar.zst project && file project.tar.zst

The capture from Ubuntu 24.04 shows the stray 645,120-byte z file and the file output for both versions of the .zst archive:

tar -cfz Cannot stat No such file or directory error creating a file named z

A stray one-letter file next to your archive is the sign of this bug, because tar used the compression letter as the archive name. Delete it and fix the flag order, so the next run writes the compressed archive you expected.

/bin/sh: line 1: zstd: command not found

GNU tar doesn’t link the compressors in; it runs them as child processes. On a default Rocky Linux 10 install, tar --zstd fails with tar: Child returned status 127 because the zstd binary isn’t there:

Rocky Linux 10 tar --zstd failing with zstd command not found on a default install

Install the missing package (sudo dnf install -y zstd here) and rerun. Expect the same class of failure with -j on Ubuntu cloud images, which ship without bzip2, although the wording differs because Ubuntu’s /bin/sh is dash.

Decoding error (36) : Frame requires too much memory for decoding

The normal zstd window is too small to see repeats that sit hundreds of megabytes apart. --long widens it. We built a 1 GB test file from the same 512 MB of random data twice, and --long=30 shrank it to 513 MB where the default produced 1.1 GB. The catch is on the other end:

zstd -q -T0 -3 --long=30 twice.bin -o twice-long.zst
zstd -d twice-long.zst -c > /dev/null

Decompression stops with Window size larger than maximum : 1073741824 > 134217728, because the decoder refuses windows over 128 MiB unless you allow them. zstd even prints the fix:

zstd --long=30 halves a repeated file and decompression fails with Frame requires too much memory for decoding

Pass the same --long value (or --memory=1024MB) when you decompress. Anything above --long=27 needs this, so record the flag next to the archive or nobody else will be able to open it.

zstd -d --long=30 twice-long.zst -c > /dev/null && echo "decompressed OK"

The kernel benchmark ran --long=27, which the zstd manual lists as the largest window a decoder accepts without a flag, and it still bought a small gain over plain level 19 (10.62 against 10.26). It cost memory, though: 915 MB to compress and 134 MB to decompress, so leave it off on small machines.

zip error: Entry too big to split, read, or write

The classic zip format stores sizes in 32 bits, which caps an entry at 4 GiB. Info-ZIP zip switches to the Zip64 extension automatically when a file needs it, so a plain zip of a 5 GB log works. On Linux you hit this error when Zip64 is turned off with -fz-:

zip -q -fz- big-nozip64.zip big.log
zip -q big.zip big.log
zipinfo -v big.zip | grep -i 'minimum software'

The working archive lists 5,368,709,120 bytes and asks for version 4.5 to extract, which is Zip64. gzip handled the same file without complaint. Its trailer only keeps the size modulo 2^32, though, so file reported 1 GiB and gzip -l spent 26.6 seconds decoding the stream to get the true size:

zip -fz- Entry too big error on a 5 GB file and default zip switching to Zip64

If an old Windows tool or embedded device can’t open a large zip, it lacks Zip64 support, and the fix is on that side or a split archive. Streamed input (cat big.log | zip stream.zip -) gets Zip64 by default because zip can’t know the size in advance, per the zip man page.

Set a default and stop thinking about it

For logs, the same answer applies whether you rotate them yourself or let logrotate do it: our guide to truncating log files and setting up logrotate shows where the compress and delaycompress options go. Backup tools have already made the call too: the BorgBackup with Borgmatic setup runs its archives with zstd. Compression shrinks what you store, but it is not protection, so pair it with the copies described in our immutable backups with Restic, Borg and S3 Object Lock guide.

These are the defaults we now put in scripts:

  • zstd -T0 at the default level for backups and dumps
  • zstd -9 -T0 for log archives
  • xz -6 for published tarballs, with -T1 on small machines
  • zip or gzip for files other people open

After two datasets and three distributions, our verdict is simple. Make zstd -T0 your everyday compressor, at the default level for backups and level 9 for logs, and keep xz for files you publish. Treat gzip, zip and bzip2 as compatibility formats you produce only when the receiver asks for them. Test every archive you write with its -t flag, because a backup you never decompressed is only a guess.

Keep reading

How to Upgrade Fedora 44 to Fedora 45 (GUI, CLI and Rollback) Fedora How to Upgrade Fedora 44 to Fedora 45 (GUI, CLI and Rollback) Java4Shell (CVE-2026-114938): Find and Patch Vulnerable JVMs Security Java4Shell (CVE-2026-114938): Find and Patch Vulnerable JVMs Upgrade Ubuntu 24.04 to Ubuntu 26.04 LTS (Step by Step) Ubuntu Upgrade Ubuntu 24.04 to Ubuntu 26.04 LTS (Step by Step) Install Waydroid on Fedora 45 to Run Android Apps Fedora Install Waydroid on Fedora 45 to Run Android Apps Encapsule on Fedora 45: Isolated Dev Containers vs Toolbx Containers Encapsule on Fedora 45: Isolated Dev Containers vs Toolbx Configure Password Aging and Expiry Policy in Linux Security Configure Password Aging and Expiry Policy in Linux

2 thoughts on “Linux File Compression: gzip vs bzip2 vs xz vs zstd Tested”

  1. I followed this on Linux Mint. The results were outstanding, 68M down to 12K. Then I opened the file and found there was nothing inside it. I found that you need to use `-r` for recursive. So the command that worked for me was `zip -9 -r desktop.zip Desktop`

    Reply
  2. I used the following command:

    zip -FS9r example.zip example

    According to “du -sh,” the example folder is 23 gigs, and the zip file is 22 gigs…

    Is there no way to achieve higher compression by using another zip flag?

    Reply

Leave a Comment

Press ESC to close