Language: EN


How to use Snappy compression in C# with Snappier

Snappier is an open-source library that implements the Snappy compression algorithm in C# for use in .NET applications.

Snappy is a compression algorithm developed in C++ by Google, which stands out for its speed and efficiency, and low CPU and memory consumption.

Unlike other compression algorithms, such as Gzip, Snappy is geared towards applications where speed is critical, such as in caching systems and real-time processing.

Among the many alternatives for using Snappy in .NET, I like the Snappier library. Unlike, for example,, which is a wrapper around the C++ library, Snappier is a complete port to C#.

Being a full managed port, we ensure that we will not have memory leaks, or “weird errors” that can occur when using Wrappers. In addition, we can use it on multiple platforms, not just on Windows.

On the other hand, it is highly optimized. The speed we will get with Snappier is similar to what we would get when using the C++ library directly. Especially important, if we consider that the goal of Snappy is speed.

Snappier is compatible with .NET Framework 4.6.1 or later, and .NET Core 2.0 or later. But only when using NET 3.0 or later, we will obtain a performance similar to that achieved in C++.

How to use Snappier

We can easily add the library to a .NET project through the corresponding Nuget package.

Install-Package Snappier

Here is a simple example of how to use Snappier, taken from the library’s documentation

byte[] data = {0, 1, 2}; 

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

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

Although it is normal to use Streams to optimize performance. Here is an example of how to use Snappier with Streams,

using var fileStream = File.OpenRead("somefile.txt");

// Compression example
using var compressed = new MemoryStream();
using (var compressor = new SnappyStream(compressed, CompressionMode.Compress, false)) {
    await fileStream.CopyToAsync(compressor);

// Decompression example
compressed.Position = 0;
using var decompressor = new SnappyStream(compressed, CompressionMode.Decompress);
var buffer = new byte[65536];
var bytesRead = decompressor.Read(buffer, 0, buffer.Length);
while (bytesRead > 0)
    bytesRead = decompressor.Read(buffer, 0, buffer.Length)

As we can see, only a few lines of code are needed to compress and decompress data with the Snappy algorithm, quickly and efficiently.

Snappier is Open Source, and all the code and documentation is available in the project repository at