Skip to content

xz

currentMigrated from docs/data/xz.mdx at 37c3317fd058; modified for Encode Wiki · CC BY-SA 4.0

xz compresses with LZMA2, the algorithm behind 7z. It produces some of the smallest files of any common tool, decompresses reasonably fast, and is the standard for source tarballs (.tar.xz) and many package formats. Compression is slow and memory-hungry. If you compress once and distribute widely, xz is a good choice; if you compress often, use zstd.

Terminal window
xz file # file.xz, preset 6; deletes file
xz -9e -k file # maximum compression, keep the original
xz -d file.xz # decompress (or unxz)
xz -dc file.xz | less
xz -t file.xz # test integrity
xz -l file.xz # sizes and ratio

Note that xz deletes the input after success unless you pass -k.

-0 … -9 select dictionary size and match-finder effort. The default is -6. Appending -e (-6e, -9e) searches harder for a little extra compression at significant extra time.

PresetDictionaryCompress memoryDecompress memory
-11 MiB9 MiB2 MiB
-34 MiB32 MiB5 MiB
-68 MiB94 MiB9 MiB
-964 MiB674 MiB65 MiB

A dictionary larger than the input is wasted memory, so -9 on a 5 MB file is slower than -6 for no gain. The decompressor needs memory proportional to the dictionary, which matters for small devices and installers: a -9e file needs ~65 MiB to unpack.

Since XZ Utils 5.6.0, xz compresses multithreaded by default, using all cores. It splits the input into independent blocks, which slightly reduces the ratio and multiplies memory by the thread count, but lets the file be decompressed multithreaded too. Use -T1 for the single-threaded compressor when you need the smallest possible output or memory is tight. Older versions were single-threaded unless given -T0.

xz compresses one stream. Use tar for directories:

Terminal window
tar -cJf backup.tar.xz dir/
XZ_OPT='-9e' tar -cJf backup.tar.xz dir/
tar -xf backup.tar.xz

-J uses the default preset; set others through XZ_OPT or -I 'xz -9e'.

  • Integrity check: xz stores a CRC64 by default (--check=crc32|crc64|sha256|none). xz -t verifies it.
  • .lzma and lzip: the older raw LZMA format (xz --format=lzma) is obsolete. lzip is a separate format.
  • Not for HTTP: there is no xz HTTP content coding. Use brotli, zstd or gzip.
  • Memory limit errors while decompressing only happen when a limit is set (for example XZ_DEFAULTS=--memlimit=150MiB); xz has none by default. Raise it with --memlimit-decompress=….