Compression and Zip Support in .NET:-

Posted: March 28, 2007 in Uncategorized
This is not a new functionality and you might alreday be using one or the other version of tools and code for providing this functionalities in your application.However if you think for using a Zip component in a Enterprise Application for production environment most probably you are using some thirdparty library and/or tools to support these functionalities.
In this post I ll try to Cover both Data as well as File Compression tools as well as what all approaches and techniques and tools avaialble for you to build one out of the box in the .NET framework itself.So to start with When I saw the System.IO.compression available in the .NET framework 2.0 my initial thought was  that it supprot out of box compression for both Data and Files but I was Wrong and although you can now compress your data there was no out of box support for file compression although you can ship one with your own.Further to that be remember that the .Zip compression format is owned by PKZip and thus you don’t have it available out of box .Something similar to the PDF for ADOBE until the openxml comes into the picture with the Office 07.
Without much writing into the historical piece let me dive directly into the topic itself:-What all approaches you have in the framework to provide FileCompression:- IO.Compression,CAB and SHell Api and using J# library using any of these you can provide functionlity to Zip a file but to implement A recursive Folder Routine into .ZIP is still distant and the Compression ratio is also debatable that is why most of the Enterprise App falls into the Thirdparty .However Saying so does not restrict you to bundle your data stream in a Compressed format or your compresssed your fiel and you are always welcome and hookup your own data.Further to a few sample linbe of codes I ll also provide quite a few useful links so that you can take this forward.
Now to use the System.IO you might code something like the following:-

using System.IO.Compression;

using System.IO;
private static void Compress(Stream source, Stream destination)
using(GZipStream output = new GZipStream(destination, CompressionMode.Compress))
Pump(source, output);
private static void Decompress(Stream source, Stream destination)
using(GZipStream input = new GZipStream(source, CompressionMode.Decompress))
Pump(input, destination);
private static void Pump(Stream input, Stream output)
byte[] bytes = new byte[4096];
int n;
while((n = input.Read(bytes, 0, bytes.Length)) != 0)
output.Write(bytes, 0, n);
 OR you might use something like :-
private static void compress()
            FileStream inputFile = null;
            GZipStream compressedZipStream = null;
               // Open and read the contents of the file
               inputFile = new FileStream(@"filepath to zip", FileMode.Open,
                                          FileAccess.Read, FileShare.Read);
               byte[] buffer = new byte[inputFile.Length];
               int count = inputFile.Read(buffer, 0, buffer.Length);
     MemoryStream memoryBuffer = new MemoryStream();
               compressedZipStream = new GZipStream(memoryBuffer,
               CompressionMode.Compress, true);
               compressedZipStream.Write(buffer, 0, buffer.Length);
             FileStream str=File.Open(@"Compressed File path",FileMode.OpenOrCreate);
               if (inputFile != null) inputFile.Close();
               if (compressedZipStream!= null) compressedZipStream.Close();

Here are few links covering different approaches for this:-

  Using GZipStream for Compression in .NET [Brian Grunkemeyer]
Compression Classes Enhance I/O in .NET 2.0
Cabinet File (*.CAB) Compression and Extraction By Elmue

There are many third-party library available and the one I have pointed above are due to  the fact that I involved with

those during different project implementations.One final Word is that the IO compression Classes were not meant for the File Compression and .Zip is not the extn due to the legal requirements.

  Ok Now that I have mentioned few lines for the Compression there are other related story which I want to separate from the Stream Compression like MTOM,HTTP compression Support in IIS which were more specific to ASP.NET & WebSvc Scenario.If you are looking for the guidelines and more into this side of the story do checkout the following:-

  I ll update this post later with more info on the compression including the Zip,Data Compression,File Compression,SOAP and HTTP comression.For the time being I hope that these will provide a quick start and guidance.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s