MrLucky Posted April 13, 2006 Posted April 13, 2006 (edited) Uncompress a gzipped byte array Is it possible to uncompress a gzipped byte array? Edited April 14, 2006 by MrLucky Quote
Administrators PlausiblyDamp Posted April 13, 2006 Administrators Posted April 13, 2006 http://www.icsharpcode.net/OpenSource/SharpZipLib/Default.aspx has a free .Net compression library that can handle gzip compression (amongst other file formats). Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
MrLucky Posted April 13, 2006 Author Posted April 13, 2006 Well I played a little with it, but I have some problems: I every time get the exception, first byte doens't match But i'm sure it's compressed with gzip.. Quote
MrLucky Posted April 14, 2006 Author Posted April 14, 2006 IS it possible to uncompress a gzipped byte array? Quote
Administrators PlausiblyDamp Posted April 14, 2006 Administrators Posted April 14, 2006 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? Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
MrLucky Posted April 14, 2006 Author Posted April 14, 2006 I thought it only can be used with streams? How can I decompress a byte array then? I'm using this function: 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 I'm 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(); Quote
Administrators PlausiblyDamp Posted April 14, 2006 Administrators Posted April 14, 2006 Use a MemoryStreeam opened over the byte array. MemoryStream ms = new MemoryStream(contents); Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
MrLucky Posted April 14, 2006 Author Posted April 14, 2006 Great thanks! :) But here's 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 didn't 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 there's no header in my file I want to be uncompressed, and that's why I still get the error I think. But how can I uncompress this then? Quote
Administrators PlausiblyDamp Posted April 14, 2006 Administrators Posted April 14, 2006 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). Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.