---
title: "Compress bytes to an XZ file in C#"
url: https://recursiveai.org/onearchive/cookbook/compress-bytes-to-xz/
description: "Compress UTF-8 bytes to an .xz file in C# with OneCompress. Set the LZMA2 preset and CRC-64 check, then decompress and compare the bytes."
version: "1.2.0"
---

# 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.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneCompress) · [GitHub](https://github.com/recursive-ai-oss/onearchive-csharp) · [Docs](https://recursiveai.org/onearchive/docs/) · [Download example (.zip)](https://recursiveai.org/onearchive/cookbook/compress-bytes-to-xz/compress-bytes-to-xz.zip)

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

```shell
dotnet add package RecursiveAI.OneCompress
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
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](https://recursiveai.org/onearchive/cookbook/compress-bytes-to-xz/compress-bytes-to-xz.zip), 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.

```csharp
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:

```csharp
var options = new XzCompressionOptions
    { Check = XzCheck.Sha256 };
```

## Related recipes

- [Decompress an XZ file in C#](https://recursiveai.org/onearchive/cookbook/decompress-xz-file/)
- [Create a tar archive in C#](https://recursiveai.org/onearchive/cookbook/create-tar-archive/)

<!-- article -->

## 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:

```csharp
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](https://recursiveai.org/onearchive/docs/xz/).
