Click here to Skip to main content
15,878,959 members
Articles / Programming Languages / C#

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 132.8K   2K   62   37
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/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().

Examples

Example 1 - Compressing and decompressing a string.

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

C#
// 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:

C#
// 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)


Written By
Chief Technology Officer Zeta Software GmbH
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.

➡️ Give me a tip 🙂

In his free time, he does climbing, running and mountain biking. In 2012 he became a father of a cute boy and in 2014 of an awesome girl.

Some cool, free software from us:

Windows 10 Ereignisanzeige  
German Developer Community  
Free Test Management Software - Intuitive, competitive, Test Plans.  
Homepage erstellen - Intuitive, very easy to use.  
Offline-Homepage-Baukasten

Comments and Discussions

 
GeneralVersion 0.84 Pin
Angelo Cresta10-Sep-05 3:33
professionalAngelo Cresta10-Sep-05 3:33 
GeneralZipEntry Fail in CompressBytes Pin
Alexandre16-Aug-05 4:02
Alexandre16-Aug-05 4:02 
QuestionIs ZetaCompressionLibrary.dll free? Pin
Anonymous15-Aug-05 15:50
Anonymous15-Aug-05 15:50 
AnswerRe: Is ZetaCompressionLibrary.dll free? Pin
Uwe Keim15-Aug-05 18:29
sitebuilderUwe Keim15-Aug-05 18:29 
GeneralCompress the bitmap in the memory Pin
happyhym22-Jun-05 19:10
happyhym22-Jun-05 19:10 
GeneralRe: Compress the bitmap in the memory Pin
Uwe Keim22-Jun-05 20:07
sitebuilderUwe Keim22-Jun-05 20:07 
GeneralProblem with decompression Pin
Anupreet22-Apr-05 19:09
Anupreet22-Apr-05 19:09 
GeneralRe: Problem with decompression Pin
Bojan Rajkovic8-Aug-05 14:56
Bojan Rajkovic8-Aug-05 14:56 
GeneralDecompression Pin
netclectic21-Mar-05 0:22
netclectic21-Mar-05 0:22 
GeneralRe: Decompression Pin
Uwe Keim21-Mar-05 0:56
sitebuilderUwe Keim21-Mar-05 0:56 
GeneralRe: Decompression Pin
netclectic21-Mar-05 1:25
netclectic21-Mar-05 1:25 

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.