Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unzip Byte Array in C#

Tags:

c#

gzip

unzip

I'm working on EBICS protocol and i want to read a data in an XML File to compare with another file.

I have successfull decode data from base64 using Convert.FromBase64String(OrderData); but now i have a byte array.

To read the content i have to unzip it. I tried to unzip it using Gzip like this example :

static byte[] Decompress(byte[] data)
{
    using (var compressedStream = new MemoryStream(data))
    using (var zipStream = new GZipStream(compressedStream, CompressionMode.Decompress))
    using (var resultStream = new MemoryStream())
    {
        zipStream.CopyTo(resultStream);
        return resultStream.ToArray();
    }
}

But it does not work i have an error message :

the magic number in gzip header is not correct. make sure you are passing in a gzip stream

Now i have no idea how i can unzip it, please help me !

Thanks !

like image 624
Thomas Rollet Avatar asked Dec 20 '25 21:12

Thomas Rollet


2 Answers

The first four bytes provided by the OP in a comment to another answer: 0x78 0xda 0xe5 0x98 is the start of a zlib stream. It is neither gzip, nor zip, but zlib. You need a ZlibStream, which for some reason Microsoft does not provide. That's fine though, since what Microsoft does provide is buggy.

You should use DotNetZip, which provides ZlibStream, and it works.

like image 52
Mark Adler Avatar answered Dec 23 '25 09:12

Mark Adler


Try using SharpZipLib. It copes with various compression formats and is free under the GPL license.

As others have pointed out, I suspect you have a zip stream and not gzip. If you check the first 4 bytes in a hex view, ZIP files always start with 0x04034b50 ZIP File Format Wiki whereas GZIP files start with 0x8b1f GZIP File Format Wiki

like image 23
Paul Avatar answered Dec 23 '25 10:12

Paul