Introduction
Bzip2 compresses a single file. Julian Seward released
bzip2 in 1996. It sorts each block of data
with the Burrows–Wheeler transform, which groups similar text together, and then codes
the result with move-to-front, run-length and Huffman coding. It compresses better than
gzip but more slowly, and has largely given way to
xz and Zstandard.
A tar archive compressed with it is named .tar.bz2, or .tbz2
or .tbz for short.
bzip2 has no official specification: the format is what bzip2 and its library, libbzip2, read and write, and the bzip2 manual says it has stayed the same since the first releases. Joe Tsai’s format description of 2016 sets it out from the sources. The pages here follow bzip2 1.0.8, the current version, and were checked against it.
Structure
A file is one or more streams. A stream starts with BZh and a digit
from 1 to 9 giving the block size in units of 100,000 bytes.
After that it is a sequence of bits, not bytes, read from the most significant bit of
each byte down: blocks start at any bit, and only the end of the stream is padded to a
whole byte.
0Stream headerBZhand the block size digit4 bytes4Blocks each starts with the 48-bit magic31 41 59 26 53 59, its CRC, the randomised bit and the origin pointer, then its tables and Huffman-coded symbolsrepeated…End of stream the 48-bit magic17 72 45 38 50 90, the stream CRC made from the block CRCs, and padding to a whole byte80 to 87 bits
The block magic is the start of π and the end magic the start of √π, in binary-coded decimal. Each block is compressed on its own in five stages: a first run-length coding, the Burrows–Wheeler transform, move-to-front, a coding of the runs of zeros that leaves, and Huffman coding with up to six tables. Streams joined end to end decompress as one file.
- Streams – the header, bit order, end of stream, CRCs, joined streams and two example files
- Blocks – every field of a block, the symbol alphabet, the checks bzip2 makes, and a worked example
- Compression stages – RLE1, the Burrows–Wheeler transform, move-to-front, RUNA and RUNB, Huffman coding and randomised blocks
Versions
bzip2’s predecessor, bzip 0.21, coded its output arithmetically and started its files
with BZ0; bzip2 cannot read them, and neither can the explorer. In bzip2’s
own format the one thing that changed is randomisation: bzip2 before 0.9.5 could set a
block’s randomised bit when sorting very repetitive data took too long, and later
versions never write it but still read it. bzip2 1.0.8 of July 2019 is the current
release; it reads files that list more selectors than a block can use, which 1.0.7
refused when fixing a security flaw (CVE-2019-12900).
In the explorer
Drop a .bz2, .tbz2 or .tbz file on this site.
The decompressed data opens as a file of its own, in its own viewer:
.tar.bz2 as a tar archive, .jsonl.bz2 as JSON Lines. The
structure tree shows every stream, block and field, and Show on a block lists its
tables, selectors and every symbol with its bits. Blocks are decoded and checked
while the output stays under 16 MB; past that, and after damage, the rest of the file
is found by searching for the block magics, and those blocks are decoded when they
are opened.
Not described
bzip2 has no written specification; these pages follow the program’s own source and Tsai’s description of the format.
Known, not yet covered
- Reference and viewer: bzip 0.21 (
BZ0) files, whose only description is the old program’s source. The viewer does not decode them.
Links
Software
Developers
- libbzip2 – the library that comes with bzip2 (C)
- bz2 – standard library module (Python)
- compress/bzip2 – standard library package that decompresses bzip2 (Go)
- Apache Commons Compress – library that reads and writes bzip2 among other formats (Java)
Reference
- bzip2 and libbzip2 manual – the program’s documentation; there is no separate format specification
- BZip2: Format Specification – Joe Tsai’s description of the format, worked out from the bzip2 sources (PDF)