How do I know the size of my compressed file used GzipStream? I know that it has a header and suffix. First 10 bytes - it's header, second 8 bytes - suffix. How do I know the size file in the suffix?
-
It goes without saying, but I'll say it anyway because none of the answers do: the last 4 bytes is the original length MOD 2^32. – Ronnie Overby Apr 09 '19 at 19:57
3 Answers
I see that you down voted my previous answer most likely because it was an example using Java. The principle is still the same, so the answer to your question would be that the last 4 bytes contain the information you require. Hopefully this answer is more what you are after.
Here is a C# Decompress
function example of decompressing the GZip inclusive of getting the size of the compressed file used by GZipStream
:
static public byte[] Decompress(byte[] b)
{
MemoryStream ms = new MemoryStream(b.length);
ms.Write(b, 0, b.Length);
//last 4 bytes of GZipStream = length of decompressed data
ms.Seek(-4, SeekOrigin.Current);
byte[] lb = new byte[4];
ms.Read(lb, 0, 4);
int len = BitConverter.ToInt32(lb, 0);
ms.Seek(0, SeekOrigin.Begin);
byte[] ob = new byte[len];
GZipStream zs = new GZipStream(ms, CompressionMode.Decompress);
zs.Read(ob, 0, len);
returen ob;
}

- 196
- 8
-
It's Java, not C#. In some cases C# and Java can be very similar and acceptable to answer to java with c# or c# with java, but in this case the code is completely different. – Gusman Nov 22 '16 at 03:21
-
1If you edit your answer and place the other response in this I will remove the negative vote, I can't remove unless you modify the answer, S.O. doesn't allows it. – Gusman Nov 22 '16 at 03:30
-
Hi @gusman I have edited my post as you requested in order to remove the down bite (albeit this was 3 years ago, lol) – WizzKidd Jan 05 '19 at 23:07
Something a bit better written:
public int GetUncompressedSize(string FileName)
{
using(BinaryReader br = new BinaryReader(File.OpenRead(pathToFile))
{
br.BaseStream.Seek(SeekOrigin.End, -4);
return br.ReadInt32();
}
}

- 14,905
- 2
- 34
- 50
I see that you down voted my previous answer most likely because it was an example using Java. The principle is still the same, so the answer to your question would be that the last 4 bytes contain the information you require. Hopefully this answer is more what you are after.
Here is a C# Decompress
function example of decompressing the GZip inclusive of getting the size of the compressed file used by GZipStream
:
static public byte[] Decompress(byte[] b)
{
MemoryStream ms = new MemoryStream(b.length);
ms.Write(b, 0, b.Length);
//last 4 bytes of GZipStream = length of decompressed data
ms.Seek(-4, SeekOrigin.Current);
byte[] lb = new byte[4];
ms.Read(lb, 0, 4);
int len = BitConverter.ToInt32(lb, 0);
ms.Seek(0, SeekOrigin.Begin);
byte[] ob = new byte[len];
GZipStream zs = new GZipStream(ms, CompressionMode.Decompress);
zs.Read(ob, 0, len);
returen ob;
}

- 196
- 8