CommonNetFuncs.Compression 5.0.0

dotnet add package CommonNetFuncs.Compression --version 5.0.0
                    
NuGet\Install-Package CommonNetFuncs.Compression -Version 5.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="CommonNetFuncs.Compression" Version="5.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="CommonNetFuncs.Compression" Version="5.0.0" />
                    
Directory.Packages.props
<PackageReference Include="CommonNetFuncs.Compression" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add CommonNetFuncs.Compression --version 5.0.0
                    
#r "nuget: CommonNetFuncs.Compression, 5.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package CommonNetFuncs.Compression@5.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=CommonNetFuncs.Compression&version=5.0.0
                    
Install as a Cake Addin
#tool nuget:?package=CommonNetFuncs.Compression&version=5.0.0
                    
Install as a Cake Tool

CommonNetFuncs.Compression

License NuGet Version NuGet downloads

Compression/decompression helpers for byte arrays, streams, files, and ZIP archives. The package targets net11.0 and netstandard2.1.

Supported stream/byte algorithms are Brotli, GZip, Deflate, and ZLib. ECompressionType.None is a sentinel and causes the compression/decompression stream factories to throw NotImplementedException. ZIP creation is a separate archive operation.

Contents

Installation

dotnet add package CommonNetFuncs.Compression
using System.IO.Compression;
using CommonNetFuncs.Compression;

Stream and byte-array API

The Streams static class supplies the following extensions. Compression and stream-to-stream methods default to CompressionLevel.Optimal; asynchronous methods accept cancellation.

byte[] input = "hello"u8.ToArray();
using var source = new MemoryStream(input);
using var compressed = new MemoryStream();

// Each stream-to-stream overload is shown explicitly.
source.CompressStream(compressed, Streams.ECompressionType.Gzip);
using var sourceForAsync = new MemoryStream(input);
using var compressedForAsync = new MemoryStream();
await sourceForAsync.CompressStreamAsync(
    compressedForAsync,
    Streams.ECompressionType.Gzip,
    CompressionLevel.SmallestSize,
    cancellationToken);

// Decompression rewinds a seekable compressed input, writes at the output's
// current position, flushes, and rewinds a seekable output. It leaves both
// caller-owned streams open.
using var restored = new MemoryStream();
compressed.DecompressStream(restored, Streams.ECompressionType.Gzip);
using var restoredAsync = new MemoryStream();
await compressedForAsync.DecompressStreamAsync(
    restoredAsync,
    Streams.ECompressionType.Gzip,
    cancellationToken);

The stream-wrapper overloads are write-only compression wrappers or read-only decompression wrappers, not streams containing eagerly copied data. They must be disposed to finish a compression stream. leaveOpen defaults to false, so disposing the wrapper normally disposes the underlying stream. For a compression wrapper, the receiver is the underlying destination; the implementation additionally requires that stream to report CanRead, so a write-only stream is rejected. Wrapper-returning decompression has no expansion-ratio limit.

using var writableTarget = new MemoryStream();
using (Stream gzipWriter = writableTarget.Compress(
    Streams.ECompressionType.Gzip,
    CompressionLevel.Optimal,
    leaveOpen: true))
{
    gzipWriter.Write(input);
}

writableTarget.Position = 0;
using (Stream gzipReader = writableTarget.Decompress(
    Streams.ECompressionType.Gzip,
    leaveOpen: true))
{
    using var result = new MemoryStream();
    gzipReader.CopyTo(result);
}

Both byte-array overloads are demonstrated below.

byte[] compressedSync = input.Compress(
    Streams.ECompressionType.ZLib,
    CompressionLevel.SmallestSize);
byte[] compressedAsync = await input.CompressAsync(
    Streams.ECompressionType.ZLib,
    CompressionLevel.SmallestSize,
    cancellationToken);

byte[] plainSync = compressedSync.Decompress(
    Streams.ECompressionType.ZLib,
    maxCompressionRatio: 100,
    cancellationToken);
byte[] plainAsync = await compressedAsync.DecompressAsync(
    Streams.ECompressionType.ZLib,
    maxCompressionRatio: 100,
    cancellationToken);

Byte-array decompression defaults to a maximum output of 1,000 times the compressed length and throws CompressionLimitExceededException when exceeded. maxCompressionRatio is not validated as positive by the implementation; pass a positive value. For untrusted data, also impose an absolute incoming/output size appropriate to the application.

Stream-to-stream decompression uses the same 1,000x ratio only when the compressed stream is seekable. For a non-seekable compressed stream the implementation uses long.MaxValue, so that path is not an effective decompression-bomb limit.

Explicit bounded copying

using var boundedTarget = new MemoryStream();
source.Position = 0;
source.CopyWithLimit(
    boundedTarget,
    maxBytes: 1_000_000,
    cancellationToken);

using var boundedTargetAsync = new MemoryStream();
sourceForAsync.Position = 0;
await sourceForAsync.CopyWithLimitAsync(
    boundedTargetAsync,
    maxBytes: 1_000_000,
    cancellationToken);

The methods copy from the current positions and leave streams open. If a read would make the copied total exceed maxBytes, they throw CompressionLimitExceededException. Because the sync method reads a complete chunk before testing, the destination can receive at most the bytes from earlier chunks, not a partial rejected chunk.

Detection

byte[] gzip = input.Compress(Streams.ECompressionType.Gzip);
using var gzipInput = new MemoryStream(gzip);
Streams.ECompressionType detected =
    await gzipInput.DetectCompressionType();

bool gzipBytes = gzip.IsGzipCompressed();
bool gzipStream = new MemoryStream(gzip).IsGzipCompressed();
byte[] zlib = input.Compress(Streams.ECompressionType.ZLib);
bool zlibBytes = zlib.IsZLibCompressed();
bool zlibStream = new MemoryStream(zlib).IsZLibCompressed();

// Brotli has no reliable fixed signature in normal framework output.
// These methods only recognize CE B2 CF 81 by convention, so use
// them only with data that promises that marker.
bool brotliBytes = new byte[] { 0xCE, 0xB2, 0xCF, 0x81 }.IsBrotliCompressed();
bool brotliStream = new MemoryStream(
    new byte[] { 0xCE, 0xB2, 0xCF, 0x81 }).IsBrotliCompressed();

bool deflate = await input.Compress(Streams.ECompressionType.Deflate)
    .IsDeflateCompressed();

Header checks return false for null/unreadable inputs. Seekable stream checks inspect from the current position and restore it. Non-seekable IsGzipCompressed, IsZLibCompressed, and IsBrotliCompressed checks consume bytes. IsDeflateCompressed attempts to read one decompressed byte; it is not a full-stream integrity check. DetectCompressionType consumes four bytes from a non-seekable stream and cannot recover them. Detection recognizes Deflate only by attempting decompression on a seekable stream/sample; DetectCompressionType returns None for Deflate on a non-seekable input.

Use DetectCompressionTypeAndReset when a non-seekable input must remain readable:

(Streams.ECompressionType type, Stream replayable) =
    await Streams.DetectCompressionTypeAndReset(networkStream);

using (replayable)
using (Stream reader = replayable.Decompress(type, leaveOpen: false))
{
    // Consume reader from the beginning, including the eight-byte header
    // already read by the detector.
}

For a seekable input, it returns the original stream and restores its original position. For a non-seekable input, it returns a public ConcatenatedStream that reads the consumed header followed by the original stream.

ConcatenatedStream

ConcatenatedStream is public, non-seekable, and read-only. Its constructor accepts two Stream instances and its synchronous/span/asynchronous Read operations read from the first and then the second. Flush is a no-op; Length, Position, Seek, SetLength, and Write throw NotSupportedException. Disposing it does not dispose either input stream because no ownership override is supplied.

using Stream first = new MemoryStream([1, 2]);
using Stream second = new MemoryStream([3, 4]);
using var concatenated = new ConcatenatedStream(first, second);
byte[] output1 = new byte[2];
int count1 = concatenated.Read(output1, 0, output1.Length);
int count2;
{
    Span<byte> output2 = stackalloc byte[2];
    count2 = concatenated.Read(output2);
}
byte[] output3 = new byte[4];
int count3 = await concatenated.ReadAsync(
    output3, 0, output3.Length, cancellationToken);
byte[] output4 = new byte[4];
int count4 = await concatenated.ReadAsync(
    output4.AsMemory(), cancellationToken);
concatenated.Flush();
bool canRead = concatenated.CanRead;
bool canSeek = concatenated.CanSeek;
bool canWrite = concatenated.CanWrite;

// The unsupported members throw NotSupportedException.
_ = () => concatenated.Length;
_ = () => concatenated.Position;
_ = () => concatenated.Position = 0;
_ = () => concatenated.Seek(0, SeekOrigin.Begin);
_ = () => concatenated.SetLength(0);
_ = () => concatenated.Write([0], 0, 1);

Limit exception

All three public constructors of CompressionLimitExceededException are available:

Exception inner = new InvalidDataException();
CompressionLimitExceededException a = new();
CompressionLimitExceededException b = new("limit exceeded");
CompressionLimitExceededException c = new("limit exceeded", inner);

File and ZIP API

ZIP helpers stream data into ZipArchive; they do not validate archive entry names, apply traversal protection, or encrypt content. AddFileToZip ignores a null file stream and resets a supplied seekable stream by assigning Position = 0 (a non-seekable stream therefore fails); a null file name gets a generated File n entry name. ZIP helper outputs that contain at least one tuple are left positioned at zero. A truly empty ZipFiles sequence returns an empty stream rather than a ZIP central directory. Caller-owned source streams and archives are not disposed by these methods.

using var one = new MemoryStream(input);
using var two = new MemoryStream(input);
(Stream? fileStream, string fileName) a = (one, "one.txt");
(Stream? fileStream, string fileName) b = (two, "folder/two.txt");

// Single-file overloads: supplied destination or newly allocated result.
await using var suppliedZip = new MemoryStream();
await a.ZipFile(
    suppliedZip,
    CompressionLevel.SmallestSize,
    cancellationToken);
await using MemoryStream allocatedZip = await a.ZipFile(
    CompressionLevel.SmallestSize,
    cancellationToken);

// Multiple-file overloads: supplied destination or newly allocated result.
await using var suppliedManyZip = new MemoryStream();
await new[] { a, b }.ZipFiles(
    suppliedManyZip,
    CompressionLevel.Optimal,
    cancellationToken);
await using MemoryStream allocatedManyZip = await new[] { a, b }.ZipFiles(
    CompressionLevel.Optimal,
    cancellationToken);

// Add to an existing archive.
using (var archiveStream = new MemoryStream())
{
    using (var archive = new ZipArchive(
        archiveStream,
        ZipArchiveMode.Create,
        leaveOpen: true))
    {
        await one.AddFileToZip(
            archive,
            "one.txt",
            CompressionLevel.Fastest,
            cancellationToken);
        await new[] { b }.AddFilesToZip(
            archive,
            CompressionLevel.Optimal,
            cancellationToken);
    }
}

For direct file-to-file compression, the input must exist. The output directory is created when needed, and FileMode.Create replaces/truncates an existing output. These helpers use CompressionLevel.Optimal and leave no FileStream open.

await Files.CompressFile(
    "input.bin",
    "compressed/input.gz",
    Streams.ECompressionType.Gzip,
    cancellationToken);
await Files.DecompressFile(
    "compressed/input.gz",
    "restored/input.bin",
    Streams.ECompressionType.Gzip,
    cancellationToken);

ECompressionType members are Brotli = 1, Gzip = 2, Deflate = 3, ZLib = 4, and None = 5.

License

MIT. See LICENSE.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed.  net11.0 is compatible. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.1

    • No dependencies.
  • net11.0

    • No dependencies.

NuGet packages (5)

Showing the top 5 NuGet packages that depend on CommonNetFuncs.Compression:

Package Downloads
CommonNetFuncs.Web.Aws.S3

Helper methods that deal with AWS S3 interactions

CommonNetFuncs.Web.Requests

Helper methods that deal with creating, sending, and handling the responses from REST API calls

CommonNetFuncs.Web.Common

Helper methods that deal with general web applications.

CommonNetFuncs.Email

Helper methods that deal with sending email, including an SMTP email sending service and an HTML email builder helper.

CommonNetFuncs.Web.Middleware

Misc middleware for ASP.NET MVC applications

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
5.0.0 97 11/23/2026
4.5.4 264 11/11/2026
4.5.3 721 10/15/2026
4.5.2 220 10/10/2026
4.5.0 301 10/9/2026
4.4.3 440 9/15/2026
4.4.1 287 8/31/2026
4.4.0 152 8/31/2026
4.3.1 472 8/11/2026
4.3.0 380 8/3/2026
4.1.14 156 8/3/2026
4.1.12 363 7/31/2026
4.1.11 299 7/9/2026
4.1.0 305 6/5/2026
4.0.53 236 5/28/2026
4.0.43 250 5/14/2026
4.0.40 221 5/10/2026
4.0.39 204 5/10/2026
4.0.37 220 5/9/2026
Loading failed