3

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?

AnthonyWJones
  • 187,081
  • 35
  • 232
  • 306
TCM
  • 16,780
  • 43
  • 156
  • 254

3 Answers3

7

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.

Chris Johnson
  • 1,230
  • 7
  • 15
  • 1
    I know this method. However my zip contains folder that I am not aware of. – TCM Apr 27 '11 at 02:20
  • Another limitation of this method: I believe (but haven't confirmed) that the GetResourceStream() method creates an internal MemoryStream (loading the entire file into memory). I'm getting OutOfMemoryExceptions when using GetResourceStreawm() with large .zip files on WP8, but small files work fine. – Jay Borseth Feb 27 '13 at 08:28
4

Check out this utility, it may help you out.

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

wilbev
  • 5,391
  • 7
  • 28
  • 30
  • 1
    +1: You might also want to look at this update to that blog (the link to the update in the blog is broken) http://www.sharpgis.net/2010/08/default.aspx – AnthonyWJones Apr 26 '11 at 07:37
1

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

Stuart
  • 66,722
  • 7
  • 114
  • 165
  • slsharpziplib is pathetic. I tried for whole day and couldn't get it to work. All I get was some sort of exception when CreateZip method of IsolatedFastZip() was being called. wilbev's suggestion is the best when all you want to do is unzip the contents without wasting too much of time reading documentation and APIs. The class is really simple and does the job quiet well. – TCM Apr 26 '11 at 18:23
  • Sorry - but useful - to hear you've had so many problems. ICSharpZipLib is an "old friend" - I've used it in "tens" of projects - but admittedly my code is mostly about "in memory" decompression Thanks for the feedback - if I find myself needing "disk-based" compression then I'll look at the other project and save myself time. – Stuart Apr 27 '11 at 14:34