【问题标题】:How to download a GZIP file from web to Windows Phone 7 and unzip the contents如何从 Web 下载 GZIP 文件到 Windows Phone 7 并解压缩内容
【发布时间】:2011-04-18 19:34:37
【问题描述】:

我需要从我的云服务器下载一个 zip(或 gzip)文件到 Windows phone 7 文件系统并解压缩 zip 中的文件夹内容。

通过搜索,我找不到完整的解决方案。我使用 HttpWebRequest 来获取二进制内容,但不确定如何进一步进行。本机 BinaryReader 不适用于 Windows Phone,并且 Windows Phone 7 的 HttpWebRequest.Headers 似乎没有用于指定编码类型的“添加”API。我也了解到 GZipStream 不适用于 Windows Phone 7。

下面是sn-p的代码:

private void btnReadUrl_Click(object sender, RoutedEventArgs e)
    {
        System.Uri targetUri = new System.Uri("http://cloud/images.gz");
        HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(targetUri);
        request.BeginGetResponse(new AsyncCallback(ReadWebRequestCallback), request);
    }

private void ReadWebRequestCallback(IAsyncResult callbackResult)
    {
        HttpWebRequest myRequest = (HttpWebRequest)callbackResult.AsyncState;
        HttpWebResponse myResponse = (HttpWebResponse)myRequest.EndGetResponse(callbackResult);
        using (StreamReader httpwebStreamReader = new StreamReader(myResponse.GetResponseStream()))
        {
            string results = httpwebStreamReader.ReadToEnd();
            //TextBlockResults.Text = results; //-- on another thread!
            Dispatcher.BeginInvoke(() => txtResult.Text = results);
        }
    }

我是 C# 新手,我正在尝试将我的应用程序从 Android 复制到 Windows 手机。

您能否指导我阅读 GZip 内容、将其写入文件系统并将内容解压缩到文件夹中需要什么 StreamReader。

【问题讨论】:

    标签: windows-phone-7 httpwebrequest gzip


    【解决方案1】:

    进一步大卫的回答。您可以从NuGet 获取SharpZipLib

    然后使用如下代码。

    string data = "";
    var stream = new GZipInputStream(response.GetResponseStream());
    using (StreamReader reader = new StreamReader(stream)) {
        data = reader.ReadToEnd();
    }

    【讨论】:

      【解决方案2】:

      您可能不得不依赖第三方组件,例如SharpZipLib

      【讨论】:

        【解决方案3】:

        感谢您的回复。我正在寻找一个具有 Apache 许可证或类似许可证的库,以便我可以在我的市场应用程序中使用。 我找到了这个库http://www.sharpgis.net/post/2010/08/25/REALLY-small-unzip-utility-for-Silverlight-e28093-Part-2.aspx,它运行良好。

        【讨论】:

        • @tempy - SharpZipLib 许可证实际上是 GNU with an exception,它允许包含在商业应用程序中如果使用二进制文件(而不是源文件)。
        【解决方案4】:

        我为 WP7 开发了一个 HTTP 类,它使用 DotNetZip (http://dotnetzip.codeplex.com/)。

        var request = new HttpGetRequest("http://www.server.com");
        request.RequestGZIP = true; // default
        Http.Get(request, (s, e) => MessageBox.Show(s) );
        

        可以在这里下载:

        https://mytoolkit.codeplex.com/

        https://mytoolkit.svn.codeplex.com/svn/Network/

        但是Http 类需要GZIP 类(在Libraries 目录中),所以最好下载整个源代码并将库用作DLL。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-02-27
          • 1970-01-01
          • 2012-03-22
          • 2010-09-06
          • 2013-12-15
          • 1970-01-01
          • 2017-02-01
          • 1970-01-01
          相关资源
          最近更新 更多