Click here to Skip to main content
Licence CPOL
First Posted 3 Mar 2009
Views 11,518
Bookmarked 15 times

Simple way to decompress Gzip files

By | 3 Mar 2009 | Article
Decompressing Gzip files.

Introduction

I have been working with Gzip files, and needed a solid way to read a stream into a correct byte size array. I studied some of what MSDN says to do, and worked out a solution to the problem. The problem is MSDN doesn’t set the size of the array and uses a default size of 4086 bytes. Some of the files I decompress are well over that amount, so I read the RFC (RFC 1952) for a Gzip, and came up with the following code.

Background

In the program that I’m writing, there is code for us to control the amount of memory that we allocate for each item used. The basic idea is to only allocate the amount of memory that you need, and this goes back to my unmanaged coding days.

Using the Code

Instead of creating a huge array of bytes and guessing what the actual size would be, we can parse the packed bytes and get the last 4 bits using ConvertBit, and get the decompressed size. I use the code for messages that are wrapped in headers, so that is why I use the variable start to offset the data, which is the start of my Gzip data. This fairly straightforward code just passes the byte array and the start index into the function, and it will hand back the decompressed file.

/// <summary>
/// Method is used to decompress Gzip files.
/// </summary>
/// <param name="data">Compressed Data</param>
/// <param name="start">Start index of compressed Data</param>
/// <returns>Uncompressed Data in Byte Array</returns>

public static byte[] HandleGzipMessage(byte[] data, int start)
{
    MemoryStream memStream;
    int size = BitConverter.ToInt32(data, data.Length - 4);
    byte[] uncompressedData = new byte[size];
    memStream = new MemoryStream(data, start, (data.Length - start));
    memStream.Position = 0;
    GZipStream gzStream = new GZipStream(memStream, CompressionMode.Decompress);

    try
    {
        gzStream.Read(uncompressedData, 0, size);
    }
    catch(Exception gzError)
    {
        throw gzError;
    }
    gzStream.Close();
    return uncompressedData;
}

Points of Interest

I hope this helps someone out with Gzip files.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

thardy33



United States United States

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 1 PinmemberChamadness4:36 10 Mar '09  
GeneralMy vote of 1 PinmemberEugene Sichkar16:14 6 Mar '09  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 3 Mar 2009
Article Copyright 2009 by thardy33
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid