BrotliSharpLib 0.3.3

Install-Package BrotliSharpLib -Version 0.3.3
dotnet add package BrotliSharpLib --version 0.3.3
<PackageReference Include="BrotliSharpLib" Version="0.3.3" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add BrotliSharpLib --version 0.3.3
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: BrotliSharpLib, 0.3.3"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install BrotliSharpLib as a Cake Addin
#addin nuget:?package=BrotliSharpLib&version=0.3.3

// Install BrotliSharpLib as a Cake Tool
#tool nuget:?package=BrotliSharpLib&version=0.3.3
The NuGet Team does not provide support for this client. Please contact its maintainers for support.


BrotliSharpLib is a full C# port of the brotli library/compression code by Google. It is intended to be a mostly 1:1 conversion of the original C code. All code is correct as of v0.6.0 of the reference implementation.

The projects uses a minimal set of APIs to ensure compatibility with a wide range of frameworks including .NET Standard and .NET Core. It also supports little-endian and big-endian architectures and is optimised for x86, x64 and ARM processors.

BrotliSharpLib is licensed under MIT.


BrotliSharpLib can be installed via the NuGet package here.

Install-Package BrotliSharpLib


Generic/basic usage:

/** Decompression **/
byte[] brotliCompressedData = ...; // arbritary data source
byte[] uncompressedData = Brotli.DecompressBuffer(brotliCompressedData, 0, brotliCompressedData.Length /**, customDictionary **/);

/** Compression **/
byte[] uncompressedData = ...; // arbritary data source

// By default, brotli uses a quality value of 11 and window size of 22 if the parameters are omitted.
byte[] compressedData = Brotli.CompressBuffer(uncompressedData, 0, uncompressedData.Length /**, quality, windowSize, customDictionary **/);

Stream usage:

/** Decompression **/
using (var ms = new MemoryStream())
using (var bs = new BrotliStream(compressedStream, CompressionMode.Decompress))

/** Compression **/
using (var fs = File.OpenRead(filePath))
using (var ms = new MemoryStream())
    using (var bs = new BrotliStream(ms, CompressionMode.Compress))
        // By default, BrotliSharpLib uses a quality value of 1 and window size of 22 if the methods are not called.
        /** bs.SetQuality(quality); **/
        /** bs.SetWindow(windowSize); **/
        /** bs.SetCustomDictionary(customDict); **/
        /* IMPORTANT: Only use the destination stream after closing/disposing the BrotliStream
           as the BrotliStream must be closed in order to signal that no more blocks are being written
           for the final block to be flushed out 
        byte[] compressed = ms.ToArray();

Real-life example: The following allows for acceptance and decompression of brotli encoded web content via a HttpClient and falls back to gzip or deflate when required.

static class HttpClientEx
    private class BrotliCompressionHandler : DelegatingHandler
        protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
            request.Headers.AcceptEncoding.Add(new StringWithQualityHeaderValue("br"));
            var response = await base.SendAsync(request, cancellationToken);
            IEnumerable<string> ce;
            if (response.Content.Headers.TryGetValues("Content-Encoding", out ce) && ce.First() == "br")
                var buffer = await response.Content.ReadAsByteArrayAsync();
                response.Content = new ByteArrayContent(Brotli.DecompressBuffer(buffer, 0, buffer.Length));
            return response;

    public static HttpClient Create()
        var handler = new HttpClientHandler();
        if (handler.SupportsAutomaticDecompression)
            handler.AutomaticDecompression = System.Net.DecompressionMethods.Deflate | System.Net.DecompressionMethods.GZip;
        return HttpClientFactory.Create(handler, new BrotliCompressionHandler());


Considerations for Build

For optimal performance, ensure to build BrotliSharpLib in Release mode to enable all possible JIT optimisations.

Performance can also be further improved by building BrotliSharpLib using .NET Framework 4.5 or above (or any framework that supports AggressiveInlining). Selecting a specific target platform (instead of AnyCPU) where possible can also further improve performance. All of this however, is completely optional as BrotliSharpLib is designed to run in a wide range of contexts and configurations regardless.


The following are benchmark results using DotNetBenchmark with BrotliSharpLib (v0.2.1) and Google's C# implementation built against .NET Framework 4.6.1. The original C version was compiled in Release mode using Visual Studio 2017 (v141) as a 64-bit Windows executable.

BenchmarkDotNet=v0.10.6, OS=Windows 10 Redstone 2 (10.0.15063)
Processor=Intel Core i5-6600K CPU 3.50GHz (Skylake), ProcessorCount=4
Frequency=3421875 Hz, Resolution=292.2374 ns, Timer=TSC
  [Host]       : Clr 4.0.30319.42000, 64bit RyuJIT-v4.7.2046.0
  RyuJitX64    : Clr 4.0.30319.42000, 64bit RyuJIT-v4.7.2046.0

File: UPX v3.91 (Windows Executable)

Method Mean
GoogleImpl 12.75 ms
BrotliSharpLib 11.63 ms
Original C 11.17 ms

As seen above, BrotliSharpLib performs close to the original C version in terms of decompression.


File: plrabn12.txt

Method Quality Mean
BrotliSharpLib 1 9.132 ms
Original C 1 9.570 ms
BrotliSharpLib 6 58.720 ms
Original C 6 36.540 ms
BrotliSharpLib 9 116.318 ms
Original C 9 73.080 ms
BrotliSharpLib 11 1822.702 ms
Original C 11 877.58 ms

While BrotliSharpLib performs comparatively at lower quality levels, it performs up to three times worse at level 11. Future versions of the port will hopefully bring this down.

NuGet packages (14)

Showing the top 5 NuGet packages that depend on BrotliSharpLib:

Package Downloads

Titanium is a light weight web proxy.


Applitools Eyes adds automated visual validation to your automated tests. With Applitools Eyes, you can automatically validate the correctness of your application's UI layout, content and appearance. To learn more, visit .


Apache Avro serializer for .NET.


HttpReports is a lightweight APM system developed for .NET Core


WebMarkupMin.AspNet.Brotli contains one compressor-adapter for compression of text content by using the Brotli algorithm ( - `BrotliCompressor`. `BrotliCompressor` is based on the BrotliSharpLib ( version 0.3.3. In version for .NET Standard 2.1 uses the built-in compressor from the `System.IO.Compression` namespace.

GitHub repositories (8)

Showing the top 5 popular GitHub repositories that depend on BrotliSharpLib:

Repository Stars
[.NET] m3u8 downloader 开源的命令行m3u8/HLS/dash下载器,支持普通AES-128-CBC解密,多线程,自定义请求头等. 支持简体中文,繁体中文和英文. English Supported.
A cross-platform asynchronous HTTP(S) proxy server in C#.
HttpReports is an APM (application performance monitor) system for .Net Core.
The Web Markup Minifier (abbreviated WebMarkupMin) - a .NET library that contains a set of markup minifiers. The objective of this project is to improve the performance of web applications by reducing the size of HTML, XHTML and XML code.
Version Downloads Last updated
0.3.3 691,545 2/6/2019
0.3.2 10,914 12/9/2018
0.3.1 39,117 10/5/2018
0.3.0 7,331 7/3/2018
0.2.1 40,693 7/16/2017
0.2.0 1,212 6/28/2017