Thursday, June 25, 2015

C# byte [ ] Array Compress and Decompress with GZipStream

A small utility class for compressing and decompressing byte [ ] array in C#

using System.IO;
using System.IO.Compression;

public static class Compression
{
    public static byte[] Compress(byte[] data)
    {
        using (var ms = new MemoryStream())
        {
            using (var gzip = new GZipStream(ms, CompressionLevel.Optimal))
            {
                gzip.Write(data, 0, data.Length);
            }
            data = ms.ToArray();
        }
        return data;
    }

    public static byte[] Decompress(byte[] data)
    {
        // the trick is to read the last 4 bytes to get the length
        // gzip appends this to the array when compressing
        var lengthBuffer = new byte[4];
        Array.Copy(data, data.Length - 4, lengthBuffer, 0, 4);
        int uncompressedSize = BitConverter.ToInt32(lengthBuffer, 0);
        var buffer = new byte[uncompressedSize];
        using (var ms = new MemoryStream(data))
        {
            using (var gzip = new GZipStream(ms, CompressionMode.Decompress))
            {
                gzip.Read(buffer, 0, uncompressedSize);
            }
        }
        return buffer;
    }
}

17 comments:

  1. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a .Net developer learn from .Net Training in Chennai. or learn thru ASP.NET Essential Training Online .

    ReplyDelete
  2. I was recommended this web site by means of my cousin.
    I am now not certain whether this post is written through him as nobody else recognise such precise about my difficulty. You're amazing! Thank you!

    Selenium training in bangalore
    Selenium training in Chennai
    Selenium training in Bangalore
    Selenium training in Pune
    Selenium Online training

    ReplyDelete
  3. When your website or blog goes live for the first time, it is exciting. That is until you realize no one but you and your. JPEG Optimizer

    ReplyDelete
  4. Whatever we gathered information from the blogs, we should implement that in practically then only we can understand that exact thing clearly, but it’s no need to do it, because you have explained the concepts very well. It was crystal clear, keep sharing..
    Java Training in Chennai
    Java Training in Coimbatore
    Java Training in Bangalore

    ReplyDelete
  5. Great blog thanks for sharing Online marketing has changed how businesses run and succeed in today’s highly competitive world. With Adhuntt Media, your E-commerce store can reach for the stars and find the right customers. Our team of marketing analysts can analyze, strategize and implement the perfect path for you to take to beat your competition and come out as #1.
    digital marketing company in chennai
    seo service in chennai
    web designing company in chennai
    social media marketing company in chennai

    ReplyDelete
  6. Nice blog thanks for sharing Set up your very own unique and classy vertical garden in Chennai. Karuna Nursery Gardens offers you with all the tools and services for building the best vertical garden in your neighbourhood.
    plant nursery in chennai
    rental plants in chennai
    corporate gardening service in chennai

    ReplyDelete
  7. Excellent blog thanks for sharing Setting up a successful salon means that you need the best wholesale cosmetics suppliers in Chennai to back up your brand. With hundreds of exclusive international brands and down to earth service, Pixies Beauty Shop is your destination to success.
    Cosmetics Shop in Chennai

    ReplyDelete