REALLY small unzip utility for Silverlight – Part 2

by Morten 25. August 2010 20:50

I earlier blogged about a very small unzip utility for Silverlight.

However there was one problem with the method: It relied on some Silverlight framework libraries that didn’t support all zip file formats (a known bug I’m told). Some file zippers places the file size after the zip data chunk, and Silverlight does not like that. So I added a little bit to the utility to detect this case and if so, will re-arrange the bytes in the zipfile so Silverlight will read it.

So I present to you: The really small unzip utility v.2.0. It’s slightly larger than before, but still under 200 lines of code (plus comments).

It works the same way as before, so read the original blogpost to see how to use it.

Although I only tested this on Silverlight 4 and WinPhone 7, it should work with earlier Silverlight releases as well.

Download here: SharpGIS.UnZipper_v2.zip (2.53 kb)

Also available at NuGet

Tags:

Silverlight

Comments (8) -

nicogis
nicogis Italy
8/29/2010 9:44:23 AM #

What do you think: http://slsharpziplib.codeplex.com/ ?

Ciao

Morten
Morten United States
8/30/2010 9:44:51 PM #

nicogis: slsharpziplib is fine if you want to add 200kb to your solution instead of 200 lines of code Smile
If you want to do compression, something like this would be required though. Unzip just happens to be much simpler, because Silverlight has all the important bits.

greg
greg United States
8/31/2010 4:16:23 PM #

I used the original 1.0 version and didn't have any problems with it. I did however modify it to support my companies custom zip headers.

Dan Walton
Dan Walton United States
9/20/2010 10:51:45 AM #

Morten,
Very helpful. Thanks again. One thing I miss from SharpZipLib is this method:

        public static bool IsStreamCompressed(Stream inputStream)
        {
            bool isCompressed = false;
            using (var reader = new BinaryReader(inputStream))
            {
                inputStream.Seek(0, SeekOrigin.Begin);
                if (reader.BaseStream.Position < reader.BaseStream.Length)
                {
                    if (reader.ReadInt32() == 67324752)
                        isCompressed = true;
                    inputStream.Seek(0, SeekOrigin.Begin);
                }
            }
            return isCompressed;
        }

Eric
Eric United States
10/6/2010 5:45:45 AM #

This version does not have the function "GetFileNamesInZip()". Any particular reason for that?
Or do I need to merge version 1 and 2 files ?

Thanks.

Morten
Morten United States
10/11/2010 2:03:27 PM #

Eric: I did some refactoring. You can use the property: IEnumerable<string> FileNamesInZip { get; }

ionman
ionman United States
11/21/2010 11:43:32 AM #

When I add this code file to my WPF application I get an error on line 68 saying "No overload for method 'GetResourceStream' takes 2 arguments". If I modify this line to remove the extra StreamResourceInfo, that line always throws an exception. Do I need to modify my project to make this work, somehow?

Morten
Morten United States
11/23/2010 12:58:35 PM #

ionman: This code is for Silverlight. It uses API's not available in WPF.

Pingbacks and trackbacks (1)+

Comments are closed

About the author

Morten Nielsen

Silverlight MVP

Morten Nielsen
<--That's me
E-mail me Send mail

Twitter @dotMorten 

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2005-2014

Month List

Recent Comments

Comment RSS