I was writing some code the other night on a little pet project I am working on and I needed to persist some data to disc. The data was quite large so I needed to compress it first. I have not used any of the compression libraries in .NET before so I had a little investigation and wrote the static methods below.

They are very easy to use, just pass in a  byte array to compress and recieve a compressed byte array back out, and visa versa to decompress the data.

I thought I  would share these code snippets as they are quite useful. Enjoy.

private static byte[] Compress(byte[] input)
{
    byte[] compressesData;

    using (var outputStream = new MemoryStream())
    {
        using (var zip = new GZipStream(outputStream, CompressionMode.Compress))
        {
            zip.Write(input, 0, input.Length);
        }

        compressesData = outputStream.ToArray();
    }

    return compressesData;
}

private static byte[] Decompress(byte[] input)
{
    byte[] decompressedData;

    using (var outputStream = new MemoryStream())
    {
        using (var inputStream = new MemoryStream(input))
        {
            using (var zip = new GZipStream(inputStream, CompressionMode.Decompress))
            {
                zip.CopyTo(outputStream);
            }
        }

        decompressedData = outputStream.ToArray();
    }

    return decompressedData;
}
Participate with Coding in the Trenches on Facebook
Participate with Coding in the Trenches on Facebook by Click the button above.
Advertisements

One comment

  1. How large is quite large? It might be worth doing some performance tests to see if it takes longer to write the uncompressed file or to compress the large byte array and write the compressed version. It might be worth looking at MemoryMappedFiles, which can attach to a physical file on disk or exist in the swap space if they’re only needed for the lifetime of the processes accessing them. They basically get added to the logical address space of your app – hence my recent Facebook image post that shows the Peak Working Set of a console application at 48Gb when the machine only has 8Gb of RAM and nowhere near 48Gb of swap. Since the MemoryMappedFile is also basically Shared Memory, it’s possible to use one in conjunction with WaitHandles to perform IPC that’s about twice as fast as WCF over Named Pipes.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s