Sunday, March 23, 2008

Unpacking ZIP files in Silverlight 2

One of the nice features in Silverlight 1.1 alpha was, in my opinion, the use of zipfiles and being able to use files from a zipped file by using the Downloader object.

In Silverlight 2 the Dowloader is replaced with the WebClient. Which is not a bad thing, but it seems to be missing the ability to use zipfiles. Or I've missed it somehow...

Anyway, I stil wanted the thumbnails on my site to be downloaded using a ZIP file simply because it makes it smaller and easier to handle.

In the past I've been using ICSharpZipLib so I figured I'd give that a try.
After adding ICSharpZipLib project to my Silverlight project I had to change a few things, mostly changing ArrayLists to generic Lists and a few minor other thing. My SharpZipLib project is available for download (bottom of this article) but be sure to test it thoroughly, I've only used a small part of the library.

How does it work

It was quite easy to implement this:

ICSharpZipLib contains a ZipInputStream which converts an existing Stream to a ZipInputStream.

The e.Result property (Stream) of the OpenReadCompletedEventArgs can be used in the constructor of the ZipInputStream class.

After creating the ZipInputStream, a call of the GetNextEntry method will return a ZipEntry (zippedFile). So we need to create a loop to get through all of the files in the ZipInputStream:

codesnippet1

codesnippet2

In between these 2 snippets is the code for handling the files.
A MemoryStream is created for reading the buffer of the outputStream of the zippedFile.

Handling the MemoryStream for each filetype

The extension of the file in the zip is used by the switch to determine which method is called for handling the MemoryStream:

codesnippet3

Inside these functions the stream is used as a parameter for a SetSource of a BitmapImage in case of an image filetype:

codesnippet4

or as a parameter for a StreamReader in case of a text file:
codesnippet5

or as the stream parameter for the SetSource of a MediaElement:

codesnippet6 

The last one, however, crashes IE. Not sure why this happens. When debugging it seems to work fine, but it crashes when playing with a System.CrossAppDomainMarshaledException (crossdomain error) even when using a clientaccesspolicy.xml or a crossdomain.xml.

 

Click here to download SharpZibLib for Silverlight

Click here to download the entire solution

4 comments:

  1. Hi,

    You can still access the content of the downloaded zip file, but the API changed. You must now use the method Application.GetResourceStream, which is able to read into a Zip file. This is a good move, because it's compatible with .NET 3.5, while the Downloader object was not.

    I published an example (related to a blog article I am currently writing) here:

    Note that to save space, I deleted the Zip file, available separately here:

    Test the application

    HTH,
    Laurent

    ReplyDelete
  2. Here's a way to do it without using a 3rd party plugin that will bloat your XAP: http://www.sharpgis.net/post/2009/04/21/REALLY-small-unzip-utility-for-Silverlight.aspx

    ReplyDelete
  3. For recover zip files I usually use different tools.But once I had very serious issue and all of it couldn't help.And I found-check corrupt zip file,this tool solved my problem in two minutes and free of cost.In addition applciation could repair corrupted archives with *.zip extension.

    ReplyDelete
  4. Yesterday my friend asked me that What I usually do with corrupted zip files.And I answered to him.Usually nothing)) But lately I found on a soft blog-repair corrupt zip and it solved like problems very easy and free of cost.Moreover tool can demonstrates how it open your file with *.zip extension or SFX self-extracting archive and analyze compressed documents.

    ReplyDelete