High performance C# implementation of the Snappy compression algorithm
OTHER License
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.
The Snappier project aims to meet the following needs of the .NET community.
Simply add a NuGet package reference to the latest version of Snappier.
<PackageReference Include="Snappier" Version="1.0.0" />
or
dotnet add package Snappier
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
}
}
}
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
}
}
}
}
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);
}
}
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)
}
}
}
There are other projects available for C#/.NET which implement Snappy compression.