Recursive AI

Compress bytes to an XZ file in C#

OneArchive is a free, open-source archive and compression library for .NET from Recursive AI: zip, tar, 7z, RAR and more in RecursiveAI.OneArchive, with compression codecs in RecursiveAI.OneCompress. MIT or Apache-2.0, no native dependencies.

XZ compresses one stream of bytes, such as UTF-8 log data, into an .xz file.

Terminal
dotnet add package RecursiveAI.OneCompress

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

C#
using System.Text;
using RecursiveAI.OneCompress.Xz;
 
var line = "level=info ready\n";
var text = string.Concat(
    Enumerable.Repeat(line, 256));
var data = Encoding.UTF8.GetBytes(text);
var options = new XzCompressionOptions
    { Check = XzCheck.Crc64 };
File.WriteAllBytes("log.xz",
    Xz.Compress(data, options));
Console.WriteLine("Wrote log.xz");

Unzip the example, then dotnet run.

Writes log.xz and prints:

Text
Wrote log.xz

The full program

The downloaded program uses the same 256 lines, selects LZMA2 preset 3 and CRC-64, then reads log.xz and compares the decoded bytes with the input. The comparison uses bytes, so it will also work if you replace the log with binary data.

C#
using System.Text;
using RecursiveAI.OneCompress.Lzma;
using RecursiveAI.OneCompress.Xz;
 
var text = string.Concat(Enumerable.Repeat("level=info ready\n", 256));
byte[] data = Encoding.UTF8.GetBytes(text);
var options = new XzCompressionOptions
{
    Lzma = new LzmaOptions { Preset = 3 },
    Check = XzCheck.Crc64,
};
File.WriteAllBytes("log.xz", Xz.Compress(data, options));
 
byte[] compressed = File.ReadAllBytes("log.xz");
byte[] restored = Xz.Decompress(compressed);
if (!data.AsSpan().SequenceEqual(restored))
    throw new InvalidDataException("The restored bytes differ");
 
Console.WriteLine($"Wrote log.xz: {data.Length} -> {compressed.Length} bytes");
Console.WriteLine("Round trip: original bytes restored");
Text
Wrote log.xz: 4352 -> 112 bytes
Round trip: original bytes restored

If the saved data is truncated or its check doesn’t match, Xz.Decompress throws CompressionFormatException instead of returning the restored array.

Variations

To write a SHA-256 check instead of CRC-64, replace options in the snippet at the top:

C#
var options = new XzCompressionOptions
    { Check = XzCheck.Sha256 };

The short answer

Xz.Compress takes bytes and returns a complete XZ stream, ready to write with File.WriteAllBytes. Pass an XzCompressionOptions instance if you want to choose a preset or integrity check. To check that the file contains the bytes you meant to save, read it back, decompress it, and compare to the input with SequenceEqual.

How can I compress a file using XZ in C#?

If your input is already a file, replace data in the snippet with the file’s bytes. No text conversion is needed, even if the file is a text file. For example, given a log.txt file in your working directory:

C#
byte[] fileBytes = File.ReadAllBytes("log.txt");
File.WriteAllBytes("log.xz", Xz.Compress(fileBytes));

The one-call API takes the entire input at once and returns a new array; account for both if you use it on a large file. XZ is not an archive of named files. For several files in one .tar.xz, create a tar archive first and compress the tar bytes.

What XZ compression options should I use?

OneCompress exposes LZMA2 presets 0 through 9 through LzmaOptions.Preset. The preset and the integrity check do different jobs: the preset configures the encoder; the check lets a decoder detect corruption in the expanded block data. The default check is CRC-64. OneCompress also reads and writes CRC-32, SHA-256 and XZ streams with no block check. Keep a check unless you have a specific format requirement to disable it.

The encoder uses one block by default. If you set BlockSize, it starts a new block at that interval and restarts the LZMA2 dictionary, so the next block doesn’t reuse the previous block’s history. For more on presets, filters and decoder limits, see the XZ format options.

All onearchive recipes