Click here to Skip to main content
Click here to Skip to main content
Go to top

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

, 25 Mar 2005
Rate this:
Please Sign up or sign in to vote.
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Uwe Keim
Chief Technology Officer Zeta Producer Desktop CMS
Germany Germany
Uwe does programming since 1989 with experiences in Assembler, C++, MFC and lots of web- and database stuff and now uses ASP.NET and C# extensively, too. He has also teached programming to students at the local university.
 
In his free time, he does climbing, running and mountain biking. Recently he became a father of a cute boy.
 
Some cool, free software from us:
 
Free Test Management Software - Intuitive, competitive, Test Plans. Download now!  
Homepage erstellen - Intuitive, very easy to use. Download now!  
Send large Files online for free by Email
Offline-Homepage-Baukasten

Comments and Discussions

 
GeneralDecompress Folder error Pinmemberdnufer25-Oct-10 1:31 
GeneralDecompression problem with empty directories PinmemberMember 192855320-Oct-10 4:51 
GeneralRe: Decompression problem with empty directories PinmvpUwe Keim20-Oct-10 18:15 
GeneralCheers PinmemberMember 220722916-Mar-10 17:38 
GeneralThank you!! Pinmemberdemkafas7-Mar-10 15:26 
QuestionHow to copy folder? Pinmemberdrsagitn20-Dec-08 4:02 
AnswerRe: How to copy folder? Pinmemberdrsagitn20-Dec-08 6:09 
GeneralIt worked for me for dataset !!! PinmemberPrash20-Nov-08 1:48 
Generalzip-Compress-Versions PinmemberCortadillo21-Aug-08 20:42 
Generalproblem with directories Pinmemberpepepaco30-Jun-08 5:12 
GeneralRe: problem with directories Pinmemberpepepaco30-Jun-08 5:32 
GeneralRe: problem with directories Pinmemberpepepaco30-Jun-08 7:48 
GeneralRe: problem with directories PinmemberCortadillo21-Aug-08 20:53 
General.Net compact framework PinmemberMember #375617223-Jan-07 19:26 
GeneralRe: .Net compact framework PinsitebuilderUwe Keim23-Jan-07 19:58 
GeneralRe: .Net compact framework PinmemberDaveChad124-Jan-07 12:37 
GeneralIts not working PinmemberMember #28700283-Jan-07 17:14 
GeneralRe: Its not working PinsitebuilderUwe Keim3-Jan-07 18:22 
GeneralMaybe it will be better? Pinmemberchanaev8-Apr-08 17:11 
GeneralRe: Maybe it will be better? Pinmembershanyg24-May-08 0:37 
Generalwhy doesn't it work it XML formatted String Pinmemberanj198315-Aug-06 0:21 
GeneralQuestion PinmemberStrattonN12-Jul-06 17:42 
Questionbzip2 or gzip wrapper ? Pinmemberhungpvtn13-Jan-06 13:40 
AnswerRe: bzip2 or gzip wrapper ? Pinmembereclipse2k111-Jul-06 23:41 
GeneralCant Decompress String PinmemberKashif Ibrahim29-Dec-05 23:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.140916.1 | Last Updated 26 Mar 2005
Article Copyright 2005 by Uwe Keim
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid