Compression in Silverlight 2: porting SharpZipLib


Share

With a goal to create a Silverlight declarative persistence solution similar to one I recently wrote for ASP.NET WebForms, I quickly discovered that System.IO.Compression no longer contains the GZipStream class we could use to achieve the goal of compressing IsolatedStorage. With a 100kb quota imposed on the browser that can only increase with explicit user permission, it makes sense that we would want to reduce the size of our required state as much as possible to delay or prevent this prompt: it’s an opportunity for both user confusion and support overhead.

Our new goal, then, is to support zip compression in Silverlight so we can compress and decompress our IsolatedStorageFile instances on the client. Since we’re likely to use some kind of string-based serialization to store our data, we can buy a lot of extra space for those strings with a compression algorithm.

I decided to port the excellent and free ICSharpCode.SharpZipLib library that anyone who worked with .NET before 2.0 will remember fondly. With a bit of work I was able to get all of the unit tests to pass, and use it to compress and decompress IsolatedStorage values; you may find other uses for it. All of the original licensing applies to this port, naturally.

A note about the port: Serialization isn’t magically cured from the reflection limitations in Silverlight 2; in this case I’m using the serialization guidelines in my last article to get some tests to pass, which means you’ll need to implement the same pattern if you go the same route. You can always serialize the subset native .NET objects supported with the DataContractJsonSerializer, if you prefer to avoid this (and I don’t blame you). An example of plain JSON serialization is included in the persistence solution that was the end result of these explorations.

Download: ICSharpCode’s.SharpZipLib for Silverlight

Kick It on DotNetKicks.com
blog comments powered by Disqus