Uncompress a gzipped binary file

MrLucky

Well-known member
Joined
Mar 5, 2006
Messages
47
Uncompress a gzipped byte array

Is it possible to uncompress a gzipped byte array?
 
Last edited by a moderator:
Well I played a little with it, but I have some problems:

I every time get the exception, first byte doenst match

But im sure its compressed with gzip..
 
Yes. The library at the link I gave you should give you everything you need to do it. If you are having a problem then could you post the relevant code / errors as it it makes it much easier for people to help.

Also are you sure it is a gzipped file?
 
I thought it only can be used with streams?

How can I decompress a byte array then?

Im using this function:

C#:
		static void Main(string[] args)
		{
			FileStream fs = new FileStream(@"C:\Documents and Settings\Lucas\My Documents\Mijn downloads\test.age3rec", FileMode.Open, FileAccess.Read);
			
			BinaryReader reader = new BinaryReader(fs);
			
			byte[] header = new byte[4];
			byte[] bytes = new byte[4];
			
			header = reader.ReadBytes(4);
			bytes = reader.ReadBytes(4);
			
			int ByteSize = (int) Convert.ToInt16(bytes[0]) + (Convert.ToInt16(bytes[1]) << 8) + (Convert.ToInt16(bytes[2]) << 16) + (Convert.ToInt16(bytes[3]) << 24);
			
			// This array needs to be gzip uncompressed
			byte[] contents = reader.ReadBytes((int)fs.Length - 8);
			
			Console.ReadLine();			
		}

And Im 100% sure it is gzip compressed, because I found a PHP script, which does the same as what I want my program to do. And that PHP script uses gzuncompress();
 
Great thanks! :)

But heres my next problem:
With a memory stream I thought I could finally uncompress the contents, but no. I still got an error with the message that the first byte didnt matched.

After some searching, I found that the PHP function gzcompress() is not the same as gzip compression. so gzuncompress() is also not the same als gzip uncompression.

The difference: (found in the PHP manual (http://www.php.net/gzcompress))
Note: This is not the same as gzip compression, which includes some header data. See gzencode() for gzip compression.

So theres no header in my file I want to be uncompressed, and thats why I still get the error I think.

But how can I uncompress this then?
 
It looks like (from the link you provided) that the data is just compressed using the ZLIB compression, the library I pointed to also handles this as well. Try using one of the zip related streams (IIRC there is a DeflateStream or similar).
 
Back
Top