---
title: "Decompress a raw LZ4 block in C#"
url: https://recursiveai.org/onearchive/cookbook/decompress-raw-lz4-block/
description: "Decode a raw LZ4 block in C# with OneCompress. Decompress into a sized buffer, use the returned byte count, and distinguish blocks from LZ4 frames."
version: "1.2.0"
---

# Decompress a raw LZ4 block 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/decompress-raw-lz4-block/decompress-raw-lz4-block.zip)

Decompress the block into a buffer you size, then read only the number of bytes the decoder returns.

```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.Lz4;
using System.Text;

byte[] block = [0x50, 72, 101, 108, 108, 111];
byte[] output = new byte[5];
int count = Lz4Block.Decompress(block, output);
Console.WriteLine(Encoding.UTF8.GetString(
    output.AsSpan(0, count)));
```

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

Prints the five decoded bytes as text:

```text
Hello
```

The example’s entire input is inline: `0x50` is the token for five literal bytes, followed by the bytes of `Hello`. The UTF-8 conversion is only for display. If the destination is too small, `Decompress` throws `CompressionLimitException`; a truncated or invalid block throws `CompressionFormatException`.

## Variations

**Allocate the result with a size limit.** Use this after defining `block` as above if you don’t have a buffer but do have an application limit:

```csharp
byte[] decoded = Lz4Block.Decompress(block,
    new Lz4BlockOptions { MaxOutputSize = 1024 });
Console.WriteLine(decoded.Length);
```

```text
5
```

**Check if the block fits a buffer.** A four-byte buffer can’t hold this example. `TryDecompress` returns `false` when the output doesn’t fit; invalid input can still throw.

```csharp
Span<byte> buffer = stackalloc byte[4];
if (Lz4Block.TryDecompress(block, buffer, out int written))
    Console.WriteLine(Encoding.UTF8.GetString(buffer[..written]));
else
    Console.WriteLine("Output buffer too small.");
```

```text
Output buffer too small.
```

## Related recipes

- [Compress and decompress LZ4 frames](https://recursiveai.org/onearchive/cookbook/lz4-compress-decompress/)

## The short answer

Use `Lz4Block.Decompress(block, destination)` from `RecursiveAI.OneCompress`. Give it a span containing exactly one raw block and a destination large enough for the decoded bytes. Read only the number of bytes it returns. For a complete LZ4 frame instead of a raw block, use `Lz4.Decompress`.

## What is the difference between an LZ4 block and an LZ4 frame?

A block holds the compressed sequences. It doesn’t have its own header that tells the reader where the block ends. If an outer format stores blocks inside a packet or a file, that format has to tell you which bytes to pass in. Including the packet header or the start of the next block changes the input to the decoder.

A frame wraps data blocks with a header, block lengths and an end marker. Flags in the header say whether fields such as a content size or checksums are present. A regular LZ4 frame starts with the magic value `0x184D2204`, written as `04 22 4D 18` in the file. See the [block and frame layouts](https://recursiveai.org/onearchive/docs/lz4-decoder/) for the distinction. Choose the decoder based on the producer’s format, not the file extension or the mere absence of those four bytes: an outer format can add its own header to a raw block.

## How big should the LZ4 decompression buffer be?

If your protocol provides the original length, check that length against a limit you’re prepared to allocate, then use it to size the destination. If exact agreement with that length matters, compare it with the returned byte count. A reusable buffer can be larger than the output; its remaining bytes aren’t part of the result. This is why the example slices the buffer before converting it to text.

The block doesn’t declare its decoded size up front. If you know the compressed byte range but not the decoded length, the allocating overload in the first variation measures the decoded length before allocating and checks it against `MaxOutputSize`. It defaults to 64 MiB; use a lower limit when your format calls for smaller messages. With the caller-supplied buffer overload, the buffer length itself sets the limit.
