How do I extract zip file in Windows Phone 7?

2019-05-10 02:46发布

问题:

I have a zip file in my Windows Phone 7 project. I have set the Build Action to Content and Copy to output directory to Always. The zip file contains the folder structure. I want this to be copied entirely as it is in my Phone Project. I am using SharpZipLib for this. This is the code :-

 Stream stremInfo = Application.GetResourceStream(new Uri("xip.zip", UriKind.Relative)).Stream;



        new FastZip(). ExtractZip(stremInfo,
            "",FastZip.Overwrite.Always,null,null,null,true,true);

However I get error when ExractZip is called. The exception I get is "MethodAccessException". Cannot call GetFullPath(). Can anybody let me know what am I missing? What can I do to avoid it?

回答1:

Check out this utility, it may help you out.

http://www.sharpgis.net/post/2009/04/22/REALLY-small-unzip-utility-for-Silverlight.aspx



回答2:

You dont need to use another library if you know what files you want out of the Zip. You can use the App.GetResourceStream phone API to reach into the Zip and get the file.

void MainPage_Loaded(object sender, RoutedEventArgs e)
{
    WebClient client = new WebClient();
    client.OpenReadCompleted += new OpenReadCompletedEventHandler(client_OpenReadCompleted);
    client.OpenReadAsync(new Uri("http://www.foo.com/pictures.zip"));
}

void client_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
    StreamResourceInfo info = new StreamResourceInfo(e.Result,"");
    StreamResourceInfo pic = App.GetResourceStream(info, new Uri("IMG_1001.jpg", UriKind.Relative));

    BitmapImage bitmap = new BitmapImage();
    bitmap.SetSource(pic.Stream);
    img.Source = bitmap;
}

For more informaiton on reading the list of files from th Zip check out this blog post.



回答3:

I've used the SL port of the SharpZipLib to do this - see http://slsharpziplib.codeplex.com/

There's lots of example code available for how to use it - and a good quickstart in their source - http://slsharpziplib.codeplex.com/SourceControl/changeset/view/75568#1416103