开发者

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

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 Binary开发者_开发百科Reader 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.


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


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


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜