[UWP]使用GZip [英] [UWP] usage of GZip

查看:128
本文介绍了[UWP]使用GZip的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,


为了最大限度地减少我的应用程序的数据使用量,我想实现从服务器到客户端的一些压缩。


I有一个PHP脚本,它给我一个压缩(zlib)字符串:

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

现在我想解压缩我的UWP App上的数据。关注此
示例
给我一个错误

公共静态字符串UnZip(string compressedText)
   ;  {
        byte [] gzBuffer = Convert.FromBase64String(compressedText);
       使用(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);
        }
    }

as

 int msgLength = BitConverter.ToInt32(gzBuffer,0)

是否定的。


然而我玩了一下并提出了这个代码:

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

再次以错误结束:抛出异常:'System.IO.InvalidDataException'in System.IO.Compression.dll


再看一下,VS在 StreamReader sr 中告诉我以下内容:


base = {System.IO.InvalidDataException:GZip标头中的幻数不正确。确保您传入的是GZip流。

  在System.IO.Compression.GZipDecoder.ReadHeader(InputBuffer输入)

  在System.IO.Compression.Inflater.Decode()

  在系统....


文档gzcompress 表示,它确实包含gzip标题,所以幻数应该是正确的。


我该怎么做才能解决这个问题?

解决方案

您可以将HttpClient设置为在下载时自动解压缩字符串。



http:// blogs .msdn.com / b / DOTNET /存档/ 2013/06/06 /便携式压缩和 - 的HttpClient-工薪together.aspx


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);
        }
    }

as

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?

解决方案

You can set the HttpClient to automatically decompress the string when it is downloaded.

http://blogs.msdn.com/b/dotnet/archive/2013/06/06/portable-compression-and-httpclient-working-together.aspx


这篇关于[UWP]使用GZip的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆