---
title: "Compress and decompress Deflate64 in C#"
url: https://recursiveai.org/onearchive/cookbook/compress-decompress-deflate64/
description: "Compress and decompress raw Deflate64 bytes in C# with OneCompress, set an output limit, and check a complete round trip."
version: "1.2.0"
---

# Compress and decompress Deflate64 in C#

**OneArchive** is a free, open-source archive and compression library for .NET from Recursive AI: archives (zip, tar, 7z, RAR 5, cpio, ar, ISO) in RecursiveAI.OneArchive, codecs (zlib, gzip, xz, Zstandard, Brotli) 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-decompress-deflate64/compress-decompress-deflate64.zip)

OneCompress, OneArchive’s codec package, compresses raw Deflate64 bytes—not ZIP files.

```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 RecursiveAI.OneCompress.Deflate64;

byte[] input = "Hello Deflate64!"u8.ToArray();
byte[] packed = Deflate64.Compress(input);
byte[] restored = Deflate64.Decompress(packed,
    new() { MaxOutputSize = 1024 });
Console.WriteLine(
    System.Text.Encoding.UTF8.GetString(restored));
```

```text
Hello Deflate64!
```

Unzip [the example](https://recursiveai.org/onearchive/cookbook/compress-decompress-deflate64/compress-decompress-deflate64.zip), then `dotnet run`.

## The full program

The program generates `Hello Deflate64!` as UTF-8 bytes, checks the round trip and saves the compressed bytes to `message.deflate64`.

```csharp
using RecursiveAI.OneCompress.Deflate64;

byte[] input = "Hello Deflate64!"u8.ToArray();
byte[] packed = Deflate64.Compress(input);
byte[] restored = Deflate64.Decompress(packed,
    new() { MaxOutputSize = 1024 });
Console.WriteLine(
    System.Text.Encoding.UTF8.GetString(restored));
Console.WriteLine($"Round-trip equal: {input.SequenceEqual(restored)}");
File.WriteAllBytes("message.deflate64", packed);
Console.WriteLine("Wrote message.deflate64");
```

`dotnet run` prints:

```text
Hello Deflate64!
Round-trip equal: True
Wrote message.deflate64
```

Malformed or truncated input throws `CompressionFormatException`; output above `MaxOutputSize` throws `CompressionLimitException`.

## Variations

Choose level 9 and reject decompression that would exceed four bytes.

```csharp
using RecursiveAI.OneCompress;
using RecursiveAI.OneCompress.Deflate64;

byte[] packed = Deflate64.Compress(
    "Hello Deflate64!"u8,
    new() { Level = 9 });
try
{
    Deflate64.Decompress(packed,
        new() { MaxOutputSize = 4 });
}
catch (CompressionLimitException)
{
    Console.WriteLine("Output exceeds 4 bytes");
}
```

```text
Output exceeds 4 bytes
```

## Related recipes

- [Create an ar archive in C#](https://recursiveai.org/onearchive/cookbook/create-ar-archive/)
- [Create a cpio archive in C#](https://recursiveai.org/onearchive/cookbook/create-cpio-archive/)
- [List RAR 5 files and extract a stored entry in C#](https://recursiveai.org/onearchive/cookbook/list-rar5-and-extract-stored-entry/)

## The short answer

Call `Deflate64.Compress` and `Deflate64.Decompress` from RecursiveAI.OneCompress. Set `MaxOutputSize` when decoding so that the compressed input cannot request unlimited output.

These methods accept and return byte arrays. There are no filenames, archive entries or text encodings stored by the raw codec. UTF-8 belongs to this example’s application data, not to Deflate64 itself. Arbitrary binary data can be passed through the same methods without a text conversion.

## How do I compress and decompress Deflate64 in C#?

Install RecursiveAI.OneCompress rather than adding an archive container around the data. The namespace is `RecursiveAI.OneCompress.Deflate64`; `Deflate64` is the one-call API. Its compression options accept levels 0 through 9, with 6 as the default. Level 0 uses stored blocks. The variation above selects level 9 and demonstrates a deliberately small decompression ceiling.

The [encoder documentation](https://recursiveai.org/onearchive/docs/deflate64-encoder/) explains the level choices. A higher level is not permission to omit the decode limit: encoder settings and decompression resource limits solve different problems. The full program checks equality of the restored bytes, not the file extension or whether the decoded bytes happen to be valid text.

`Decompress` returns the whole decoded byte array. Its output limit defaults to 1 GiB, but an application expecting a small message should set a smaller ceiling. Here the short snippet uses 1024 bytes for a 16-byte message. For larger streaming workflows, the package also exposes incremental encoder and decoder APIs; the one-call example is appropriate when retaining both input and output in memory is acceptable.

## Is Deflate64 the same as Deflate in a ZIP file?

No. The [decoder documentation](https://recursiveai.org/onearchive/docs/deflate64-decoder/) identifies raw Deflate64 as the data used by ZIP method 9. Deflate64 changes Deflate’s length and distance codes and uses a larger history window; the formats aren’t interchangeable.

There is no ZIP header, directory or checksum around the output of `Deflate64.Compress`. Renaming `message.deflate64` to `.zip` would not create an archive. In a ZIP container, the ZIP layer supplies metadata and checks CRC-32; the raw codec has no checksum of its own. The program’s byte comparison confirms this particular round trip but does not add a checksum to the saved stream.

The decoder reads one stream. By default the one-call API refuses bytes after its end, as well as malformed or truncated input. `AllowTrailingData` changes the trailing-byte policy, not the compression format. Keep the strict default for a file expected to contain exactly one raw stream; if your application stores several messages together, define their framing explicitly rather than assuming a filename supplies boundaries.
