关于httpwebrequest:如何将GZIP文件从Web下载到Windows Phone 7并解压缩内容

How to download a GZIP file from web to Windows Phone 7 and unzip the contents

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

经过搜索,我找不到完整的解决方案。 我使用HttpWebRequest来获取二进制内容,但不确定如何继续进行操作。 Windows BinaryReader不适用于Windows Phone,Windows Phone 7的HttpWebRequest.Headers似乎没有用于指定编码类型的"添加" API。 我也了解GZipStream不适用于Windows Phone 7。

以下是代码段:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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 Phone。

您能否指导我了解什么是StreamReader才能读取GZip内容,将其写入文件系统并将内容解压缩到文件夹。


进一步回答David的问题。 您可以从NuGet获取SharpZipLib。

然后使用如下代码。

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


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

1
2
3
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。


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


您可能必须依赖第三方组件,例如SharpZipLib