Lz4 Project: [http://cyan4973.github.io/lz4/]
Lastest Lz4 version: [https://code.google.com/p/lz4/source/detail?r=94 r94]
A high compression derivative, called LZ4_HC, is also provided. It trades CPU for compression ratio.
Lz4.Net is a wrapper using the native dll (x86/x64) and has some helper methods and stream implementation.Lz4.Net implement some helpers methods for compression and decompression that keeps a 8-byte header on each compressed buffer. If you prefer to avoid this small overhead, you can use the Lz4 native methods.
```csharp// out data: some byte array byte[] buffer = Encoding.UTF8.GetBytes ("large text"); // compress byte[] compressed = Lz4Net.Lz4.CompressBytes (buffer, 0, buffer.Length, Lz4Net.Lz4Mode.Fast);
<h3>Simple byte[] decompression</h3>
```csharp
// decompress
byte[] buffer = Lz4Net.Lz4.DecompressBytes (compressed);
// compress string compressed = Lz4Net.Lz4.CompressString ("large text");
// decompress string uncompressed = Lz4Net.Lz4.DecompressString (compressed);
<h3>Exposed native methods</h3>
```csharp
unsafe
{
// compress
// our data
byte[] data = Encoding.UTF8.GetBytes ("large text");
int originalSize = data.Length;
// get minimum work buffer
buffer = new byte[LZ4_compressBound (originalSize)];
int sz;
// get buffers pointers
fixed (byte* pData = &data[0], pBuffer = &buffer[0])
{
// compress
sz = Lz4Net.Lz4.LZ4_compress (pData, pBuffer, data.Length);
}
// adjust final array size
Array.Resize (ref buffer, sz);
// decompress phase
// since we know the original size
byte[] uncompressed = new byte[originalSize];
// get again the pointers
fixed (byte* pSrc = &buffer[0], pDst = &uncompressed [0])
{
// decompress
Lz4Net.Lz4.LZ4_uncompress (pSrc, pDst, originalSize);
}
}
Lz4CompressionStream to handle compression.
Lz4DecompressionStream to handle decompression.
// create file
using (var stream = new Lz4CompressionStream (new FileStream (filename, FileMode.Create), 1 << 18, Lz4Mode.HighCompression, true))
{
stream.Write (buffer, 0, buffer.Length);
}
// read
int sz = 0;
using (var stream = new Lz4DecompressionStream (new FileStream (filename, FileMode.Open), true))
{
while ((sz = stream.Read (buffer, 0, buffer.Length)) > 0)
{
// ...
}
}