如何从网络下载 GZIP 文件到 Windows Phone 7 并解压缩内容

发布于 2024-11-02 09:57:18 字数 1341 浏览 0 评论 0 原文

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

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

以下是代码片段:

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 内容、将其写入文件系统并将内容解压缩到文件夹。

I have a requirement to download a zip(or gzip) file from my cloud server to Windows phone 7 file system and unzip the folder contents in the zip.

With the search I did, I could not find a complete solution for this. I used HttpWebRequest to get the binary content but not sure how to proceed further. The native BinaryReader is not available for windows phone and the HttpWebRequest.Headers for Windows Phone 7 does not seem to have the 'Add' API for specifying the encoding type. I also understand that GZipStream is not available for Windows Phone 7.

Following is the code snippet:

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

I am new to c#, and I an trying to replicate my application from Android to Windows phone.

Could you guide me on what StreamReader is required to read the GZip content, write it into the file system and unzip the content to folders.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

忆依然 2024-11-09 09:57:18

进一步大卫的回答。您可以从 SharpZipLib “nofollow">NuGet。

然后使用如下代码。

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

Further to David's answer. You can get SharpZipLib from NuGet.

Then use code like the following.

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

没︽人懂的悲伤 2024-11-09 09:57:18

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

You may have to rely on a third-party component such as SharpZipLib

我的影子我的梦 2024-11-09 09:57:18

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

Thanks for the replies. I was looking for a library with Apache license or similar so that I can use in my market app.
I found this library http://www.sharpgis.net/post/2010/08/25/REALLY-small-unzip-utility-for-Silverlight-e28093-Part-2.aspx and it worked fine.

只怪假的太真实 2024-11-09 09:57:18

我为 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。

I've developed a HTTP class for WP7 which uses 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) );

It can be downloaded here:

https://mytoolkit.codeplex.com/

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

But the Http class needs the GZIP classes (found in Libraries directory) so its best to download the whole source and use the library as DLL.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文