Skip to content

Latest commit

 

History

History
119 lines (84 loc) · 3.02 KB

README.md

File metadata and controls

119 lines (84 loc) · 3.02 KB

lz4-net

Lz4.Net is a wrapper of the Lz4 lib

Lz4 Project: [http://cyan4973.github.io/lz4/]
Lastest Lz4 version: [https://code.google.com/p/lz4/source/detail?r=94 r94]

Lz4 Compression Algorithm

LZ4 is a very fast lossless compression algorithm, providing compression speed at 300 MB/s per core, scalable with multi-cores CPU. It also features an extremely fast decoder, with speeds up and beyond 1GB/s per core, typically reaching RAM speed limits on multi-core systems.

A high compression derivative, called LZ4_HC, is also provided. It trades CPU for compression ratio.

Lz4.Net

Lz4.Net is a wrapper using the native dll (x86/x64) and has some helper methods and stream implementation.

Examples

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.

Simple byte[] compression

```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);

String Compression Helpers

These help functions will compress the string bytes and return a base 64 encoded string. ```csharp

// 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);
    }

}

Streams

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)
    {
        // ...
    }
}

License

http://opensource.org/licenses/BSD-3-Clause