Git Product home page Git Product logo

libdeflate.net's Introduction

LibDeflate.NET nuget stable

LibDeflate.NET is a managed wrapper around libdeflate, a native library for fast, whole-buffer DEFLATE-based compression and decompression.

Native binaries of libdeflate are packaged in LibDeflate.Native.

Usage

Compression

Creating a Compressor

Create a compressor for the DEFLATE (RFC 1951) format.

using LibDeflate;

using Compressor compressor = new DeflateCompressor(compressionLevel: 9);

Create a compressor for the zlib (RFC 1950) format.

using LibDeflate;

using Compressor compressor = new ZlibCompressor(compressionLevel: 9);

Create a compressor for the gzip (RFC 1952) format.

using LibDeflate;

using Compressor compressor = new GzipCompressor(compressionLevel: 9);

Compressing with a Compressor

Compress text using a Compressor

using System;
using System.Text;
using LibDeflate;

using Compressor compressor = new DeflateCompressor(compressionLevel: 9);

// This will fail, because it is too short to compress effectively
CompressString("Hello, world!");
// These will succeed, because they are long enough to compress effectively
CompressString("Gases have the lowest density of the three, are highly compressible, and completely fill any container in which they are placed.");
CompressString("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis fermentum lacus a est auctor, ut ultrices leo lobortis. Cras ac enim neque. Maecenas nisi nulla, auctor a nisi ut, mollis auctor nibh. Donec lacinia augue neque, at molestie lectus porta et. Maecenas posuere sed nisi ac vehicula. Ut id nisl a dui fringilla rhoncus sit amet id erat. In sagittis sem est, quis porta diam tempor id. Vestibulum non cursus justo. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia curae; Vivamus in malesuada urna, sed volutpat mi. Donec dapibus nibh sodales, cursus mauris quis, tempus mauris. In eget fermentum nunc. Nunc convallis luctus libero. Nulla sed eros nec tortor scelerisque euismod.");

void CompressString(string input)
{
    byte[] inputBytes = Encoding.UTF8.GetBytes(input);
    //Compress returns a System.Buffers.IMemoryOwner<byte>, which should be disposed after use
    using var ownedMemory = compressor.Compress(inputBytes);
    if (ownedMemory == null)
    {
        Console.WriteLine("Compressing this {0} byte input would output greater than {0} bytes!", inputBytes.Length);
        return;
    }
    Console.WriteLine("Compressed this {0} byte input to {1} byte output ({2:0.0%} ratio)", inputBytes.Length, ownedMemory.Length, (double)ownedMemory.Length / inputBytes.Length);
}

This produces the output:

Compressing this 13 byte input would output greater than 13 bytes!
Compressed this 128 byte input to 95 byte output (74.2% ratio)
Compressed this 717 byte input to 382 byte output (53.3% ratio)

Decompression

Creating a Decompressor

Create a decompressor for the DEFLATE (RFC 1951) format.

using LibDeflate;

using Decompressor decompressor = new DeflateDecompressor();

Create a decompressor for the zlib (RFC 1950) format.

using LibDeflate;

using Decompressor decompressor = new ZlibDecompressor();

Create a decompressor for the gzip (RFC 1952) format.

using LibDeflate;

using Decompressor decompressor = new GzipDecompressor();

Decompressing with a Decompressor

Compress text with a Compressor and immediately decompress it with a Decompressor

using System;
using System.Buffers;
using System.Text;
using LibDeflate;

///
// Compression
///

using Compressor compressor = new DeflateCompressor(compressionLevel: 9);

// These will succeed, because they are long enough to compress effectively
const string exampleText1 = "Gases have the lowest density of the three, are highly compressible, and completely fill any container in which they are placed.";
const string exampleText2 = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis fermentum lacus a est auctor, ut ultrices leo lobortis. Cras ac enim neque. Maecenas nisi nulla, auctor a nisi ut, mollis auctor nibh. Donec lacinia augue neque, at molestie lectus porta et. Maecenas posuere sed nisi ac vehicula. Ut id nisl a dui fringilla rhoncus sit amet id erat. In sagittis sem est, quis porta diam tempor id. Vestibulum non cursus justo. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia curae; Vivamus in malesuada urna, sed volutpat mi. Donec dapibus nibh sodales, cursus mauris quis, tempus mauris. In eget fermentum nunc. Nunc convallis luctus libero. Nulla sed eros nec tortor scelerisque euismod.";
using var compressedString1 = CompressString(exampleText1, out int uncompressedLength1);
using var compressedString2 = CompressString(exampleText2, out int uncompressedLength2);

IMemoryOwner<byte> CompressString(string input, out int uncompressedSize)
{
    byte[] inputBytes = Encoding.UTF8.GetBytes(input);
    // libdeflate recommends storing the uncompressed size and using it for decompression.
    // Otherwise, we would need to guess at how big the uncompressed output buffer is.
    uncompressedSize = inputBytes.Length;
    // Compress returns a System.Buffers.IMemoryOwner<byte>, which should be disposed after use
    var ownedMemory = compressor.Compress(inputBytes);
    if (ownedMemory == null)
    {
        Console.WriteLine("Compressing this {0} byte input would output greater than {0} bytes!", inputBytes.Length);
        return null;
    }
    Console.WriteLine("Compressed this {0} byte input to {1} byte output ({2:0.0%} ratio)", inputBytes.Length, ownedMemory.Length, (double)ownedMemory.Length / inputBytes.Length);
    return ownedMemory;
}

///
// Decompression
///

using Decompressor decompressor = new DeflateDecompressor();

var decompressedString1 = DecompressString(compressedString1.Memory.Span, uncompressedLength1);
var decompressedString2 = DecompressString(compressedString2.Memory.Span, uncompressedLength2);

string DecompressString(ReadOnlySpan<byte> compressedInput, int uncompressedSize)
{
    // Compress returns a System.Buffers.OperationStatus, which should be checked for success before using the output parameters
    var status = decompressor.Decompress(compressedInput, uncompressedSize, out var ownedMemory);
    if (status != OperationStatus.Done)
    {
        Console.WriteLine("Failed to decompress data!");
        return null;
    }

    Console.WriteLine("Decompressed this {0} byte input to {1} byte output", compressedInput.Length, ownedMemory.Length);
    var decompressedBytes = ownedMemory.Memory.Span;
    var decompressedString = Encoding.UTF8.GetString(decompressedBytes);
    Console.WriteLine("Decompressed output: {0}", decompressedString);
    return decompressedString;
}

This produces the output:

Decompressed this 95 byte input to 128 byte output
Decompressed output: Gases have the lowest density of the three, are highly compressible, and completely fill any container in which they are placed.
Decompressed this 382 byte input to 717 byte output
Decompressed output: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis fermentum lacus a est auctor, ut ultrices leo lobortis. Cras ac enim neque. Maecenas nisi nulla, auctor a nisi ut, mollis auctor nibh. Donec lacinia augue neque, at molestie lectus porta et. Maecenas posuere sed nisi ac vehicula. Ut id nisl a dui fringilla rhoncus sit amet id erat. In sagittis sem est, quis porta diam tempor id. Vestibulum non cursus justo. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia curae; Vivamus in malesuada urna, sed volutpat mi. Donec dapibus nibh sodales, cursus mauris quis, tempus mauris. In eget fermentum nunc. Nunc convallis luctus libero. Nulla sed eros nec tortor scelerisque euismod.

libdeflate.net's People

Contributors

jzebedee avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

libdeflate.net's Issues

Upgrade to libdeflate 1.18

Please stay up to date with the latest version of libdeflate so that people don't run into issues that have already been fixed. Thanks!

Please add a license

I would like to use your code to add zlib decompression support to one of my projects. However, I've noticed that there is no license mentioned anywhere in this repo. This effectively means that all rights to this code are reserved and the only thing other people are allowed to do is view and fork the repo due to GitHub's TOS.

Can you please add a license to this repo to clearly define what I and others can and can't do with your code?
If you would like a suggestion, my favourite is the MIT License which is the same license that the Libdeflate project uses. Otherwise, if you want to do something else, describe what you want from a license and maybe I could offer some suggestions for what sounds the most appropriate.

Support GDeflate?

Hello!

I'm working on a .NET app that needs to use GDeflate from libdeflate. I've found your package on NuGet, but, unfortunately for me, it doesn't implement GDeflate (nor has corresponding entry points in the .dll, it seems).

Do you have plans to implement GDeflate support in this package?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.