Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

A small set of routines for compressing and decompressing various types of data

0.00/5 (No votes)
25 Mar 2005 1  
A class with multiple routines for quick and easy compression and decompression of various types of data.

Example image of a folder structure that can be compressed and decompressed using the routines of the class described in this article

Introduction

(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.

Background

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 Compression routine Decompression routine
A file system folder with contained files and subfolders. CompressFolder() DecompressFolder()
Multiple files within one file system folder. CompressFiles() DecompressFiles()
A single file. CompressFile() DecompressFile()
An XmlDocument in memory. CompressXmlDocument() DecompressXmlDocument()
A string in memory. CompressString() DecompressString()
A DataSet in memory. CompressDataSet() DecompressDataSet()
A byte[] array in memory. CompressBytes() DecompressBytes()

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 DecompressString().

Examples

Example 1 - Compressing and decompressing a string.

The following example shows how to compress a string and then decompress the string again:

// Compress a string.
string stringToCompress = "Hello world!";
byte[] buffer = 
    CompressionHelper.CompressString( stringToCompress );
 
...
 
// Decompress.
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:

// Compress multiple files and subfolders.
byte[] buffer = CompressionHelper.CompressFolder(
    @"c:\MyInputFolder" );
  
...// Decompress multiple files and subfolders.
CompressionHelper.DecompressFolder(
    buffer,
    @"c:\MyOutputFolder" );

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:

  1. 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.
  2. Copy and add the source file "CompressionHelper.cs" to your project, so that it gets compiled with your project.

Conclusion

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.

History

  • 2005-03-21: Created first version of article.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here