Some Bits: Nelson's log

zstandard as gzip replacement for files

Some notes on how to use the zstandard compression system for files as a replacement for gzip. I spent a lot of time looking for subtle but important differences but really zstadnard is quite easy. zstandard files are named “.zst” instead of “.gz” and there are command line tools that are mostly equivalents for gzip.

  • gzip -> zstd --rm
  • gunzip -> unzstd
  • zcat -> zstdcat
  • tar czf -> tar --zstd -cf
  • tar xzf -> tar xaf
  • pigz -> zstdmt

Why use zstandard? Because it is much faster and smaller. I’ve used gzip for years and years to compress files. (I am old enough to have used BSD compress and have the .Z files to prove it.) I briefly used bzip2 but it just took way too long to compress to be worth it. I also use pigz when I remember too, the parallel gzip, and that is a lot better. But, eh, what about the new hotness?

zstandard seems mostly to be an algorithm product. the zstd(1) command line tool is pretty good though and is thoughtfully documented. It mostly does the things you want with files, preserving timestamps and permissions and such. he one important difference is that zstd does not remove the original uncompressed source file, unlike gzip. There’s a --rm option to zstd if you want that behavior.

The default compression level of 3 is fine. I assume it’s the default for a reason. Higher levels get slower very rapidly for not a huge advantage. Using the 100MB Wikipedia corpus from this benchmark the highest normal level 19 makes a file that’s 25% smaller but takes 100 times as long. Some quickie data:

AlgorithmCompression ratioCompression speed
source10
zstd -32.809201 MB/s
zstd -93.7101.92 MB/s
gzip2.73925 MB/s
gzip -92.74419 MB/s
bzip23.44716 M B/s

zstdmt or pzstd for parallelism. Much like pigz, it’s possible to compress with zstandard in parallel. pigz compresses my test data in 9% of the time of single threaded gzip; 11x speedup for a 16 thread Ryzon 3700. Both pzstd and zstdmt take 20% of the time of single thread zstandard. Not sure why it’s not better than that, didn’t dive deep. zstdmt produces a file that’s identical to single threaded zstd. pzstd makes a very slightly bigger file; I’m guessing it’s doing a block approach like pigz does.

Recovery from corruption may be a weak point for zstandard. One nice thing about gzip is that it compresses in chunks. If one byte is corrupted in a gzip you don’t lose the whole file, you can use gzrecover or gzrt and maybe get most of your data back. (This chunking is also how pigz parallelizes). I’ve actually done this for real once and was grateful to get most of my tarball back. I don’t think zstd(1) has any equivalent behavior by default, and this GitHub issue suggests the zstd team thinks it’s out of scope. There is block chunking via the -B flag (and probably pzstd) but in the absence of an open source recovery program I wouldn’t count on it.

I see no reason to not use zstandard everywhere instead of gzip. Except habit, and I suspect in reality I won’t change my behavior unless it’s something where it really matters.