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.
Decompress a gzip-compressed HTTP-Response (chunked encoding)
-
08-12-2021 - |
Pregunta
like descriped in the title, I want to decompress a HTTP-Resonse. Here is what I do:
- receive the http-response
- check the content-encoding (lets assume its gzip)
- exctract the content from the http-response ( the result of this point is a byte array that contains the content of the HTTP-Message )
- 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:
- Extract
CHUNK1_DATA
toCHUNKn_DATA
(without the/cr/n
and without0
, the last chunk) - Concat
CHUNK1_DATA
...CHUNKn_DATA
to onebyte-array
- Decompress
byte-array
with the code above
I'd really appreciate an answer.
Solución
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow