0

I'm connecting to a REST API that returns a zip file with a small size (10k) with an XML file inside. When I copy it to a MemoryString and then Decompress it, I get the following error:

System.IO.InvalidDataException: 'The magic number in GZip header is not correct. Make sure you are passing in a GZip stream.'

Obs: When I write it directly on the disk everything goes well. Why can't I do Decompress? Thank you.

public byte[] makeGET()
{
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(endpoint);
    request.Method = httpMethod.ToString();
    request.Headers.Add("Authorization", "Basic " + authHeader);
    request.Accept = "application/zip";


    using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
    {
        if (response.StatusCode != HttpStatusCode.OK)
        {
            throw new ApplicationException("Error:" + response.StatusCode);
        }

        using (Stream responseStream = response.GetResponseStream())
        {
            if (responseStream != null)
            {

                /*using (Stream s = File.Create(@"c:\test.zip"))
                {
                    responseStream.CopyTo(s);
                }*/             

                using (MemoryStream msIn = new MemoryStream())
                {
                    responseStream.CopyTo(msIn);
                    return msIn.ToArray();                      

                }



            }
        }
    }
    return null;            

}



public byte[] Decompress (byte[] msIn)
{
    byte[] output;

    MemoryStream tmp = new MemoryStream(msIn);

    using (MemoryStream msOut = new MemoryStream())
    {
        using (var gzip = new GZipStream(tmp, CompressionMode.Decompress))
        {
            gzip.CopyTo(msOut);
        }

        output = msOut.ToArray();

    }
        return output;

}
  • Are you sure the response is a gzip archive, and not a zip archive? `application/zip` is normally zip, not gzip. – canton7 May 18 '20 at 09:31
  • The response is a zip...I can't use GzipStream? – Johnny Santos May 18 '20 at 09:39
  • No, GzipStream is gzip, not zip. You probably want [ZipArchive](https://learn.microsoft.com/en-us/dotnet/api/system.io.compression.ziparchive?view=netcore-3.1). Note that zip files are archives -- they contain potentially multiple files and directories, so you'll have to do a bit more work to extract the parts you want. – canton7 May 18 '20 at 09:41
  • Ok, thanks..I will try with ZipArchive – Johnny Santos May 18 '20 at 09:47
  • My project is .NET Framework...ZiArchive is.NET Core...there is something for zip files in .net framework? – Johnny Santos May 18 '20 at 10:01
  • It is in .NET Framework -- see the bottom of the page I linked, it goes back to .NET Framework 4.5. It's in `System.IO.Compression.dll` (see the top of [this page](https://learn.microsoft.com/en-us/dotnet/api/system.io.compression.ziparchive?view=netframework-4.5)), which might not be referenced by default: try adding a reference to it. – canton7 May 18 '20 at 10:04

0 Answers0