Zlib compression in .NET Core

I recently had the need to decompress data that had been compressed with zlib. Unfortunately the DeflateStream built in to .NET does not work with zlib streams containing a header and trailer (RFC1950).

Instead I used the excellent SharpZipLib library. It's written completely in C#, supports a number of compression formats and targets .NET Standard 2.0.

The API should feel familiar to any .NET developer and both compression and decompression took just a few lines of code:

public static Stream Decompress(byte[] data)
    var outputStream = new MemoryStream();
    using (var compressedStream = new MemoryStream(data))
    using (var inputStream = new InflaterInputStream(compressedStream))
        outputStream.Position = 0;
        return outputStream;

Ben Foster

About Me

I'm a software engineer and aspiring entrepreneur with 12+ years experience in the tech industry and have worked with startups and SMB’s in areas such as healthcare, recruitment and e-commerce (I even worked in enterprise, once). I founded my first startup Fabrik in 2011.

I now head up the engineering team at Checkout. If you're interested in working in an exciting and innovative fin-tech company, drop me a message.

Creative Commons Licence