Click here to Skip to main content
15,389,584 members
Articles / Programming Languages / C#
Tip/Trick
Posted 15 Aug 2013

Tagged as

Stats

17K views
4 bookmarked

GZipStream length when uncompressed

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
15 Aug 2013CPOL
Extract decompressed file size from a Gzip file !

Introduction

As stated in the documentation, the Property Length of the GZipStream is not supported. so you wont really know the size of the file, your about to extract.

Using the code

C#
/// <summary>
/// Extracts the original filesize of the compressed file.
/// </summary>
/// <param name="fi">GZip file to handle</param>
/// <returns>Size of the compressed file, when its decompressed.</returns>
/// <remarks>More information at <a href="http://tools.ietf.org/html/rfc1952">http://tools.ietf.org/html/rfc1952</a> section 2.3</remarks>
public static int GetGzOriginalFileSize(string fi)
{
    return GetGzOriginalFileSize(new FileInfo(fi));
}
/// <summary>
/// Extracts the original filesize of the compressed file.
/// </summary>
/// <param name="fi">GZip file to handle</param>
/// <returns>Size of the compressed file, when its decompressed.</returns>
/// <remarks>More information at <a href="http://tools.ietf.org/html/rfc1952">http://tools.ietf.org/html/rfc1952</a> section 2.3</remarks>
public static int GetGzOriginalFileSize(FileInfo fi)
{
    try
    {
        using (FileStream fs = fi.OpenRead())
        {
            try
            {
                byte[] fh = new byte[3];
                fs.Read(fh, 0, 3);
                if (fh[0] == 31 && fh[1] == 139 && fh[2] == 8) //If magic numbers are 31 and 139 and the deflation id is 8 then...
                {
                    byte[] ba = new byte[4];
                    fs.Seek(-4, SeekOrigin.End);
                    fs.Read(ba, 0, 4);
                    return BitConverter.ToInt32(ba, 0);
                }
                else
                    return -1;
            }
            finally
            {
                fs.Close();
            }
        }
    }
    catch (Exception)
    {
        return -1;
    }
}

Its pretty simple you just call the GetGzOriginalFileSize method and parse the file path or fileinfo object and you get the size of the file when its decompressed.

Resources

  C# and I

History

  16-08-2013: First post.

License

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

Share

About the Author


Comments and Discussions

 
QuestionYou should return an UInt32 Pin
Octopod6-May-20 0:35
MemberOctopod6-May-20 0:35 
QuestionMy Vote of 5 Pin
Aydin Homay15-Aug-13 23:57
MemberAydin Homay15-Aug-13 23:57 
AnswerRe: My Vote of 5 Pin
Paw Jershauge16-Aug-13 7:57
MemberPaw Jershauge16-Aug-13 7:57 
GeneralRe: My Vote of 5 Pin
Aydin Homay16-Aug-13 22:41
MemberAydin Homay16-Aug-13 22:41 

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

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