Snappier 1.1.6

dotnet add package Snappier --version 1.1.6
NuGet\Install-Package Snappier -Version 1.1.6
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="Snappier" Version="1.1.6" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Snappier --version 1.1.6
#r "nuget: Snappier, 1.1.6"
#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.
// Install Snappier as a Cake Addin
#addin nuget:?package=Snappier&version=1.1.6

// Install Snappier as a Cake Tool
#tool nuget:?package=Snappier&version=1.1.6

Snappier

.NET Core

Introduction

Snappier is a pure C# port of Google's Snappy compression algorithm. It is designed with speed as the primary goal, rather than compression ratio, and is ideal for compressing network traffic. Please see the Snappy README file for more details on Snappy.

Project Goals

The Snappier project aims to meet the following needs of the .NET community.

  • Cross-platform C# implementation for Linux and Windows, without P/Invoke or special OS installation requirements
  • Compatible with .NET 4.6.1 and later and .NET Core 2.0 and later
  • Use .NET paradigms, including asynchronous stream support
  • Full compatibility with both block and stream formats
  • Near C++ level performance
    • Note: This is only possible on .NET Core 3.0 and later with the aid of Span<T> and System.Runtime.Intrinsics.
    • .NET Core 2.1 is almost as good, .NET 4.6.1 is the slowest
  • Keep allocations and garbage collection to a minimum using buffer pools

Installing

Simply add a NuGet package reference to the latest version of Snappier.

<PackageReference Include="Snappier" Version="1.0.0" />

or

dotnet add package Snappier

Block compression/decompression using a buffer you already own

using Snappier;

public class Program
{
    private static byte[] Data = {0, 1, 2}; // Wherever you get the data from

    public static void Main()
    {
        // This option assumes that you are managing buffers yourself in an efficient way.
        // In this example, we're using heap allocated byte arrays, however in most cases
        // you would get these buffers from a buffer pool like ArrayPool<byte> or MemoryPool<byte>.

        // Compression
        byte[] buffer = new byte[Snappy.GetMaxCompressedLength(Data)];
        int compressedLength = Snappy.Compress(Data, buffer);
        Span<byte> compressed = buffer.AsSpan(0, compressedLength);

        // Decompression
        byte[] outputBuffer = new byte[Snappy.GetUncompressedLength(compressed)];
        int decompressedLength = Snappy.Decompress(compressed, outputBuffer);

        for (var i = 0; i < decompressedLength; i++)
        {
            // Do something with the data
        }
    }
}

Block compression/decompression using a memory pool buffer

using Snappier;

public class Program
{
    private static byte[] Data = {0, 1, 2}; // Wherever you get the data from

    public static void Main()
    {
        // This option uses `MemoryPool<byte>.Shared`. However, if you fail to
        // dispose of the returned buffers correctly it can result in memory leaks.
        // It is imperative to either call .Dispose() or use a using statement.

        // Compression
        using (IMemoryOwner<byte> compressed = Snappy.CompressToMemory(Data))
        {
            // Decompression
            using (IMemoryOwner<byte> decompressed = Snappy.DecompressToMemory(compressed.Memory.Span))
            {
                // Do something with the data
            }
        }
    }
}

Block compression/decompression using heap allocated byte[]

using Snappier;

public class Program
{
    private static byte[] Data = {0, 1, 2}; // Wherever you get the data from

    public static void Main()
    {
        // This is generally the least efficient option,
        // but in some cases may be the simplest to implement.

        // Compression
        byte[] compressed = Snappy.CompressToArray(Data);

        // Decompression
        byte[] decompressed = Snappy.DecompressToArray(compressed);
    }
}

Stream compression/decompression

Compressing or decompressing a stream follows the same paradigm as other compression streams in .NET. SnappyStream wraps an inner stream. If decompressing you read from the SnappyStream, if compressing you write to the SnappyStream

This approach reads or writes the Snappy framing format designed for streaming. The input/output is not the same as the block method above. It includes additional headers and CRC32C checks.

using System.IO;
using System.IO.Compression;
using Snappier;

public class Program
{
    public static async Task Main()
    {
        using var fileStream = File.OpenRead("somefile.txt");

        // First, compression
        using var compressed = new MemoryStream();

        using (var compressor = new SnappyStream(compressed, CompressionMode.Compress, true)) {
            await fileStream.CopyToAsync(compressor);

            // Disposing the compressor also flushes the buffers to the inner stream
            // We pass true to the constructor above so that it doesn't close/dispose the inner stream
            // Alternatively, we could call compressor.Flush()
        }

        // Then, decompression

        compressed.Position = 0; // Reset to beginning of the stream so we can read
        using var decompressor = new SnappyStream(compressed, CompressionMode.Decompress);

        var buffer = new byte[65536];
        var bytesRead = decompressor.Read(buffer, 0, buffer.Length);
        while (bytesRead > 0)
        {
            // Do something with the data

            bytesRead = decompressor.Read(buffer, 0, buffer.Length)
        }
    }
}

Other Projects

There are other projects available for C#/.NET which implement Snappy compression.

  • Snappy.NET - Uses P/Invoke to C++ for great performance. However, it only works on Windows, and is a bit heap allocation heavy in some cases. It also hasn't been updated since 2014 (as of 10/2020). This project may still be the best choice if your project is on the legacy .NET Framework on Windows, where Snappier is much less performant.
  • IronSnappy - Another pure C# port, based on the Golang implemenation instead of the C++ implementation.
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  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 is compatible.  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 is compatible.  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. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  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.

NuGet packages (10)

Showing the top 5 NuGet packages that depend on Snappier:

Package Downloads
MongoDB.Driver.Core The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org.

Core Component of the Official MongoDB .NET Driver.

IronCompress

Buffer compresison library supporting all the major compression algorithms (gzip, brotli, snappy, zstd etc.)

MongoDB.Driver.Core.signed

(Unofficial) Signed Core Component of the MongoDB .NET Driver. The containing assembly was generated by signing the officially published MongoDB.Driver.Core.dll.

Etherna.MongoDB.Driver.Core

Fork by Etherna of the Core Component of the Official MongoDB .NET Driver.

Cave.MongoDB.Driver.Core

Signed version of the Core Component of the Official MongoDB .NET Driver.

GitHub repositories (7)

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

Repository Stars
ravendb/ravendb
ACID Document Database
mongodb/mongo-csharp-driver
The Official C# .NET Driver for MongoDB
jfrog/project-examples
Small projects in universal build ecosystems to configure CI and Artifactory
alexandre-spieser/mongodb-generic-repository
An example of generic repository implementation using the MongoDB C# Sharp 2.0 driver (async)
couchbase/couchbase-net-client
The official Couchbase SDK for .NET Core and Full Frameworks
Version Downloads Last updated
1.1.6 5,615 3/5/2024
1.1.5 33,426 1/23/2024
1.1.4 7,882 1/15/2024
1.1.3 24,453 12/1/2023
1.1.2 23,959 11/9/2023
1.1.1 1,031,345 3/27/2023
1.1.0 17,553 3/12/2023
1.0.0 28,427,325 1/23/2021
1.0.0-beta002 1,676 11/3/2020
1.0.0-beta001 1,342 10/22/2020