Compression Utilities

The compression utilities inside Protogame provide helper methods which compress byte arrays using the LZMA algorithm.

The compression utilities do not require loading a module; instead use the LzmaHelper class directly.

LzmaHelper.Compress

This method takes an input (uncompressed) stream and an output stream.

To compress a byte array, use this method in conjunction with the MemoryStream class available in .NET:

byte[] uncompressedBytes;
byte[] compressedBytes;

using (var input = new MemoryStream(uncompressedBytes))
{
    using (var output = new MemoryStream())
    {
        LzmaHelper.Compress(input, output);
        var length = output.Position;
        compressedBytes = new byte[length];
        output.Seek(SeekOrigin.Begin, 0);
        output.Read(compressedBytes, 0, length);
    }
}

LzmaHelper.Decompress

This method takes an input (compressed) stream and an output stream.

To compress a byte array, use this method in conjunction with the MemoryStream class available in .NET:

byte[] uncompressedBytes;
byte[] compressedBytes;

using (var input = new MemoryStream(compressedBytes))
{
    using (var output = new MemoryStream())
    {
        LzmaHelper.Decompress(input, output);
        var length = output.Position;
        uncompressedBytes = new byte[length];
        output.Seek(SeekOrigin.Begin, 0);
        output.Read(uncompressedBytes, 0, length);
    }
}