Pergunta

like descriped in the title, I want to decompress a HTTP-Resonse. Here is what I do:

  1. receive the http-response
  2. check the content-encoding (lets assume its gzip)
  3. exctract the content from the http-response ( the result of this point is a byte array that contains the content of the HTTP-Message )
  4. Try to decompress the content-byte-array

Last Point is not working. I get this error: "The magic number in GZip header is not correct. Make sure you are passing in a GZip stream."

Can you tell me what am I doing wrong?

Edit: Since there is no answer yet, I post my decompress-code here:

    public static byte[] Decompress_GZip(byte[] gzip)
    {
        using (GZipStream stream = new GZipStream(new MemoryStream(gzip),
                                   CompressionMode.Decompress))
        {
            byte[] buffer = new byte[size];
            using (MemoryStream memory = new MemoryStream())
            {
                int count = 0;
                do
                {
                    count = stream.Read(buffer, 0, size);
                    if(count>0)
                    {
                        memory.Write(buffer, 0, count);
                    }
                }
                while (count > 0);
                return memory.ToArray();
            }
        }
    }

What I have thought of yet: Might there be any problem with the endianess? Maybe the GZip stream cannot deal with the network byte-order.

Edit: At least I noticed, this error just occurs when receiving chunked-messages. Lets assume a chunked message looks like that:

STATUS-LINE
HEADER_1 /cr/n
...
HEADER_n /cr/n
/cr/n
CHUNK1_SIZE /cr/n
CHUNK1_DATA /cr/n
...
CHUNKn_SIZE /cr/n
CHUNKn_DATA /cr/n
0 /cr/n

Thats what I guess, what I have to do to decompress the message:

  1. Extract CHUNK1_DATA to CHUNKn_DATA (without the /cr/n and without 0, the last chunk)
  2. Concat CHUNK1_DATA ... CHUNKn_DATA to one byte-array
  3. Decompress byte-array with the code above

I'd really appreciate an answer.

Foi útil?

Solução

Well, I just tried myself and it worked as I described it in my Question. If your interested into my code, just commend or write me a message.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top