2

I'm getting string result from web service and parsing it with the following code:

 public static T FromXmlString<T>(string xml)
    {
        T xmlClass = default(T);


            using (TextReader reader = new StringReader(xml))
            {
                try
                {
                    xmlClass =
                        (T)new XmlSerializer(typeof(T)).Deserialize(reader);
                }
                catch (InvalidOperationException e)
                {
                    //
                }
            }


        return xmlClass;
    }

In the result there is compressed string , after parsing I try to decompress it with the following code

  byte[] bytes = Convert.FromBase64String(voucher.Document.Value);
              using (var compressedStream = new MemoryStream(bytes))
  using (var zipStream = new GZipStream(compressedStream, CompressionMode.Decompress))
  using (var resultStream = new MemoryStream())
        {
            zipStream.CopyTo(resultStream);
            return File(resultStream.ToArray(), "application/pdf", "voucher.pdf");
        }

But I fail every time. It throws the below error: "The magic number in GZip header is not correct. Make sure you are passing in a GZip stream."

I know there are a lot of questions similar to this. I tried every answers in the following links:

Compression/Decompression string with C#

Error decompressing gzipstream -- The magic number in GZip header is not correct

https://social.msdn.microsoft.com/Forums/vstudio/en-US/21901efe-8d36-40ed-9dad-2ce9968b4273/the-magic-number-in-gzip-header-is-not-correct-error?forum=csharpgeneral

But did not get any result. So guys, do you have any idea why do I get this error. What is wrong with my code?

Thanks in advance!

Nigar Alizade
  • 369
  • 3
  • 15
  • How does the base64 string start? It should be fairly easy to tell whether it really represents gzip... – Jon Skeet Nov 26 '18 at 14:11
  • @JonSkeet "eJy0/WOQbF22BoyWbZu7bNu2bdu27apddtUu27Zt27b9vd19ug+/..." like that – Nigar Alizade Nov 26 '18 at 14:16
  • Right, well that starts with bytes 78-9C-B4-FD-63-90-6C-5D... that doesn't look like [gzip](https://en.wikipedia.org/wiki/Gzip) to me. Perhaps it's Deflate rather than GZip? – Jon Skeet Nov 26 '18 at 14:38
  • @JonSkeet could please, clarify, what do you mean with deflate? – Nigar Alizade Nov 27 '18 at 12:37
  • Deflate is another compression scheme. You'd use `DeflateStream` instead of `GZipStream`. – Jon Skeet Nov 27 '18 at 12:50
  • @JonSkeet now I get this error: "Block length does not match with its complement." – Nigar Alizade Nov 27 '18 at 13:04
  • That I don't know about. But the first thing you need to find out reliably is what it *is* meant to be. We have no idea where you're getting the data from, but presumably you do - so you should consult the documentation for that. – Jon Skeet Nov 27 '18 at 13:34
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/184326/discussion-between-nigar-alizade-and-jon-skeet). – Nigar Alizade Nov 27 '18 at 13:38

1 Answers1

0

I found out the problem. After researching more, I found this:

Why does my C# gzip produce a larger file than Fiddler or PHP?

And I used Ionic.Zlib libray. With using that I can decompress and get the original file without any problem.

Here is my code snippet:

     byte[] bytes = Convert.FromBase64String(voucher.Document.Value);
     var zippedStream = new MemoryStream(bytes);
     var decompressed = new MemoryStream();
     zOut = new ZlibStream(decompressed, Ionic.Zlib.CompressionMode.Decompress, 
true);
      CopyStream(zippedStream, zOut);
      byte[] byteArray = decompressed.ToArray();
      return File(byteArray, "application/pdf", "voucher.pdf");

CopyStream function:

 static void CopyStream(System.IO.Stream src, System.IO.Stream dest)
    {
        byte[] buffer = new byte[1024];
        int len;
        while ((len = src.Read(buffer, 0, buffer.Length)) > 0)
            dest.Write(buffer, 0, len);
        dest.Flush();
    }
Nigar Alizade
  • 369
  • 3
  • 15