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
public static int GetGzOriginalFileSize(string fi)
{
return GetGzOriginalFileSize(new FileInfo(fi));
}
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)
{
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.