The following forum(s) are migrating to a new home on Microsoft Q&A (Preview): Developing Universal Windows apps!

Ask new questions on Microsoft Q&A (Preview).
Interact with existing posts until December 13, 2019, after which content will be closed to all new and existing posts.

Learn More

[UWP] usage of GZip RRS feed

  • Question

  • Hey,

    to minimize data usage from my Apps, I want to implement some compression from Server to Client.

    I have a PHP script, that gives me a compressed (zlib) string:

    $gzdata = gzcompress($myString, 6);
    echo base64_encode($gzdata);

    now I want to decompress the data on my UWP App. Following this example gives me an Error

    public static string UnZip(string compressedText)
            byte[] gzBuffer = Convert.FromBase64String(compressedText);
            using (MemoryStream ms = new MemoryStream())
                int msgLength = BitConverter.ToInt32(gzBuffer, 0);
                ms.Write(gzBuffer, 4, gzBuffer.Length - 4);

                byte[] buffer = new byte[msgLength];

                ms.Position = 0;
                using (System.IO.Compression.GZipStream zip = new System.IO.Compression.GZipStream(ms, System.IO.Compression.CompressionMode.Decompress))
                    zip.Read(buffer, 0, buffer.Length);

                return System.Text.Encoding.Unicode.GetString(buffer, 0, buffer.Length);


    int msgLength = BitConverter.ToInt32(gzBuffer, 0)

    is negative.

    However I played a little bit around and came up with this code:

    using (var res = GenerateStreamFromString(myResult))
        using (GZipStream decompressionStream = new GZipStream(res, CompressionMode.Decompress))
            using (var sr = new StreamReader(decompressionStream))
                var k = sr.ReadToEnd();

    which again ends in an error: Exception thrown: 'System.IO.InvalidDataException' in System.IO.Compression.dll.

    Looking at it a bit closer, VS tells me in the StreamReader sr the following:

    base = {System.IO.InvalidDataException: The magic number in GZip header is not correct. Make sure you are passing in a GZip stream.
       at System.IO.Compression.GZipDecoder.ReadHeader(InputBuffer input)
       at System.IO.Compression.Inflater.Decode()
       at System....

    But the documentation of gzcompress states, that it does include gzip headers, so the magic number should be correct.

    What can I do to fix this?

    Saturday, September 5, 2015 10:01 AM

All replies