UPDATE: See this updated blogpost.
There are quite a few libraries out there that adds zip decompression/compression to Silverlight. However, common to them all is that they add significantly to the size of the resulting .xap.
It turns out that Silverlight 2.0 already has zip decompression built-in. It uses this to uncompress the .xap files which really just are zip files with a different file extension.
There are several blog posts out there that will tell you how to dynamically load a XAP file and load it. It turns out that if you use the same approach with almost any other zip file, you can actually do the same thing, even though this is not a Silverlight XAP. I don’t think this was the original intent. but its still really neat! Here’s how to accomplish that, based on a zip file stream:
public static Stream GetFileStream(string filename, Stream stream)
{
Uri fileUri = new Uri(filename, UriKind.Relative);
StreamResourceInfo info = new StreamResourceInfo(stream, null);
StreamResourceInfo streamInfo = System.Windows.Application.GetResourceStream(info, fileUri);
if (streamInfo != null)
return streamInfo.Stream;
return null; //Filename not found or invalid ZIP stream
}
However, the problem is that this requires you to know before-hand what the names of the files are inside the zip file, and Silverlight doesn’t give you any way of getting that information (Silverlight uses the manifest file for reading the .xap).
Luckily getting filenames from the zip is the easy part of the ZIP specification to understand. This enabled us to create a generic ZIP file extractor in very few lines of code. Below is a small class utility class I created that wraps this all nicely for you.
public class UnZipper
{
private Stream stream;
public UnZipper(Stream zipFileStream)
{
this.stream = zipFileStream;
}
public Stream GetFileStream(string filename)
{
Uri fileUri = new Uri(filename, UriKind.Relative);
StreamResourceInfo info = new StreamResourceInfo(this.stream, null);
StreamResourceInfo stream = System.Windows.Application.GetResourceStream(info, fileUri);
if(stream!=null)
return stream.Stream;
return null;
}
public IEnumerable<string> GetFileNamesInZip()
{
BinaryReader reader = new BinaryReader(stream);
stream.Seek(0, SeekOrigin.Begin);
string name = null;
while (ParseFileHeader(reader, out name))
{
yield return name;
}
}
private static bool ParseFileHeader(BinaryReader reader, out string filename)
{
filename = null;
if (reader.BaseStream.Position < reader.BaseStream.Length)
{
int headerSignature = reader.ReadInt32();
if (headerSignature == 67324752) //PKZIP
{
reader.BaseStream.Seek(14, SeekOrigin.Current); //ignore unneeded values
int compressedSize = reader.ReadInt32();
int unCompressedSize = reader.ReadInt32();
short fileNameLenght = reader.ReadInt16();
short extraFieldLenght = reader.ReadInt16();
filename = new string(reader.ReadChars(fileNameLenght));
if (string.IsNullOrEmpty(filename))
return false;
//Seek to the next file header
reader.BaseStream.Seek(extraFieldLenght + compressedSize, SeekOrigin.Current);
if (unCompressedSize == 0) //Directory or not supported. Skip it
return ParseFileHeader(reader, out filename);
else
return true;
}
}
return false;
}
}
Basically you create a new instance of the UnZipper parsing in the stream to the zip file. The method “GetFileNamesInZip” will provide you with a list of the file names available inside the file, that you can use to reference the file using “GetFileStream”.
Below is a simple example of using this. The contents of each file will be shown in a message box:
private void LoadZipfile()
{
WebClient c = new WebClient();
c.OpenReadCompleted += new OpenReadCompletedEventHandler(openReadCompleted);
c.OpenReadAsync(new Uri("http://www.mydomain.com/myZipFile.zip"));
}
private void openReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
UnZipper unzip = new UnZipper(e.Result);
foreach (string filename in unzip.GetFileNamesInZip())
{
Stream stream = unzip.GetFileStream(filename);
StreamReader reader = new StreamReader(stream);
string contents = reader.ReadToEnd();
MessageBox.Show(contents);
}
}
Note that some ZIP files which doesn't report file size before the file content is not supported by Silverlight, and is therefore also ignored by this class. This is sometimes the case when the zip file is created through a stream where the resulting file size is written after the compressed data. If you are dealing with those kind of zip files (seems fairly rare to me), you will need to use a 3rd party zip library that supports this.
UPDATE: See this updated blogpost.
This class will hardly add much to your resulting .xap, and I think it will cover 95% of the use cases when working with zip files.
Download class file: UnZipper.zip (1.25 kb)