Skip to content
Latchkey

xz: Compress with -9 -T0 for Small Artifacts

xz compresses files to .xz using LZMA2, giving the best ratio of the common CLIs, with -T0 to use every core for speed.

xz is what you use when artifact size matters more than compress time (release tarballs, kernel sources). Multi-threading with -T0 makes -9 tolerable in CI.

What it does

xz compresses each file with LZMA2, appends .xz, and removes the source unless -k is given. unxz or xz -d reverses it. -T0 enables multi-threaded compression across all cores, at a small cost to ratio.

Common usage

Terminal
xz -9 -T0 release.tar          # best ratio, all cores
xz -k -6 build.log            # keep source, default-ish level
tar cf - src/ | xz -9 > src.tar.xz
xz -d package.tar.xz          # decompress

Options

FlagWhat it does
-9Maximum compression (large dictionary)
-T0 / --threads=0Use all available cores (0 = auto)
-e / --extremeExtra effort at a given level for a bit more ratio
-k / --keepKeep the input file
-c / --stdoutWrite to stdout
-d / --decompressDecompress (like unxz)

In CI

Add -T0 so xz -9 uses every core; without it xz is single-threaded and slow. Multi-threaded compression splits into blocks, which very slightly reduces ratio but is worth it in a pipeline. Install with apt-get install -y xz-utils or apk add xz.

Common errors in CI

"xz: (stdin): File format not recognized" on decompress means the input is not xz (often gzip or zstd); check with file. "xz: Cannot allocate memory" at -9 on small runners is real: -9 needs roughly 700 MB per thread, so lower the level or thread count. "xz: command not found" means xz-utils is not installed.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →