How do I extract zip file in Windows Phone 7?

TCM picture TCM · Apr 26, 2011 · Viewed 12k times · Source

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?

Answer

Chris Johnson picture Chris Johnson · Apr 26, 2011

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.