lz4 for .NET

Posted on 2015-01-01
lz4 for .NET, original C sources recompiled for the CLR (/clr:pure)
https://github.com/IonKiwi/lz4.net

Currently supports:
  • non streaming, using managed byte arrays
  • streaming, uses the LZ4 Framing Format (v1.4.1)

Example

non-streamed
  // compress data
  byte[] dataToCompress = ...
  byte[] compressedData = LZ4Helper.Compress(dataToCompress);
  // decompress data
  byte[] decompressedData = LZ4Helper.Decompress(compressedData);


streamed
  // compress data [with content checksum]
  using (LZ4Stream stream = LZ4Stream.CreateCompressor(innerStream, LZ4FrameBlockMode.Linked, LZ4FrameBlockSize.Max64KB, LZ4FrameChecksumMode.Content, -1, false)) {
    // write uncompressed data to the lz4 stream
    // the stream will compress the data and write it to the innerStream
    stream.Write(buffer, 0, buffer.Length); 
  }

  // compress data [with block and content checksum, start a new frame after 100 data blocks]
  using (LZ4Stream stream = LZ4Stream.CreateCompressor(innerStream, LZ4FrameBlockMode.Linked, LZ4FrameBlockSize.Max64KB, LZ4FrameChecksumMode.Block | LZ4FrameChecksumMode.Content, 100, false)) {
    // write uncompressed data to the lz4 stream
    // the stream will compress the data and write it to the innerStream
    stream.Write(buffer, 0, buffer.Length); 
  }

  // decompress data
  using (LZ4Stream stream = LZ4Stream.CreateDecompressor(innerStream, false)) {
    // the lz4 stream will read the compressed data from the innerStream
    // and return the uncompressed data in 'buffer'
    int bytesRead = stream.Read(buffer, 0, buffer.Length)
  }
© 2021 - Ewout van der Linden - IonKiwi.nl