ZCS.XZ 5.8.3.2

dotnet add package ZCS.XZ --version 5.8.3.2
                    
NuGet\Install-Package ZCS.XZ -Version 5.8.3.2
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="ZCS.XZ" Version="5.8.3.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="ZCS.XZ" Version="5.8.3.2" />
                    
Directory.Packages.props
<PackageReference Include="ZCS.XZ" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add ZCS.XZ --version 5.8.3.2
                    
#r "nuget: ZCS.XZ, 5.8.3.2"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package ZCS.XZ@5.8.3.2
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=ZCS.XZ&version=5.8.3.2
                    
Install as a Cake Addin
#tool nuget:?package=ZCS.XZ&version=5.8.3.2
                    
Install as a Cake Tool

ZCS.XZ

License: MIT NuGet

A high-performance .NET library for XZ (LZMA2) compression and decompression, built on top of the native liblzma library via P/Invoke.

Features

  • Streaming API — standard System.IO.Stream-based compress/decompress, compatible with CopyTo, ReadAsync, pipelines, etc.
  • Zero-copy writes — the compressor pins the caller's buffer directly via unsafe fixed, eliminating intermediate copies on the write path.
  • Multi-threaded compression — optional parallel encoding via lzma_stream_encoder_mt with configurable thread count.
  • Auto-detection — the decompressor automatically handles both .xz and legacy .lzma file formats.
  • Concatenated streams — supports concatenated .xz members (e.g., files produced by xz --keep with multiple appends).
  • Cross-platform — ships native liblzma binaries for Windows, Linux, and macOS on x64 and ARM64.
  • Multi-targeting — supports .NET 8, .NET 9, and .NET 10.
  • 100 % code coverage — comprehensive test suite with full line, branch, and method coverage.

Installation

Install via the NuGet Package Manager:

dotnet add package ZCS.XZ

Or via the Package Manager Console in Visual Studio:

Install-Package ZCS.XZ

Quick Start

Compress data

using ZCS.XZ;

byte[] data = GetData();

using var output = File.Create("data.xz");
using (var xz = new XZCompressStream(output))
{
    xz.Write(data);
}
// The .xz stream is finalized when the XZCompressStream is disposed.

Decompress data

using ZCS.XZ;

using var input = File.OpenRead("data.xz");
using var xz = new XZDecompressStream(input);
using var output = new MemoryStream();
xz.CopyTo(output);

byte[] decompressed = output.ToArray();

Compress a file with options

using ZCS.XZ;

var options = new XZCompressOptions
{
    Level = XZCompressionLevel.Maximum, // Maximum compression
    Extreme = true,                     // Marginally better ratio, slower
    Threads = 0,                        // Auto-detect thread count
    BufferSize = 131072,                // 128 KB internal buffer
};

using var input = File.OpenRead("largefile.bin");
using var output = File.Create("largefile.bin.xz");
using (var xz = new XZCompressStream(output, options))
{
    input.CopyTo(xz);
}

Decompress with a memory limit

using ZCS.XZ;

ulong maxMemory = 256 * 1024 * 1024; // 256 MB

using var input = File.OpenRead("data.xz");
using var xz = new XZDecompressStream(input, memoryLimit: maxMemory, leaveOpen: false);
using var output = new MemoryStream();
xz.CopyTo(output);

API Reference

XZCompressStream

A write-only stream that compresses data and writes the .xz output to an underlying stream.

Constructor Description
XZCompressStream(Stream) Default options, disposes the inner stream on close.
XZCompressStream(Stream, XZCompressOptions) Custom options, disposes the inner stream on close.
XZCompressStream(Stream, XZCompressOptions, bool leaveOpen) Full control over options and inner stream lifetime.

Important: The stream must be disposed to finalize the .xz output (writes the stream footer). Failing to dispose produces a corrupt file.

XZDecompressStream

A read-only stream that decompresses .xz (or legacy .lzma) data from an underlying stream.

Constructor Description
XZDecompressStream(Stream) Default settings, disposes the inner stream on close.
XZDecompressStream(Stream, bool leaveOpen) Control inner stream lifetime.
XZDecompressStream(Stream, ulong memoryLimit, bool leaveOpen) Set a decoder memory limit.
XZDecompressStream(Stream, int bufferSize, bool leaveOpen) Custom internal buffer size.
XZDecompressStream(Stream, ulong memoryLimit, int bufferSize, bool leaveOpen) Full control.

XZCompressOptions

Property Type Default Description
Level XZCompressionLevel Default (6) Compression level 0–9.
Extreme bool false Enable extreme mode for marginally better compression.
Threads int 1 Thread count. 0 = auto, 1 = single-threaded, >1 = multi-threaded.
BufferSize int 81920 Internal I/O buffer size in bytes.

XZCompressionLevel

Value Level Description
None 0 No compression (store only).
Fastest 1 Fastest compression.
Level2Level5 2–5 Increasing compression ratio.
Default 6 Recommended balance of speed and ratio.
Level7Level8 7–8 Higher ratio, more CPU and memory.
Maximum 9 Highest ratio, most CPU and memory.

XZException

Thrown when liblzma returns an error. The LzmaReturnCode property contains the raw integer code (e.g., LZMA_DATA_ERROR, LZMA_MEM_ERROR).

LzmaCheck

Enum for integrity check types: None, Crc32, Crc64, Sha256.

Supported Platforms

OS Architecture Native Library
Windows x64, ARM64 liblzma.dll
Linux x64, ARM64 liblzma.so
macOS x64, ARM64 liblzma.dylib

The native liblzma binaries are bundled under the runtimes/{rid}/native/ directory and resolved automatically at runtime.

Building from Source

Prerequisites

  • .NET 10 SDK or later
  • Native liblzma binaries placed under ZCS.XZ/runtimes/{rid}/native/

Build

dotnet build

Run Tests

dotnet test

Run Tests with Code Coverage

dotnet test --collect:"XPlat Code Coverage"

To generate an HTML coverage report, install ReportGenerator:

dotnet tool install -g dotnet-reportgenerator-globaltool
reportgenerator -reports:**/coverage.cobertura.xml -targetdir:CoverageReport -reporttypes:Html

Contributing

Contributions are welcome! Please follow these steps:

  1. Fork the repository.
  2. Create a branch for your feature or bug fix: git checkout -b feature/my-feature.
  3. Write tests — aim to maintain 100 % code coverage.
  4. Build and test: dotnet build && dotnet test.
  5. Submit a pull request with a clear description of your changes.

Please open an issue first if you plan a large change, so we can discuss the approach.

License

This project is licensed under the MIT License.

Acknowledgements

Product 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • 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.

Version Downloads Last Updated
5.8.3.2 120 5/6/2026
5.8.3.1 241 4/22/2026