Search code examples
c#gzipunzip

Unzip Byte Array in C#


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 !


Solution

  • 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.