Compressed streams
editXzStream, LzmaStream, LZipStream and BZip2Stream work like GZipStream: create them with CompressionMode.Decompress to read, or with a CompressionLevel or their options to write, and dispose them to finish the compressed data.
XZ
var data = "Hello, XZ! Hello, XZ! Hello, XZ!"u8.ToArray();
using var compressed = new MemoryStream();
using (var xz = new XzStream(compressed, new XzCompressionOptions { Preset = 9, Check = XzCheck.Sha256 }, leaveOpen: true))
{
xz.Write(data);
}
compressed.Position = 0;
using var decompress = new XzStream(compressed, CompressionMode.Decompress);
using var result = new MemoryStream();
decompress.CopyTo(result);
Assert.Equal(data, result.ToArray());XzCompressionOptions sets the preset (0 to 9, as xz -0 to xz -9), the dictionary size, the integrity check (XzCheck: CRC-64 by default) and the size of the blocks. Decompressing reads the files of several streams (cat a.xz b.xz), checks the integrity checks, and applies the filters of the file (x86, ARM, ARM64, RISC-V, Delta, ...).
bzip2
var data = "Hello, bzip2! Hello, bzip2!"u8.ToArray();
using var compressed = new MemoryStream();
using (var bzip2 = new BZip2Stream(compressed, new BZip2CompressionOptions { BlockSize = 9 }, leaveOpen: true))
{
bzip2.Write(data);
}
compressed.Position = 0;
using var decompress = new BZip2Stream(compressed, CompressionMode.Decompress);
using var result = new MemoryStream();
decompress.CopyTo(result);
Assert.Equal(data, result.ToArray());The block size of BZip2CompressionOptions is 1 to 9, as bzip2 -1 to bzip2 -9. Decompressing reads the files of several streams, such as the files of pbzip2.
LZMA and lzip
LzmaStream reads and writes the .lzma files of the LZMA SDK and of xz --format=lzma, and LZipStream the .lz files of lzip. Their options are LzmaCompressionOptions and LZipCompressionOptions.
Unix compress
UnixCompressStream decompresses the .Z files of the compress command. It cannot compress.