The site is currently in read-only mode for maintenance. Posting of new items will be available again shortly.
(For the latest changes, please see the History section below)
In some of my applications, both for web and Windows, I was in the need for an easy way to compress data like single files, multiple files, XML documents etc. For e.g.
DataSets. A common scenario for me was that I wrote a Windows client application that needs to transfer tables and files to a Web server using a Web Service in a bandwidth-saving (and therefore compressed) manner.
Therefore I wrote a little class, that I will present in this article, to make my life a little bit easier. This class has easy to use static methods to compress and decompress various kinds of data.
The class uses the ZIP algorithm to do the actual compressing and decompressing of the data. I did not write the zipping and unzipping routines by myself, but rather used the free library SharpZipLib. So if you are evil, you could say that all I did was "just" to write a wrapper around this ZIP library. But hey - it makes my life easier and may the routines help you a little bit too.
The class contains static routines for compressing and decompressing the kinds of data presented in the following table, with each of them having a pair of compression/decompression routines. (Please note: parameters are omitted here for better readability.)
|Type of data to compress/decompress
|A file system folder with contained files and subfolders.
|Multiple files within one file system folder.
|A single file.
XmlDocument in memory.
string in memory.
DataSet in memory.
byte array in memory.
Each of the compression routine returns a
byte array that you can use to transfer to a destination (e.g. a web server)and then decompress the
byte array there with the correct decompression routine.
Please note: The decompression routines do not know with which compression routine the
byte array was produced. Therefore it is up to you as the programmer, to ensure that the decompression routine only gets a
byte array passed, that was compressed with the corresponding compression routine. E.g. it would be an error to pass the results of a call to
CompressString() to the
DecompressFile() routine. Instead a call to
CompressString() must of course be decompressed by a call to
Example 1 - Compressing and decompressing a string.
The following example shows how to compress a string and then decompress the string again:
string stringToCompress = "Hello world!";
byte buffer =
CompressionHelper.CompressString( stringToCompress );
string decompressedString =
CompressionHelper.DecompressString( buffer );
Example 2 - Compress a folder with multiple files and subfolders.
To compress a folder with multiple files and subfolders, use the methods described in the following example:
byte buffer = CompressionHelper.CompressFolder(
Using the Code in your Projects
The code download for this article contains a small example project as well as precompiled versions (.NET version 1.1, debug and release) of the library. To use it in your own projects you have (at least) two possibilities:
- Simply copy the content of the "Release" folder (namely the files "ICSharpCode.SharpZipLib.dll" and "ZetaCompressionLibrary.dll") to a folder of your choice and add a reference to it by using the Add reference command in Visual Studio .NET 2003.
- Copy and add the source file "CompressionHelper.cs" to your project, so that it gets compiled with your project.
In this article, I've shown you a small class to quick and easy compress and decompress various kinds of data. The code helped me a lot when I was in the need of quick compression and decompression of data without handling all the complexities when doing a compression by hand. Hopefully you'll find this class useful, too.
For questions, comments and remarks, please use the commenting section at the bottom of this article.
- 2005-03-21: Created first version of article.