Xaml and System.IO.Compression: Bosom Buddies

Once you get past the fact that the word “Bosom” is in the title of this post (does anyone still use this word?) you’ll be able to see that I’m quite pleased with the fact that .NET 2.0 introduced stream compression as part of the framework – finally!

System.IO.Compression introduces a couple of classes:  DeflateStream, and GZipStream.  I’ve been using GZipStream to compress the serialized content of my application, with great results.

Before I continue, a few notes.  It’s been said before, but it’s important to note the distinction between file and stream compression.  In short, if you intend to compress multiple files into an archive, these classes may not be for you.  It is certainly possible to achieve this type of functionality, but that is not what these classes were designed for.  Since these classes are designed to operate on streams, the amount of prediction they can do is limited, which means that the eventual compression will likely be less than what can be achieved with a file compression utility.  At any rate, the most important thing from my perspective is the fact that compression is now part of the framework not that it provides the best possible compression available.  The fact that it operates on streams comes in very handy in the .NET world.

Xaml and GZipStream work together well because of the characteristics of XML.  In essence, the XML format includes a tremendous amount of statistical redundancy (think about the repetition involved in opening and closing tags, etc) which is great from a compression perspective.  In general, the more repetition in your content, the better compression you’ll be able to achieve.  For some background on how compression works, a decent place to start is the data compression wiki.

I’ve been very satisfied with the results of my initial tests – in most cases, I’m achieving at least an 80% compression rate, at a very acceptable performance cost.  In many cases, this allows me to achieve file sizes which are smaller than their original sizes in their binary CAD format. 

So, whether your serializing WPF objects to Xaml, or simply saving XML, System.IO.Compression could be very useful in any situation where size matters.  For information on how to use the classes in System.IO.Compression, developer.com has a short primer that should be enough to get you started.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: