Next step of programming

Just another WordPress.com weblog

Archive for the ‘Compression’ Category

Compression & Decompression using System.IO.Compression namespace (GZip Format)

with 2 comments

Microsoft has include the compression classes in the framework itself. Now we can perform compression without having the 3-party libraries. One of the format GZip is given below with a sample class

public class GZipCompression    
{
    /// 
    /// Compress the data from specified file to new file        
    /// 
    /// File to be compressed
    /// Compressed file        
    public void CompressFile(string inputFile, string outputFile)
    {
    //Load the content from the file            
    FileStream inputStream = new FileStream(inputFile, FileMode.Open,
    FileAccess.Read,FileShare.Read);
    byte[] buffer = new byte[inputStream.Length];

    int readCount = inputStream.Read(buffer, 0, buffer.Length);
    if (readCount == 0)
        return;
    inputStream.Close();

    //Compress the data in the memory stream            
    MemoryStream memoryStream = new MemoryStream();
    GZipStream gzipStream = new GZipStream(memoryStream, CompressionMode.Compress, true);
    gzipStream.Write(buffer, 0, buffer.Length);
    gzipStream.Close();

    //write all the compress data to new file            
    byte[] compressedBuff  = new byte[memoryStream.Length];
    int compressedCount = 0;
    memoryStream.Position = 0;
    compressedCount = memoryStream.Read(compressedBuff, 0, compressedBuff.Length);
    File.WriteAllBytes(outputFile, compressedBuff);
    memoryStream.Close();
    }

    /// 
    /// Decompress the compressed file for format GZip        
    /// 
    /// File to be decompressed
    /// file to be compressed        
    public void DecompressFile(string inputFile, string outputFile)
    {
    //Load the content from the file            
    FileStream inputStream = new FileStream(inputFile, FileMode.Open,
    FileAccess.Read, FileShare.Read);
    byte[] buffer = new byte[inputStream.Length];
    int readCount = inputStream.Read(buffer, 0, buffer.Length);
    if (readCount == 0)
        return;

    //decompress the data in the memory stream            
    MemoryStream memoryStream = new MemoryStream();
    memoryStream.Write(buffer, 0, buffer.Length);
    memoryStream.Position = 0;
    GZipStream gzipStream = new GZipStream(memoryStream, CompressionMode.Decompress);

    byte[] decompressedBuff = new byte[1024];
    string fileData = string.Empty;
    while (true)
    {
        int currentCount = gzipStream.Read(decompressedBuff, 0, decompressedBuff.Length);
        if (currentCount == 0)
            break;

        fileData += BytesToString(decompressedBuff, currentCount);
    }

    //Write the data to the new decompressed file            
    StreamWriter outputWriter = new StreamWriter(outputFile, false);
    outputWriter.Write(fileData);
    outputWriter.Close();

    //Close all the streams
    gzipStream.Close();
    memoryStream.Close();
    inputStream.Close();
    }

    private string BytesToString(byte[] buff, int length)
    {
    string content = string.Empty;
    for(int i = 0; i <length; i++)
    {
        content += Convert.ToString((char)buff[i]);
    }
    return content;
    }
}

Written by A.Sethi

January 27, 2009 at 8:27 am