Archive for March, 2007

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;
            try
            {
               // 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);
               inputFile.Close();
     MemoryStream memoryBuffer = new MemoryStream();
               compressedZipStream = new GZipStream(memoryBuffer,
               CompressionMode.Compress, true);
               compressedZipStream.Write(buffer, 0, buffer.Length);
               compressedZipStream.Close();
             FileStream str=File.Open(@"Compressed File path",FileMode.OpenOrCreate);
                str.Write(buffer,0,buffer.Length);
                str.Close();
             }
            finally
            {
               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] http://blogs.msdn.com/bclteam/archive/2005/06/15/429542.aspx
Compression Classes Enhance I/O in .NET 2.0 http://www.developer.com/net/net/article.php/11087_3510026_1
Cabinet File (*.CAB) Compression and Extraction By Elmue http://www.codeproject.com/cs/files/CABCompressExtract.asp

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.