Skip to main content
XZ is a modern compression format using the LZMA2 algorithm, providing excellent compression ratios with reasonable decompression speed.

Format Overview

XZ provides:
  • Excellent compression ratio - Best general-purpose compression
  • LZMA2 algorithm - Advanced dictionary-based compression
  • Block structure - Independent blocks for parallelization
  • Integrity checking - CRC32, CRC64, SHA-256 support
  • No size limits - Files up to 16 exabytes
  • Stream padding - Multiple concatenated streams
XZ typically achieves 10-30% better compression than BZIP2 and 20-50% better than GZIP, making it ideal for distribution and archival.

Format Structure

From source/CPP/7zip/Archive/XzHandler.cpp:37-43, XZ uses a block-based structure:

Stream Format

Stream Header

The XZ magic bytes FD 37 7A 58 5A 00 make XZ files easily identifiable and less likely to be confused with other formats.

Compression Algorithm

LZMA2 Method

From XzHandler.cpp:34:
LZMA2 features:
  • Dictionary matching - Finds repeated patterns
  • Range encoding - Efficient entropy coding
  • Multi-threading support - Parallel compression
  • Better than LZMA - Reset states, better for incompressible data

Filters

XZ supports preprocessing filters (XzHandler.cpp:101):
Common filters:
  • BCJ (x86) - For x86 executables
  • BCJ (ARM, ARM64, PPC, IA64, SPARC) - For other architectures
  • Delta - For numeric data

Usage Examples

Compress Single File

Compress with Maximum Compression

Compress with Specific Dictionary Size

Dictionary sizes:
  • -md=16m - Fast, moderate compression
  • -md=32m - Good balance (default for -mx=9)
  • -md=64m - Better compression, more memory
  • -md=128m - Maximum compression, high memory

Decompress File

Create TAR.XZ Archive

Or using system tar:

Multi-threaded Compression

Uses 4 threads for faster compression.
Multi-threaded XZ compression can dramatically reduce compression time on multi-core systems, though it may slightly reduce compression ratio.

Handler Implementation

From source/CPP/7zip/Archive/XzHandler.cpp:46-78:

Handler State

Archive Properties

From XzHandler.cpp:196-200:
XZ tracks:
  • Size - Uncompressed size
  • PackSize - Compressed size
  • Method - Compression method (LZMA2)
  • NumStreams - Number of streams
  • NumBlocks - Number of blocks

Compression Levels

Performance Comparison (100 MB mixed files)

Higher compression levels require significantly more memory. Ensure your system has sufficient RAM before using -mx=9 with large dictionary sizes.

Advanced Features

Block Size Configuration

Set block size:
Block sizes:
  • Smaller blocks - Better for random access, multi-threading
  • Larger blocks - Better compression ratio

Integrity Checks

XZ supports multiple check types:
  • None - No integrity check (fastest)
  • CRC32 - Fast, good detection
  • CRC64 - Better detection
  • SHA-256 - Cryptographic hash

Stream Concatenation

Multiple XZ streams can be concatenated:

Filters for Executables

Use BCJ filter for better executable compression:
Filter types:
  • BCJ - x86 executables
  • ARM - ARM binaries
  • ARM64 - ARM64 binaries
  • PPC - PowerPC binaries

Memory Requirements

Compression Memory

Examples:
  • -md=8m: ~200 MB
  • -md=32m: ~470 MB
  • -md=64m: ~840 MB
  • -md=128m: ~1.6 GB

Decompression Memory

Examples:
  • 8 MB dict: ~18 MB
  • 32 MB dict: ~42 MB
  • 64 MB dict: ~74 MB
Decompression is much more memory-efficient than compression, allowing archives compressed with -mx=9 to be extracted on low-memory systems.

Implementation Details

XZ implementation files:
Includes C library:
Uses 7-Zip’s XZ implementation compatible with XZ Utils.

Comparison with Other Formats

XZ vs GZIP

XZ vs BZIP2

XZ vs 7z

Best Practices

For Distribution

Use -mx=9 with moderate dictionary for best compression

For Large Files

Use multi-threading: -mmt=4 or more

For Archives

Combine with TAR: .tar.xz format

For Speed

Use -mx=3 to -mx=5 for faster compression

Common Use Cases

Software Distribution

XZ is becoming the standard for Linux source distributions.

System Backups

Log Archival

Database Dumps

Performance Optimization

Parallel Compression

Use multi-threading:
Or specify thread count:

Dictionary Size Selection

Choose based on file size and available RAM:
  • File < 1 MB - Use -md=1m
  • File 1-10 MB - Use -md=8m
  • File 10-100 MB - Use -md=32m
  • File > 100 MB - Use -md=64m or higher

Preset Levels

Recommended presets:
  • -mx=3 - Fast, good compression
  • -mx=6 - Balanced (similar to xz -6)
  • -mx=9 - Maximum compression

Limitations

XZ format limitations:
  • Single file compression (use TAR for multiple files)
  • High memory requirements for compression
  • Slower than GZIP and BZIP2 for compression
  • No encryption support
  • Requires modern tools (not on very old systems)

Compatibility

Tool Support

  • xz-utils - Reference implementation (Linux)
  • 7-Zip - Full support (Windows, Linux)
  • XZ Utils - Native support (macOS via Homebrew)
  • PeaZip - GUI support
  • File Roller, Ark - Linux GUI support

Platform Support

  • Linux - Universal (xz command)
  • macOS - Via Homebrew: brew install xz
  • Windows - Via 7-Zip
  • BSD - Built-in or via packages

See Also