Skip to main content
BZIP2 is a high-quality compression format using the Burrows-Wheeler transform algorithm, providing better compression than GZIP at the cost of speed.

Format Overview

BZIP2 provides:
  • High compression ratio - 10-15% better than GZIP
  • Block-based compression - Independent 100-900 KB blocks
  • Burrows-Wheeler algorithm - Advanced text transformation
  • Error recovery - Block independence aids recovery
  • No file size limit - Unlimited file sizes
  • Open source - Free implementation (libbzip2)
BZIP2 excels at compressing text and source code, often achieving 10-15% better compression than GZIP while using similar memory.

Format Structure

From source/CPP/7zip/Archive/Bz2Handler.cpp:107-121, BZIP2 files have this structure:

File Header

Block Size

The fourth byte indicates block size:
Larger block sizes generally provide better compression but require more memory. The default is usually ‘9’ (900 KB blocks).

Compression Algorithm

BZIP2 uses a multi-stage compression pipeline:
  1. Run-Length Encoding (RLE) - First pass
  2. Burrows-Wheeler Transform (BWT) - Sorts data for better compression
  3. Move-To-Front (MTF) - Transform to improve locality
  4. Run-Length Encoding - Second pass
  5. Huffman Coding - Final entropy encoding
This sophisticated pipeline achieves excellent compression ratios.

Usage Examples

Compress Single File

Compress with Specific Block Size

Decompress File

Create TAR.BZ2 Archive

Or using system tar:

Test Archive Integrity

List Archive Information

Shows compressed and uncompressed sizes.

Handler Implementation

From source/CPP/7zip/Archive/Bz2Handler.cpp:23-47:

Archive Properties

BZIP2 handler tracks several properties (Bz2Handler.cpp:49-82):

Archive-Level Properties

  • kpidPhySize - Compressed size
  • kpidUnpackSize - Uncompressed size
  • kpidNumStreams - Number of BZIP2 streams
  • kpidNumBlocks - Number of compression blocks
  • kpidErrorFlags - Error indicators

Item Properties

  • kpidPackSize - Compressed size
  • kpidSize - Uncompressed size

Block Independence

BZIP2’s block structure enables:

Parallel Decompression

Blocks can be decompressed independently:

Error Recovery

If one block is corrupted, other blocks remain intact:

Seeking

Blocks allow seeking within compressed files without full decompression.

Compression Performance

Compression Levels

Sample Performance (100 MB text file)

BZIP2 provides a good balance between GZIP’s speed and XZ’s compression ratio.

Advanced Usage

Compress from Standard Input

Decompress to Standard Output

Multiple Streams

BZIP2 supports concatenated streams:

Recovery Archives

Implementation Details

BZIP2 compression codec:
Archive handler:
Key components:

Comparison with Other Formats

BZIP2 vs GZIP

Advantages:
  • 10-15% better compression
  • Block structure enables recovery
  • Better for text files
Disadvantages:
  • Slower compression (2-3x)
  • Slower decompression (2x)
  • Higher memory usage

BZIP2 vs XZ

Advantages:
  • Faster compression and decompression
  • Lower memory requirements
  • Block independence
Disadvantages:
  • Lower compression ratio (15-20% larger)
  • Less efficient for binary data

BZIP2 vs 7z/LZMA

Advantages:
  • Simpler format
  • Better tool support
  • Faster decompression
Disadvantages:
  • Lower compression ratio
  • Single file only (needs TAR)
  • No encryption

Best Practices

For Text Files

Use -mx=9 for excellent compression of source code and documents

For Log Archives

Combine with TAR for compressed log archives

For Balance

Use BZIP2 when GZIP is too weak and XZ is too slow

For Recovery

Use block structure for better error recovery

Common Use Cases

Source Code Distribution

Log Compression

Database Dumps

System Backup

Memory Requirements

Compression

For -mx=9 (900KB blocks):

Decompression

For 900KB blocks:
BZIP2 uses less memory than LZMA/XZ but more than GZIP, making it suitable for resource-constrained environments.

Error Handling

From Bz2Handler.cpp:73-81:
Error detection:
  • IsNotArc - Invalid BZIP2 signature
  • UnexpectedEnd - Truncated file
  • DataAfterEnd - Extra data after stream

Limitations

BZIP2 limitations:
  • Single file compression (use TAR for multiple files)
  • No encryption support
  • Slower than GZIP
  • Higher memory usage than GZIP
  • No solid compression

Compatibility

Universal Support

BZIP2 is widely supported:
  • Linux/Unix - bzip2, bunzip2 commands
  • macOS - Built-in bzip2
  • Windows - 7-Zip, WinZip, various tools
  • Programming - libbzip2 library in many languages

Archive Tools

  • 7-Zip - Full support
  • bzip2 - Reference implementation
  • pbzip2 - Parallel implementation
  • lbzip2 - Multi-threaded implementation
  • WinZip - Windows support

Performance Optimization

Parallel Compression

Use parallel implementations:

Choosing Block Size

  • Small files (less than 1 MB) - Use -mx=1 or -mx=3
  • Medium files (1-10 MB) - Use -mx=5
  • Large files (greater than 10 MB) - Use -mx=9

Memory-Constrained Systems

See Also