File types · Explainer

The BZ2 file (.bz2)

A BZ2 file is a single file compressed with bzip2, which squeezes text harder than gzip by sorting it first. This guide explains what's inside one, how its block-sorting compression works, how it compares with gzip and XZ, and has free sample BZ2 files to download for testing.

At a glance

Full name
bzip2 compressed file
Extensions
.bz2; for tar archives .tar.bz2, .tbz2, .tbz, .tb2
MIME type
application/x-bzip2 (in wide use, not registered with IANA)
Kind
Binary, lossless compression of one file (not an archive)
Standard
No formal specification; defined by the reference program and library, libbzip2
First released
1996, by Julian Seward
Compression
Burrows-Wheeler transform, move-to-front and Huffman coding, in blocks of 100 to 900 kB
Integrity check
CRC-32 per block, plus a combined CRC for the stream
Opens with
bunzip2 on Linux and macOS, 7-Zip on Windows; .tar.bz2 in recent Windows 11 File Explorer
Magic number
42 5A 68 (BZh), a digit 1 to 9, then 31 41 59 26 53 59

The basics

What is a BZ2 file?

A BZ2 file is one file compressed with bzip2. dump.sql becomes dump.sql.bz2, and decompressing it gives back the original exactly. Like gzip, bzip2 compresses a single file and has no idea of folders; a folder is first bundled with tar, giving a .tar.bz2 (or .tbz2) file.

Julian Seward released bzip2 in 1996. For text it compressed noticeably better than gzip, so through the late 1990s and 2000s many open-source projects published .tar.bz2 downloads next to .tar.gz. Since then most have moved on to XZ or Zstandard, which compress about as well and decompress much faster.

You still meet .bz2 in older software archives, scientific data sets, and Wikipedia's database dumps, which use a multi-stream bzip2 layout so that single articles can be read without decompressing everything.

Anatomy

What's inside a BZ2 file

A bzip2 file has a 4-byte header and then one or more compressed blocks. Only the start is aligned to whole bytes: after the first block's checksum, everything is a stream of bits, so the end marker can start in the middle of a byte. Here is sample-text.txt.bz2, 1,104 bytes.

sample-text.txt.bz2 (hex and bits)
offset      bytes / bits             part
0           42 5A 68                 "BZh": the signature
3           39                       "9": 900 kB blocks (bzip2 -9)
4           31 41 59 26 53 59        block magic: 3.14159265359
10          99 F3 5A 08              block CRC-32
14          00 01 …                  from here on, bits not bytes:
                                     1 bit: randomised? (0 = no)
                                     24 bits: BWT origin pointer
                                     map of which byte values occur
                                     2 to 6 Huffman tables
                                     Huffman-coded symbols, end of block
1093 bit 7  17 72 45 38 50 90        end-of-stream magic: √π
1099 bit 7  99 F3 5A 08              combined CRC of all blocks
1103 bit 7  0                        1 padding bit
1104                                 end of file
  1. 142 5A 68

    The signature

    BZh: B and Z for bzip, and h for Huffman coding. Its predecessor, bzip (version 1), was withdrawn and isn't compatible.

  2. 239

    Block size

    The ASCII digit 9 means blocks of 900 kB (900,000 bytes) of input. bzip2 -1 writes 1 and uses 100 kB blocks. The level sets this size and nothing else.

  3. 331 41 59 26 53 59

    Block header magic

    The first twelve digits of π, as packed decimal. Every block starts with this 48-bit pattern, which lets recovery tools find blocks in a damaged file.

  4. 499 F3 5A 08

    Block CRC

    A CRC-32 of the block's original data, checked after decompressing that block.

  5. 500 01 …

    The compressed block

    A flag bit (old bzip2 versions could randomise input; current ones never do), the position the Burrows-Wheeler transform needs to undo itself, a bitmap of which byte values appear, the Huffman tables, and the coded data.

  6. 617 72 45 38 50 90

    End-of-stream marker

    The digits of √π (1.7724538509). It begins at bit 8,751, seven bits into byte 1,093, which is why you won't find it with a simple byte search.

  7. 799 F3 5A 08 (again)

    Combined CRC

    Each block's CRC is folded into a stream CRC. With only one block, the two are the same value.

  8. 80

    Padding

    Zero bits to finish the last byte. There's no stored file name or date: bunzip2 names the output by dropping .bz2.

Under the hood

How bzip2 works: sorting before compressing

gzip and xz look back for repeated strings. bzip2 does something different: it rearranges each block so that similar bytes end up next to each other, then compresses the result. The stages, in order:

  1. 1Run-length encoding: runs of 4 to 255 identical bytes are shortened first.
  2. 2Burrows-Wheeler transform (BWT): imagine every rotation of the block, sorted alphabetically. The last letter of each sorted rotation is written out. Because similar contexts sort together, this output has long runs of the same character. It's reversible with one extra number, the origin pointer.
  3. 3Move-to-front: each byte is replaced by its position in a list of recently seen bytes, so those runs turn into lots of zeros and small numbers.
  4. 4Run-length encoding of zeros, then Huffman coding with up to six code tables, switching table every 50 symbols to suit the data.

Each block is compressed on its own, which has three consequences. Bigger blocks (-9, the default) give the sorting more to work with and compress better. Blocks can be compressed in parallel: pbzip2 and lbzip2 use every CPU core and write files any bzip2 can read. And damage stays local: bzip2recover can rescue the intact blocks of a broken file.

bzip2 is modest with memory. At -9, the bzip2 manual gives about 7.6 MB to compress and 3.7 MB to decompress, or 2.35 MB with the slower -s option. Lower levels need less.

Trade-offs

bzip2 vs gzip and XZ: size and speed

Our own measurement: an 11.7 MB tar of a TypeScript web app's source code plus generated CSV, JSON, log and PNG files. One core of a 2.8 GHz Xeon cloud server, best of three runs.
Tool and levelOutput sizeCompression speedDecompression speed
gzip -6 (default)20.2%25 MB/s160 MB/s
bzip2 -9 (default)14.7%7 MB/s22 MB/s
xz -6 (default)15.6%1.2 MB/s66 MB/s

On this text-heavy data, bzip2 made the smallest file of the three and compressed six times faster than xz. Its weak point is decompression: about seven times slower than gzip and three times slower than xz. For a download that's compressed once and unpacked many times, that's the wrong way round, which is the main reason projects switched to xz. On other data, especially binaries, xz usually compresses better than bzip2 as well. The XZ page has the full table, including Zstandard and ZIP.

Levels in bzip2 mean something different

-1 to -9 set the block size (100 to 900 kB), not how hard bzip2 tries, and the manual says speed is virtually unaffected. --fast doesn't make bzip2 noticeably faster, and --best is simply the default.

Quirks

Multi-stream files and other quirks

Two bzip2 files joined with cat a.bz2 b.bz2 > both.bz2 form a valid multi-stream file, and bunzip2 decompresses both. Parallel compressors such as pbzip2 write files like that. Not every library reads past the first stream: Python's bz2 module has done so since Python 3.3, while Apache Commons Compress only does when its decompressConcatenated option is on. sample-multi-stream.bz2 tests exactly this.

  • bunzip2 deleted the .bz2 file. Like gzip, bzip2 replaces the input by default. Use -k to keep it.
  • The name came out different. bzip2 stores no name; it drops .bz2, and turns .tbz2 and .tbz into .tar.
  • (stdin) is not a bzip2 file: the data doesn't start with BZh. It may be a gzip or XZ file with the wrong extension; file name.bz2 tells you.
  • Compressed file ends unexpectedly: the file was cut short. Run bzip2recover name.bz2 to save the complete blocks into separate files.
  • Slow to unpack a big archive: lbzip2 -d decompresses on all CPU cores. pbzip2 -d does too, but only for files that pbzip2 made.

How to

How to open, create and convert a BZ2 file

  • Linux and macOS: bunzip2 file.bz2 (or bzip2 -d) to decompress, bzip2 -k file to compress and keep the original, bzcat and bzless to read without unpacking. For a .tar.bz2, tar -xf file.tar.bz2 and tar -cjf archive.tar.bz2 folder/. On a Mac, double-clicking also usually works.
  • Windows: recent Windows 11 opens .tar.bz2 and .tbz2 archives in File Explorer. For a plain .bz2, or on Windows 10, use a free tool such as 7-Zip or PeaZip.
  • Programming: Python's bz2 module, Java's Apache Commons Compress, Go's compress/bzip2 (decompression only) and libbzip2 for C.
  • Convert to XZ or gzip: bzcat file.bz2 | xz -9 > file.xz, or for a tarball, bzcat project.tar.bz2 | gzip > project.tar.gz. The tar inside doesn't change.

Security

Is a BZ2 file safe?

Decompressing runs no code from the file. The things to watch:

  • Extreme expansion. Because bzip2 run-length encodes before anything else, repetitive data shrinks dramatically. In our test, 1 GiB of zero bytes compressed to 785 bytes with bzip2 -9, a ratio of over a million to one (gzip managed about a thousand to one on the same input). Code that decompresses uploads must cap the output size, not trust the input size.
  • Old decoder bugs. bzip2 before version 1.0.7 had a memory corruption bug triggered by crafted files (CVE-2019-12900). Keep bzip2 and libraries that bundle it up to date.
  • What's inside. A .tar.bz2 brings the tar risks of ../ paths and symbolic links, explained on the TAR page, and any file can contain malware.

Free downloads

Sample BZ2 files to download

Free sample bzip2 files for testing decompressors, libraries and upload checks. Each one passes bzip2 -t and decompresses with Python's bz2 module. All were made with bzip2 1.0.8.

sample-text.txt.bz2 · 1.1 KBDownload

Browsers can't show this kind of file in a page. Download it to open it in the app it belongs to.

What's inside sample-text.txt.bz2

A UTF-8 text file compressed with bzip2 -9: one stream with one block. The anatomy above shows where each part sits.

  1. Header

    BZh9: bzip2 with 900 kB blocks. The text is only 2,742 bytes, so it fits in one block with room to spare.

  2. One block

    Starts with the π magic at byte 4 and its CRC 0x99F35A08.

  3. Bit-packed data

    After byte 14 nothing lines up with byte boundaries any more. The end marker starts at bit 8,751.

  4. No name, no date

    Unlike gzip, the file carries no original name or time stamp. Rename it and the output name follows.

  5. Ratio

    2,742 bytes compress to 1,104, about 40%. Short files compress less well than long ones: there's less repetition to find.

All sample BZ2 files

  • Text file, one block

    sample-text.txt.bz2 · 1.1 KB (1,104 bytes)

    A UTF-8 text file with accented letters, Greek, Japanese and an emoji, compressed with bzip2 -9.

    • Basic decompression tests
    • Looking at the header in a hex viewer
  • Project folder as .tar.bz2

    sample-project.tar.bz2 · 3.9 KB (3,997 bytes)

    A small project folder (README, CSV, JSON, a PNG and an executable script) in a ustar archive, compressed with bzip2 -9. The same files as the .tar.gz and .tar.xz samples.

    • Testing `.tar.bz2` extraction
    • Comparing sizes with the .tar.gz and .tar.xz versions
  • Four blocks (bzip2 -1)

    sample-multi-block.bz2 · 22.9 KB (23,401 bytes)

    A 393,212-byte log of fictional weather readings compressed with -1, so it's split into four 100 kB blocks.

    • Testing decoders across block boundaries
    • Trying `bzip2recover` and parallel decompressors
  • Two streams back to back

    sample-multi-stream.bz2 · 3.9 KB (4,029 bytes)

    Two complete bzip2 files joined together, as pbzip2 and cat produce. Decompresses to 51,467 bytes; the second half starts with a line saying it's stream 2.

    • Checking a library reads every stream
    • Testing streaming code

Ways to test with them

  • All streams read: sample-multi-stream.bz2 must decompress to 51,467 bytes. If you get about half that, your reader stops after the first stream.
  • Block boundaries: sample-multi-block.bz2 should decompress to 393,212 bytes, 4,601 lines. Compare a checksum of the output between tools.
  • Damage handling: flip a byte in the middle of sample-multi-block.bz2. bzip2 -t should report a CRC error, and bzip2recover should save the blocks that are intact.
  • Size comparison: the same project is 3,997 bytes here, 4,273 as .tar.gz and 3,760 as .tar.xz.

Free to use for testing, teaching and demos, commercially or not, with no attribution needed. A link back to this page is always appreciated.

Put it online

Have a BZ2 file? Give it a link.

Drop a .bz2 or .tar.bz2 file on 99helpers and it gets its own download link, so you can share a compressed data set or archive without attachment limits. Anyone with the link downloads the file exactly as you uploaded it.

Questions

BZ2 files, frequently asked

What is a .bz2 file?

A single file compressed with bzip2. If the name ends in .tar.bz2 or .tbz2, it's a whole folder: a tar archive compressed with bzip2.

How do I open a .bz2 file?

On Linux or macOS, run bunzip2 file.bz2, or tar -xf file.tar.bz2 for an archive. On Windows, use 7-Zip; recent Windows 11 also opens .tar.bz2 archives in File Explorer.

Is bzip2 better than gzip?

It usually makes smaller files from text, but it's slower, especially to decompress. In our test it was about seven times slower than gzip to unpack.

Is bzip2 better than xz?

On our text-heavy test data bzip2 made a slightly smaller file and compressed faster, but xz decompressed three times faster. Most projects now prefer xz or Zstandard for downloads.

What's the difference between .tar.bz2 and .tbz2?

None. .tbz2 (and .tbz, .tb2) are short forms of .tar.bz2. bunzip2 turns them into .tar files.

Can a damaged .bz2 file be recovered?

Partly. bzip2recover writes each intact block to its own small .bz2 file; only the damaged blocks, up to 900 kB of original data each, are lost.

Does bzip2 store the file name?

No. A .bz2 file holds only compressed data and checksums. The output name comes from the compressed file's name, minus .bz2.

Related formats

Other file types explained

Keep going

Related

Sources

Last reviewed 27 October 2026.