Introduction
XZ compresses a single file with the LZMA2 method, which usually gives smaller files
than gzip or bzip2
at the cost of slower compression. Lasse Collin designed the format, and
XZ Utils was released in 2009. Many Linux
distributions use it for software packages and source code releases, often as
.tar.xz. It replaced the older LZMA
file format, which had no magic number and no integrity check.
Structure
An XZ file is one or more streams. Each stream is framed by a header and a footer that mirror each other, so a reader can check it from either end.
0Stream header magicFD 37 7A 58 5A 00, stream flags, CRC-3212 bytes12Blocks each a block header, compressed data, padding and a check valuerepeated…Index the compressed and uncompressed size of every blockvaries…Stream footer CRC-32, index size, stream flags, magicYZ12 bytes
The stream flags name the check used on each block’s data: none, CRC-32, CRC-64 or SHA-256. The index lets a reader find any block and jump straight to it, and each block header lists the filters applied, normally LZMA2 and sometimes a filter that makes machine code compress better. Every field and code is on the reference pages: stream and index and blocks and filters.
What the explorer shows
Drop an .xz, .txz, .tar.xz (or .lzma) file on the
explorer. The tree holds every byte: stream headers and footers, each block’s header with its filter
flags, every LZMA2 chunk (control byte, sizes, properties, data), padding, checks, the index with its
records and the padding between streams. The index is compared with the blocks, and each block’s CRC32,
CRC64 or SHA-256 is computed and compared while the file is read, for every block whatever its size (blocks of any
size are decoded through a window, with the check computed as the data goes by, so memory stays near the
dictionary size; a very large file therefore takes a while to open, SHA-256 the longest: the page’s own SHA-256 code
took about 25 seconds for each GiB of decompressed data in a test with a 256 MiB block in Node.js (22.7 s for the
hash alone, 26 s with the decoding), and about 44 seconds per GiB when a 128 MiB file was opened in the browser (file reading
and drawing included), so a 4 GiB file with SHA-256 needs a few minutes, and a phone or a busy computer takes
longer; the file is read in a worker, so the page stays usable, but no percentage is shown). The decompressed data opens as a file of its own, named
from the file (app.tar.xz opens as app.tar, which the tar viewer reads). Data of more than 64 MiB opens as a
windowed file: it is never held whole, the hex view reads it in windows from any offset, and each read restarts from the nearest
saved decoder state (the start of a block, an LZMA2 chunk that resets the dictionary, or a full snapshot of the
decoder with the state of its Delta and branch filters). A block with a dictionary of hundreds of megabytes keeps no
snapshots, so a read deep inside it decodes from the start of the block: slow, never wrong; a match that reaches back
further than the 1 GiB the decoder holds (a dictionary of up to 4 GiB − 1 can be declared) is answered by decoding
that part again. Show on a block
opens the symbol trace: every literal, match, rep and short
rep with the range coder’s registers, pages of any size produced on demand, and a picture of the output linked to the bytes that made it.
Damaged files are shown as far as they can be read, with each problem named.
More about the explorer
Not part of the format: filter IDs from 0x4000000000000000 (262) up are
reserved for implementations’ own internal use, and the specification says they must never appear in a block
header; the viewer reports one as a problem and names it “reserved, never allowed in a block header”.
Check types 2, 3, 5 to 9 and 11 to 15 are reserved by the specification, so
the viewer names them and leaves their data and checks alone (the xz test files include a reserved check, which
is labelled). A bare LZMA or LZMA2 stream with no header, as 7z archives and some firmware images hold, is not
an .xz or .lzma file; the viewer shows such streams inside the files that carry them.
A block that cannot be read: the bytes after the point where it stops stay in the tree as
unread data, and the file is followed on from there. The stream’s footer is looked for in the bytes after the
damage (the footer’s CRC32, its stream flags equal to the header’s, an index that is sound and ends at the
footer, and records whose sizes add up to exactly the size of the stream from its header to that footer; an
.xz file stored whole inside the data does not fit this). When it is found, the index says where
each block starts, so the blocks after the damaged one are read, decoded and checked as usual; the damaged
block’s remaining bytes are shown as unread data inside it. When no footer is found, the next stream is looked
for by its header (the six magic bytes and a right CRC32 of the flags), and the bytes before it are shown as
unread data. When neither is found the bytes to the end of the file are shown as unread data. Streams are
joined with stream padding (zeros, a multiple of four) between them, and anything after the last stream that
is not another stream is an issue and stays in the tree.
Problems: every problem is kept, with its true count. The problems list shows the first 500 and pages
through the rest, 200 rows at a time (a
file with 593 wrong checks lists all 593). The index is read whole through a window that moves along it, so a valid
index of any size is checked (a test reads one of 65 MiB, 7.6 million records). While a very large file is read there
is no percentage, only the wait indicator: the worker that parses a file has no way to report progress.
The file bad-1-index-huge-uncomp.xz, which the xz repository’s README lists, is not in the 5.8.2 set of
test files and has not been tried.
Links
Software
Developers
Reference
- The .xz File Format – the specification from the Tukaani Project