Click here to Skip to main content
15,114,081 members
Articles / Programming Languages / C#
Posted 20 Mar 2005


61 bookmarked

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

Rate me:
Please Sign up or sign in to vote.
4.67/5 (19 votes)
25 Mar 2005CPOL3 min read
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


(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/decompressCompression routineDecompression 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().


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.
    @"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.


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.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


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:

Deutschsprachiges Forum zu Elektromobilität und Tesla  
Windows 10 Ereignisanzeige  
German Developer Community  
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

Comments and Discussions

GeneralDecompress Folder error Pin
dnufer25-Oct-10 2:31
Memberdnufer25-Oct-10 2:31 
GeneralDecompression problem with empty directories Pin
Member 192855320-Oct-10 5:51
MemberMember 192855320-Oct-10 5:51 
GeneralRe: Decompression problem with empty directories Pin
Uwe Keim20-Oct-10 19:15
sitebuilderUwe Keim20-Oct-10 19:15 
GeneralCheers Pin
Member 220722916-Mar-10 18:38
MemberMember 220722916-Mar-10 18:38 
GeneralThank you!! Pin
demkafas7-Mar-10 16:26
Memberdemkafas7-Mar-10 16:26 
QuestionHow to copy folder? Pin
drsagitn20-Dec-08 5:02
Memberdrsagitn20-Dec-08 5:02 
AnswerRe: How to copy folder? Pin
drsagitn20-Dec-08 7:09
Memberdrsagitn20-Dec-08 7:09 
GeneralIt worked for me for dataset !!! Pin
Prash20-Nov-08 2:48
MemberPrash20-Nov-08 2:48 
Generalzip-Compress-Versions Pin
Cortadillo21-Aug-08 21:42
MemberCortadillo21-Aug-08 21:42 
Generalproblem with directories Pin
pepepaco30-Jun-08 6:12
Memberpepepaco30-Jun-08 6:12 
GeneralRe: problem with directories Pin
pepepaco30-Jun-08 6:32
Memberpepepaco30-Jun-08 6:32 
GeneralRe: problem with directories Pin
pepepaco30-Jun-08 8:48
Memberpepepaco30-Jun-08 8:48 
GeneralRe: problem with directories Pin
Cortadillo21-Aug-08 21:53
MemberCortadillo21-Aug-08 21:53 
General.Net compact framework Pin
DaveChad123-Jan-07 20:26
MemberDaveChad123-Jan-07 20:26 
GeneralRe: .Net compact framework Pin
Uwe Keim23-Jan-07 20:58
sitebuilderUwe Keim23-Jan-07 20:58 
GeneralRe: .Net compact framework Pin
DaveChad124-Jan-07 13:37
MemberDaveChad124-Jan-07 13:37 
GeneralIts not working Pin
Member 28700283-Jan-07 18:14
MemberMember 28700283-Jan-07 18:14 
GeneralRe: Its not working Pin
Uwe Keim3-Jan-07 19:22
sitebuilderUwe Keim3-Jan-07 19:22 
GeneralRe: Maybe it will be better? Pin
shanyg24-May-08 1:37
Membershanyg24-May-08 1:37 
Generalwhy doesn't it work it XML formatted String Pin
anj198315-Aug-06 1:21
Memberanj198315-Aug-06 1:21 
GeneralQuestion Pin
Nigel Stratton12-Jul-06 18:42
MemberNigel Stratton12-Jul-06 18:42 
Questionbzip2 or gzip wrapper ? Pin
hungpvtn13-Jan-06 14:40
Memberhungpvtn13-Jan-06 14:40 
AnswerRe: bzip2 or gzip wrapper ? Pin
eclipse2k112-Jul-06 0:41
Membereclipse2k112-Jul-06 0:41 
GeneralCant Decompress String Pin
Kashif Ibrahim30-Dec-05 0:42
MemberKashif Ibrahim30-Dec-05 0:42 
GeneralNice job! Pin
Tornacious14-Dec-05 8:29
MemberTornacious14-Dec-05 8:29 

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

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