How to download a GZIP file from web to Windows Ph

2019-04-13 03:13发布

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.

4条回答
可以哭但决不认输i
2楼-- · 2019-04-13 03:57

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.

查看更多
做自己的国王
3楼-- · 2019-04-13 04:05

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

查看更多
成全新的幸福
4楼-- · 2019-04-13 04:14

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

查看更多
男人必须洒脱
5楼-- · 2019-04-13 04:15

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.

查看更多
登录 后发表回答