# bzip2

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

bzip2, by Julian Seward, compresses with the Burrows–Wheeler transform in independent blocks of 100–900 kB. It beats [gzip](/codecs/data/gzip/) on ratio, particularly for text, but is slow in both directions, single-threaded, and is outperformed on speed, and usually on ratio, by [xz](/codecs/data/xz/) and [zstd](/codecs/data/zstd/). Today it is mainly something you decompress: old source tarballs (`.tar.bz2`) and some datasets. For new files, use zstd or xz.

## Common commands

```sh
bzip2 -k file              # file.bz2, 900k blocks, keep the original
bzip2 -1 file              # 100k blocks: faster, less memory, worse ratio
bzip2 -d file.bz2          # decompress (or bunzip2); bzcat to stdout
bzip2 -t file.bz2          # test integrity
```

bzip2 **deletes** the input unless you pass `-k`. The `-1` … `-9` options set the block size to 100 kB … 900 kB; the default is `-9`, and smaller blocks only help when memory is limited. Compression at `-9` needs about 7.6 MB of memory and decompression about 3.7 MB (`-s` reduces it to at most 2.5 MB, at some cost in speed).

## Archives and parallelism

```sh
tar -cjf backup.tar.bz2 dir/
tar -xf backup.tar.bz2
```

The reference `bzip2` uses a single thread. Because blocks are independent, parallel implementations such as `pbzip2` and `lbzip2` can speed it up and also decompress `.bz2` files in parallel. 7-Zip can also create bzip2 with multiple threads (`7z a -tbzip2`).

## Pitfalls

- **Block independence helps recovery.** Damage to one block loses only that block, and `bzip2recover` can extract the intact ones. gzip has no equivalent.
- **Poor on binary or already-compressed data** and slower than modern alternatives; the gain over gzip rarely justifies the time.
- **`.bz2` is a single stream,** not an archive. Combine it with [tar](/codecs/data/tar/) for directories.
