Recursive AI

Compress and decompress lzip 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.

Lzip stores LZMA-compressed data in a .lz container with a checksum. The example writes a file and restores its original bytes.

Terminal
dotnet add package RecursiveAI.OneCompress

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

C#
using RecursiveAI.OneCompress.Lzip;
 
byte[] packed = Lzip.Compress("Hello!"u8);
byte[] restored = Lzip.Decompress(packed);
Console.WriteLine(
    System.Text.Encoding.UTF8.GetString(restored));
Text
Hello!

Unzip the example, then dotnet run.

The full program

The program generates message.txt: the text Hello from Lzip! followed by a newline, repeated 100 times. It writes message.txt.lz and restored.txt, then compares the bytes. It replaces these sample files on each run.

C#
using RecursiveAI.OneCompress.Lzip;
using System.Text;
 
byte[] original = Encoding.UTF8.GetBytes(
    string.Concat(Enumerable.Repeat("Hello from Lzip!\n", 100)));
File.WriteAllBytes("message.txt", original);
byte[] packed = Lzip.Compress(original);
File.WriteAllBytes("message.txt.lz", packed);
byte[] restored = Lzip.Decompress(
    File.ReadAllBytes("message.txt.lz"),
    new LzipDecompressionOptions { MaxOutputSize = 1024 * 1024 });
File.WriteAllBytes("restored.txt", restored);
Console.WriteLine($"Input: {original.Length} bytes");
Console.WriteLine($"Compressed: {packed.Length} bytes");
Console.WriteLine($"Restored: {restored.Length} bytes");
Console.WriteLine($"Bytes match: {original.SequenceEqual(restored)}");

dotnet run prints:

Text
Input: 1700 bytes
Compressed: 66 bytes
Restored: 1700 bytes
Bytes match: True

Invalid or truncated compressed data raises CompressionFormatException; exceeding a decoder limit raises CompressionLimitException.

Variations

Write three-byte members at level 0 and set both decoder limits. Each snippet runs independently.

C#
using RecursiveAI.OneCompress.Lzip;
 
byte[] packed = Lzip.Compress("Hello!"u8,
    new LzipOptions { Level = 0, MemberSize = 3 });
byte[] restored = Lzip.Decompress(packed,
    new LzipDecompressionOptions
    {
        MaxDictionarySize = 64 * 1024 * 1024,
        MaxOutputSize = 1024
    });
Console.WriteLine(System.Text.Encoding.UTF8.GetString(restored));
Text
Hello!

Use streams without putting the complete compressed output in a byte array. Dispose the encoder before reading its output.

C#
using RecursiveAI.OneCompress.Lzip;
 
using var packed = new MemoryStream();
using (var encoder = new LzipEncoderStream(packed, leaveOpen: true))
    encoder.Write("Hello!"u8);
packed.Position = 0;
using var decoder = new LzipStream(packed);
using var reader = new StreamReader(decoder);
Console.WriteLine(reader.ReadToEnd());
Text
Hello!

The short answer

For lzip compression and decompression in C#, use the RecursiveAI.OneCompress NuGet package. Lzip.Compress produces the .lz container and Lzip.Decompress restores it. Both operate on byte buffers. The full program writes message.txt.lz, reads that file back and compares the restored bytes with the original. You do not need to launch an external compression command.

Lzip and xz are separate containers. Use the lzip types for .lz data, not the xz decoder and not a ZIP reader. OneCompress has separate APIs for those formats. A filename extension is useful for choosing the API, but renaming another compressed format to .lz does not convert it.

How do I compress files with lzip in C#?

Read the source file into bytes and pass them to Lzip.Compress, then save the result. The full program shows both file operations, using text it generates itself. For a real file, replace the generated original array with File.ReadAllBytes for your path. The decompression call can stay the same, but change its output cap to the largest file your application accepts.

The lzip options use levels 0 through 9, defaulting to 6. A level selects encoder settings; it does not request a final size or a compression percentage. The example’s byte count is for its repeated text only. Already-compressed or very short input may not shrink.

For stream-based writing, use LzipEncoderStream, as in the second variation. Dispose the encoder before reading or publishing the compressed file: disposal completes its output. For reading, use LzipStream. These are different types with different jobs. The reader takes LzipDecompressionOptions; the encoder takes LzipOptions. leaveOpen: true lets a surrounding scope keep ownership of the underlying stream.

What is lzip?

Lzip is a container around LZMA data. A member has a trailer with CRC-32, data size and member size. OneCompress verifies those trailer fields when decoding. The format documentation describes its container handling, defaults and supported members.

A file can contain several members in sequence. By default the encoder writes one member. Set MemberSize to cut input into members at that many uncompressed bytes; the first variation uses three-byte members solely to make this behavior visible with a six-byte input. The decoder accepts multiple members and joins their restored output. Member boundaries do not become file entries or filenames.

Set MaxOutputSize to bound the total restored output and MaxDictionarySize to bound the dictionary a member declares. The defaults are 1 GiB and 64 MiB respectively. These are separate controls: a small dictionary does not imply a small output. The format permits dictionaries from 4 KiB through 512 MiB, but the default decoder cap intentionally accepts less than that upper end. Raise a limit only when your application expects such input and can afford the memory. Version 0 members are rejected as unsupported. Invalid trailers, truncation and non-member trailing bytes are errors by default; the example does not turn off checksum checking.

All onearchive recipes