ASP.NET VERSIONS: ALL
Discover Some ASP.NET Uses for ZIP Compression
By Ken McNamee
ZIP compression has been around for a long time. Most commonly we use this type of compression to facilitate the sharing of large files on the Internet or to save space on our hard drives. Although we don t often think of using ZIP compression in Web development, it does have its uses in this area.
.NET 1.0 and 1.1 do not natively include support for ZIP compression. However, .NET 2.0 will provide this ability with the System.IO.Compression namespace (for more on this visit http://msdn2.microsoft.com/library/system.io.compression.aspx). Until then, there are several compression libraries for .NET that are commercially available, as well as one free, open-source library: #ziplib, or SharpZipLib (http://www.icsharpcode.net/OpenSource/SharpZipLib/). Fortunately, #ziplib and the coming .NET 2.0 namespace use the same compression algorithm and very similarly named classes. So, the code you write now for #ziplib will port very easily to System.IO.Compression and because #ziplib appears to offer more options than .NET 2.0 you may choose to not port at all.
First off, there is nothing proprietary about the ZIP format that #ziplib produces. Because it uses the well-known RFC 1951 Deflate algorithm (http://www.faqs.org/rfcs/rfc1951.html), any of the popular ZIP file managers will be able to open and work with a file that #ziplib creates. Of course, #ziplib will conversely be able to open any ZIP file, as well.
I ve worked on a couple of ASP.NET projects in which database reporting also included a file export in either XML or a character-delimited format. It was originally estimated that the size of these files would only be 20 or 30 kilobytes. Estimates being what they are, eventually the file sizes grew into the megabytes as the frustration of the application s users also grew. One of the methods we employed to lessen this frustration was taking the data and compressing it into a ZIP file. After clicking on the export to file link, instead of the file opening in Excel or an XML viewer, the user s ZIP file manager appeared. Users then had to take the extra step of opening the file from there but this was a small price to pay for a drastically reduced download time, and they were happier because of that.
Figure 1 displays some very simple code that takes some XML data and compresses it using #ziplib. Although I m using some static XML in this example, you can easily see how this could instead be the XML or CSV representation of a DataSet, results from a Web service, or imported from a local file.
public class Default : System.Web.UI.Page
string xml =
"<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
"<Phone>(555) 123-4567</Phone>" +
"<Phone>(555) 234-5678</Phone>" +
override protected void OnLoad(System.EventArgs e)
Response.ContentType = "application/x-zip-compressed";
byte xmlBytes = ASCIIEncoding.ASCII.GetBytes(xml);
MemoryStream memStream = new MemoryStream();
ZipOutputStream zip = new ZipOutputStream(memStream);
zip.Write(xmlBytes, 0, xmlBytes.Length);
Figure 1: The #ziplib code to compress data and send it to the user as a ZIP file is very simple and a precursor to what you can expect with .NET 2.0.
In the Page s OnLoad method, the first thing I do is change Response s ContentType and add an HTTP header that tells the browser that it should be expecting a ZIP file and what the filename is. The next steps are to convert the XML string into an array of bytes and open a MemoryStream object that will contain the compressed bytes. Then I use #ziplib s ZipOutputStream class to encapsulate the MemoryStream, add an entry for the XML file it will contain, and write the compressed bytes to the stream. Notice that you don t explicitly see code that compresses the bytes. This happens implicitly when the bytes are written to the ZipOutputStream. Finally, the last steps are merely to tell the ZipOutputStream that I am finished using it and write the bytes to Response s stream. The next thing the user sees is a dialog box asking them whether to open or save the ZIP file.
The use for #ziplib I just described is not the only one and probably is not even the most important. My first exposure to .NET ZIP compression was when a client complained about the speed of a Smart Client application that was querying a Web service for data that was being returned as plain SOAP XML. My first thought was that the users were not being specific enough with their report queries; unfortunately, I couldn t very well say this. A computer engineering solution was in order, not a social engineering one. So, my solution was to use ZIP compression on the body of the SOAP. A SoapExtension was used to compress the SOAP after it was serialized on the server and decompress it before deserializing it on the Smart Client. You can find a better and more extensive description of this process at http://www.mastercsharp.com/article.aspx?ArticleID=86&&TopicID=7.
Another ingenious use for ZIP compression is to help limit the size of the ASP.NET viewstate without having to sacrifice any of its benefits. If you re not familiar with how viewstate works, it basically involves packaging up the state of every control that has EnableViewState enabled and saving this package in a hidden form tag on the client. When the client submits the form, the server unpacks the viewstate for all the controls that contributed to it. The problem is that viewstate, like report data, can grow very large. Because this data is sent down to the client with each HttpResponse and is sent back to the server with each HttpRequest, it can really slow down the performance of the application, especially on a slow Internet connection.
One easy solution to this viewstate problem is to only enable it for the controls on the page that really need it. However, if you ve already done that and your viewstate is still very large, then you may be ready for ZIP compression. You can find a very good explanation of how to implement this solution at http://www.hanselman.com/blog/ZippingCompressingViewStateInASPNET.aspx.
ZIP compression is probably not something you will use very often as an ASP.NET developer. However, it s one of those tools that, when you need it, you really need it because it can provide a simple solution to a complex problem. Whether you are compressing exported report files, trimming down your viewstate, or trying to improve the perceptive performance of a Smart Client application, ZIP compression can be invaluable. #ziplib may not be as full-featured as some commercial products, but it is a very good middle of the road compression library that is also highly extensible. I ve gotten a lot of mileage out of it and I think you will too.
Ken McNamee is a Senior Software Developer with Vertigo Software, Inc., a leading provider of software development and consulting services on the Microsoft platform. Prior to this, he led a team of developers in re-architecting the Home Shopping Network s e-commerce site, http://www.HSN.com, to 100% ASP.NET with C#. Readers can contact him at [email protected].