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
Fromsource/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
FromXzHandler.cpp:34:
- 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):
- 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
-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
Multi-threaded Compression
Multi-threaded XZ compression can dramatically reduce compression time on multi-core systems, though it may slightly reduce compression ratio.
Handler Implementation
Fromsource/CPP/7zip/Archive/XzHandler.cpp:46-78:
Handler State
Archive Properties
FromXzHandler.cpp:196-200:
- 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)
| Level | Dict Size | Time | Size | Ratio | Memory (Comp) | Memory (Decomp) |
|---|---|---|---|---|---|---|
| -mx=0 | - | 5s | 80 MB | 80% | 3 MB | 1 MB |
| -mx=1 | 256 KB | 20s | 35 MB | 35% | 10 MB | 2 MB |
| -mx=3 | 4 MB | 35s | 28 MB | 28% | 50 MB | 6 MB |
| -mx=5 | 8 MB | 45s | 25 MB | 25% | 100 MB | 10 MB |
| -mx=7 | 16 MB | 60s | 23 MB | 23% | 200 MB | 18 MB |
| -mx=9 | 64 MB | 90s | 20 MB | 20% | 700 MB | 66 MB |
Advanced Features
Block Size Configuration
- 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:BCJ- x86 executablesARM- ARM binariesARM64- ARM64 binariesPPC- PowerPC binaries
Memory Requirements
Compression Memory
-md=8m: ~200 MB-md=32m: ~470 MB-md=64m: ~840 MB-md=128m: ~1.6 GB
Decompression Memory
- 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:Comparison with Other Formats
XZ vs GZIP
| Feature | XZ | GZIP |
|---|---|---|
| Compression ratio | Excellent | Good |
| Compression speed | Slow | Fast |
| Decompression speed | Medium | Fast |
| Memory usage | High | Low |
| Best for | Distribution, archival | Quick compression |
XZ vs BZIP2
| Feature | XZ | BZIP2 |
|---|---|---|
| Compression ratio | 15-20% better | Good |
| Compression speed | Similar | Similar |
| Decompression speed | Faster | Slower |
| Memory usage | Higher | Lower |
| Block independence | Yes | Yes |
XZ vs 7z
| Feature | XZ | 7z |
|---|---|---|
| Multiple files | No (needs TAR) | Yes |
| Solid compression | Block-based | Full solid support |
| Encryption | No | Yes (AES-256) |
| Compression ratio | Excellent | Excellent (similar) |
| Compatibility | Growing | Requires 7-Zip |
Best Practices
For Distribution
Use
-mx=9 with moderate dictionary for best compressionFor Large Files
Use multi-threading:
-mmt=4 or moreFor Archives
Combine with TAR:
.tar.xz formatFor Speed
Use
-mx=3 to -mx=5 for faster compressionCommon Use Cases
Software Distribution
System Backups
Log Archival
Database Dumps
Performance Optimization
Parallel Compression
Use multi-threading: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=64mor higher
Preset Levels
-mx=3- Fast, good compression-mx=6- Balanced (similar toxz -6)-mx=9- Maximum compression
Limitations
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