# xz

Canonical URL: https://encode.wiki/codecs/data/xz/
Content version: ca42e61ede0a24f789d7287a0001244ad22f4abf
Status: current
License: CC BY-SA 4.0
Checksum (SHA-256): d637ea08e9c3efd9c4224ed33c2c703db4c310b19cc31283734fdd269c2a8192

`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](/codecs/data/zstd/).

## Common commands

```sh
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`.

## Presets

`-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.

| Preset | Dictionary | Compress memory | Decompress memory |
| ------ | ---------- | --------------- | ----------------- |
| `-1`   | 1 MiB      | 9 MiB           | 2 MiB             |
| `-3`   | 4 MiB      | 32 MiB          | 5 MiB             |
| `-6`   | 8 MiB      | 94 MiB          | 9 MiB             |
| `-9`   | 64 MiB     | 674 MiB         | 65 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`.

## Archives and pipelines

xz compresses one stream. Use [tar](/codecs/data/tar/) for directories:

```sh
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'`.

## Other notes

- **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](/codecs/data/brotli/), [zstd](/codecs/data/zstd/) or [gzip](/codecs/data/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=…`.
