Lzma.Net
5.8.3.2
dotnet add package Lzma.Net --version 5.8.3.2
NuGet\Install-Package Lzma.Net -Version 5.8.3.2
<PackageReference Include="Lzma.Net" Version="5.8.3.2" />
<PackageVersion Include="Lzma.Net" Version="5.8.3.2" />
<PackageReference Include="Lzma.Net" />
paket add Lzma.Net --version 5.8.3.2
#r "nuget: Lzma.Net, 5.8.3.2"
#:package Lzma.Net@5.8.3.2
#addin nuget:?package=Lzma.Net&version=5.8.3.2
#tool nuget:?package=Lzma.Net&version=5.8.3.2
Lzma.Net
A native C# implementation of the XZ/LZMA2/LZMA compression format. No native binaries, no P/Invoke, no liblzma dependency — just pure managed C# code that runs anywhere .NET runs.
Features
- 100% managed C# — zero native dependencies, works on any platform .NET supports
- XZ format — full read/write support for the
.xzcontainer format (spec) - LZMA2 codec — chunked LZMA compression with automatic dictionary resets
- Streaming API —
XzCompressStreamandXzDecompressStreamfor processing data without loading it all into memory - One-shot API —
XzCompressor.Compress/Decompressfor simple byte-array operations - Async API —
CompressAsync/DecompressAsyncand async stream methods for non-blocking I/O - Multi-threaded compression — parallel block compression via the
Threadsoption - Presets 0–9 — matching
xzCLI compression levels and dictionary sizes - Extreme mode — equivalent to
xz -e, spends more CPU time for better compression - Integrity checks — CRC32, CRC64, SHA-256, or no check
- Concatenated streams — reads multiple XZ streams appended back-to-back
- Zero-copy design — uses
Span<T>,ReadOnlySpan<T>,ArrayPool<T>, andstackallocthroughout - .NET 8 / 9 / 10 — multi-target support
Installation
dotnet add package LzmaNet
Quick Start
Compress and decompress a byte array
using LzmaNet;
byte[] original = File.ReadAllBytes("data.bin");
// Compress with default settings (preset 6, CRC64)
byte[] compressed = XzCompressor.Compress(original);
// Decompress
byte[] restored = XzCompressor.Decompress(compressed);
Compress with options
using LzmaNet;
var options = new XzCompressOptions
{
Preset = 9, // Maximum compression
Extreme = true, // Spend more CPU for slightly better ratio
Threads = 0, // Use all available CPUs
};
byte[] compressed = XzCompressor.Compress(data, options);
Stream API — compress a file
using LzmaNet;
using var input = File.OpenRead("data.bin");
using var output = File.Create("data.xz");
using (var xz = new XzCompressStream(output))
{
input.CopyTo(xz);
}
Stream API — decompress a file
using LzmaNet;
using var input = File.OpenRead("data.xz");
using var output = File.Create("data.bin");
using var xz = new XzDecompressStream(input);
xz.CopyTo(output);
Async — compress and decompress
using LzmaNet;
// One-shot async
byte[] compressed = await XzCompressor.CompressAsync(data);
byte[] restored = await XzCompressor.DecompressAsync(compressed);
Async — stream API
using LzmaNet;
using var input = File.OpenRead("data.bin");
using var output = File.Create("data.xz");
await using (var xz = new XzCompressStream(output))
{
await input.CopyToAsync(xz);
}
ASP.NET — decompress an upload on the fly
app.MapPost("/upload", async (HttpRequest request) =>
{
using var xz = new XzDecompressStream(request.Body);
using var output = File.Create("uploaded.bin");
await xz.CopyToAsync(output);
});
Compression Options
All tuning knobs are exposed through the XzCompressOptions class:
| Property | Type | Default | Description |
|---|---|---|---|
Preset |
int |
6 |
Compression level 0–9. Higher = smaller output, more CPU/memory. |
Extreme |
bool |
false |
When true, spends significantly more CPU to improve ratio. Equivalent to xz -e. |
Threads |
int |
1 |
0 = all CPUs, 1 = single-threaded, N = N threads. |
CheckType |
XzCheckType |
Crc64 |
Integrity check: None, Crc32, Crc64, or Sha256. |
DictionarySize |
int? |
null |
Override the preset's dictionary size (bytes, min 4 KB). |
BlockSize |
int? |
null |
XZ block size (bytes, min 4 KB). null = max(dict×2, 1 MB). |
Preset dictionary sizes
| Preset | Dictionary Size |
|---|---|
| 0 | 64 KB |
| 1 | 1 MB |
| 2 | 2 MB |
| 3–4 | 4 MB |
| 5–6 | 8 MB |
| 7 | 16 MB |
| 8 | 32 MB |
| 9 | 64 MB |
Benchmarks
See BENCHMARK.md for detailed performance comparisons against native liblzma.
Quick summary (16 MB data, preset 6, single-threaded):
| Compress | Decompress | |
|---|---|---|
| LzmaNet (pure C#) | 10.9 MB/s | 40.7 MB/s |
| liblzma (native C) | 8.9 MB/s | 64.5 MB/s |
LzmaNet is faster at compression; liblzma is faster at decompression due to decades of hand-optimized C in the range decoder.
Interoperability
Output is fully compatible with the standard xz tool and any other XZ-compliant decoder. The test suite validates round-trip interoperability with the xz CLI in both directions.
Architecture
LzmaNet is structured as a set of layered codecs, all implemented in pure C#:
XzCompressor / XzCompressStream / XzDecompressStream (public API)
└─ XZ container format (header, block, index, footer)
└─ LZMA2 codec (chunked wrapper over LZMA)
└─ LZMA codec (LZ77 + adaptive range coding)
├─ HC4 match finder (hash-chain, 4-byte hashing)
├─ Range encoder / decoder
└─ CRC32 / CRC64 integrity checks
Acknowledgments
This implementation is based on the algorithms and file format from XZ Utils 5.8.3, originally created by Lasse Collin and maintained by the Tukaani Project. The BCJ filter implementations are ported from the liblzma C source.
Special thanks to:
- Lasse Collin — original author of liblzma and the XZ file format
- Jia Tan and other contributors to the Tukaani Project
- Igor Pavlov — creator of the LZMA algorithm and 7-Zip
License
0BSD — free for any use, no attribution required.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net10.0
- No dependencies.
-
net8.0
- No dependencies.
-
net9.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Initial release. Pure C# port of XZ Utils 5.8.3 with LZMA/LZMA2/XZ compression, BCJ/Delta filters, multi-threaded compression, and streaming APIs.