{"id":15036186,"url":"https://github.com/master131/brotlisharplib","last_synced_at":"2025-04-09T23:23:39.038Z","repository":{"id":46095076,"uuid":"92816093","full_name":"master131/BrotliSharpLib","owner":"master131","description":"Full C# port of Brotli compression algorithm","archived":false,"fork":false,"pushed_at":"2021-11-15T04:35:32.000Z","size":3178,"stargazers_count":96,"open_issues_count":8,"forks_count":23,"subscribers_count":8,"default_branch":"master","last_synced_at":"2025-03-24T01:12:52.111Z","etag":null,"topics":["brotli","compression","csharp","csharp-library","decompression","net-framework"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/master131.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-05-30T09:14:31.000Z","updated_at":"2024-05-20T00:54:17.000Z","dependencies_parsed_at":"2022-08-30T21:21:58.252Z","dependency_job_id":null,"html_url":"https://github.com/master131/BrotliSharpLib","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/master131%2FBrotliSharpLib","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/master131%2FBrotliSharpLib/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/master131%2FBrotliSharpLib/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/master131%2FBrotliSharpLib/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/master131","download_url":"https://codeload.github.com/master131/BrotliSharpLib/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248126940,"owners_count":21052106,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["brotli","compression","csharp","csharp-library","decompression","net-framework"],"created_at":"2024-09-24T20:30:26.874Z","updated_at":"2025-04-09T23:23:39.020Z","avatar_url":"https://github.com/master131.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# BrotliSharpLib\n\nBrotliSharpLib is a full C# port of the brotli library/compression code [by Google](https://github.com/google/brotli). 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.\n\nThe 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.\n\nBrotliSharpLib is licensed under [MIT](https://github.com/master131/BrotliSharpLib/blob/master/LICENSE).\n\n## Installation\nBrotliSharpLib can be installed via the NuGet package [here](https://www.nuget.org/packages/BrotliSharpLib/).\n```\nInstall-Package BrotliSharpLib\n```\n\n## Usage\n**Generic/basic usage:**\n```c#\n/** Decompression **/\nbyte[] brotliCompressedData = ...; // arbritary data source\nbyte[] uncompressedData = Brotli.DecompressBuffer(brotliCompressedData, 0, brotliCompressedData.Length /**, customDictionary **/);\n\n/** Compression **/\nbyte[] uncompressedData = ...; // arbritary data source\n\n// By default, brotli uses a quality value of 11 and window size of 22 if the parameters are omitted.\nbyte[] compressedData = Brotli.CompressBuffer(uncompressedData, 0, uncompressedData.Length /**, quality, windowSize, customDictionary **/);\n```\n\n**Stream usage:**\n```c#\n/** Decompression **/\nusing (var ms = new MemoryStream())\nusing (var bs = new BrotliStream(compressedStream, CompressionMode.Decompress))\n{\n    bs.CopyTo(ms);\n}\n\n/** Compression **/\nusing (var fs = File.OpenRead(filePath))\nusing (var ms = new MemoryStream())\n{\n    using (var bs = new BrotliStream(ms, CompressionMode.Compress))\n    {\n        // By default, BrotliSharpLib uses a quality value of 1 and window size of 22 if the methods are not called.\n        /** bs.SetQuality(quality); **/\n        /** bs.SetWindow(windowSize); **/\n        /** bs.SetCustomDictionary(customDict); **/\n        fs.CopyTo(bs);\n    }\n\n    byte[] compressed = ms.ToArray();\n}\n```\n\n**Real-life example:**\nThe following allows for acceptance and decompression of brotli encoded web content via a [HttpClient](https://msdn.microsoft.com/en-us/library/system.net.http.httpclient(v=vs.118).aspx) and falls back to gzip or deflate when required.\n```c#\nstatic class HttpClientEx\n{\n    private class BrotliCompressionHandler : DelegatingHandler\n    {\n        protected override async Task\u003cHttpResponseMessage\u003e SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)\n        {\n            request.Headers.AcceptEncoding.Add(new StringWithQualityHeaderValue(\"br\"));\n            var response = await base.SendAsync(request, cancellationToken);\n            IEnumerable\u003cstring\u003e ce;\n            if (response.Content.Headers.TryGetValues(\"Content-Encoding\", out ce) \u0026\u0026 ce.First() == \"br\")\n            {\n                var buffer = await response.Content.ReadAsByteArrayAsync();\n                response.Content = new ByteArrayContent(Brotli.DecompressBuffer(buffer, 0, buffer.Length));\n            }\n            return response;\n        }\n    }\n\n    public static HttpClient Create()\n    {\n        var handler = new HttpClientHandler();\n        if (handler.SupportsAutomaticDecompression)\n            handler.AutomaticDecompression = System.Net.DecompressionMethods.Deflate | System.Net.DecompressionMethods.GZip;\n        return HttpClientFactory.Create(handler, new BrotliCompressionHandler());\n    }\n}\n```\n\n## Performance\n### Considerations for Build\nFor optimal performance, ensure to build BrotliSharpLib in **Release** mode to enable all possible JIT optimisations.\n\nPerformance 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.\n\n### Benchmark\n\nThe following are benchmark results using [DotNetBenchmark](https://github.com/dotnet/BenchmarkDotNet) with BrotliSharpLib (v0.2.1) and [Google's C# implementation](https://github.com/google/brotli/tree/master/csharp/org/brotli/dec) 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.\n\n``` ini\nBenchmarkDotNet=v0.10.6, OS=Windows 10 Redstone 2 (10.0.15063)\nProcessor=Intel Core i5-6600K CPU 3.50GHz (Skylake), ProcessorCount=4\nFrequency=3421875 Hz, Resolution=292.2374 ns, Timer=TSC\n  [Host]       : Clr 4.0.30319.42000, 64bit RyuJIT-v4.7.2046.0\n  RyuJitX64    : Clr 4.0.30319.42000, 64bit RyuJIT-v4.7.2046.0\nRuntime=Clr  \n```\n#### Decompression\nFile: UPX v3.91 (Windows Executable)\n\n |         Method |     Mean |\n |--------------- |---------:|\n |     GoogleImpl | 12.75 ms |\n | BrotliSharpLib | 11.63 ms | \n |     Original C | 11.17 ms |\n \n As seen above, BrotliSharpLib performs close to the original C version in terms of decompression.\n\n #### Compression\n File: plrabn12.txt\n \n |         Method |  Quality |         Mean |\n |--------------- |--------- |-------------:|\n | BrotliSharpLib |        1 |     9.132 ms |\n |     Original C |        1 |     9.570 ms |\n | BrotliSharpLib |        6 |    58.720 ms |\n |     Original C |        6 |    36.540 ms |\n | BrotliSharpLib |        9 |   116.318 ms |\n |     Original C |        9 |    73.080 ms |\n | BrotliSharpLib |       11 |  1822.702 ms |\n |     Original C |       11 |    877.58 ms |\n \n 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.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmaster131%2Fbrotlisharplib","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmaster131%2Fbrotlisharplib","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmaster131%2Fbrotlisharplib/lists"}