loading
Note:
When to reach for this over GZipStream .NET already has GZipStream, and on Blazor Server that is the better tool. This earns its place on WebAssembly: the browser's implementation is native rather than interpreted, and not pulling the managed compression code into the published bundle is worth something on its own.

Support check

IsSupported

Returns true when the runtime exposes CompressionStream. During prerender/SSR the check returns false rather than throwing, so defer it to OnAfterRenderAsync.

C#
@inject Bit.Butil.Compression compression

var supported = await compression.IsSupported();
Live sample
support check output
Results will appear here when you interact with the samples.

Round-trip some text

CompressText / DecompressText

The convenience pair for the common case: shrink a JSON payload before putting it in LocalStorage or IndexedDb, and expand it on the way back out. Text is encoded as UTF-8 in both directions. A corrupt payload comes back as null rather than throwing - bad data from storage or the network is a normal outcome, not an exceptional one.

C#
var packed = await compression.CompressText(json);              // byte[]?
var original = await compression.DecompressText(packed!);       // string?

// pick a codec explicitly:
var raw = await compression.CompressText(json, CompressionFormat.DeflateRaw);
Live sample
round-trip output
Results will appear here when you interact with the samples.

Bytes, and choosing a codec

Compress / Decompress / CompressionFormat

The byte overloads take whatever you have. Which codec to pick depends on what is at the other end: Gzip is the interoperable default and matches .NET's GZipStream. Deflate is the zlib-wrapped variant that matches ZLibStream - and note that .NET's DeflateStream is actually the raw variant, so pair that one with DeflateRaw.

C#
var packed = await compression.Compress(bytes, CompressionFormat.Gzip);
var original = await compression.Decompress(packed!, CompressionFormat.Gzip);

// matching .NET on the server:
//   GZipStream    <-> CompressionFormat.Gzip
//   ZLibStream    <-> CompressionFormat.Deflate
//   DeflateStream <-> CompressionFormat.DeflateRaw
Live sample
bytes output
Results will appear here when you interact with the samples.

API reference

Member
Signature
Description
IsSupported
ValueTask<bool> IsSupported()
True when the runtime exposes CompressionStream. Returns default (false) during prerender/SSR instead of throwing.
Compress
ValueTask<byte[]?> Compress(byte[] data, CompressionFormat format = CompressionFormat.Gzip)
Compresses bytes. Null when the API is unavailable or the stream errored.
Decompress
ValueTask<byte[]?> Decompress(byte[] data, CompressionFormat format = CompressionFormat.Gzip)
Decompresses bytes. Null when the input is corrupt or the format doesn't match - nothing is thrown.
CompressText
ValueTask<byte[]?> CompressText(string text, CompressionFormat format = CompressionFormat.Gzip)
Compresses text as UTF-8.
DecompressText
ValueTask<string?> DecompressText(byte[] data, CompressionFormat format = CompressionFormat.Gzip)
Decompresses bytes back into UTF-8 text.
CompressionFormat
Gzip | Deflate | DeflateRaw
Gzip matches .NET's GZipStream, Deflate matches ZLibStream, and DeflateRaw matches DeflateStream.
An unhandled error has occurred. Reload 🗙