Compress/Decompress Files using System.IO.Compression.GZipStream Class [2.0]

I was in need to compress and decompress files in one of the projects im currently working on. At the start i only knew about GZipStream existance in 2.0 but never used it before. So today i used it and i was able to compress and decompress the files back when i need to. Below are two functions

    /// <summary>
    /// To Compress A File
    /// </summary>
    /// <param name="filePath">The Complete file path to the file you want to compress</param>  
    protected void Compress(string filePath)
    {
                    FileStream sourceFile = File.OpenRead(filePath);
            FileStream destinationFile = File.Create(Server.MapPath("~") + "/tv.gzip");
            byte[] buffer = new byte[sourceFile.Length];
            GZipStream zip = null;
            try
            {
                sourceFile.Read(buffer, 0, buffer.Length);
                zip = new GZipStream(destinationFile, CompressionMode.Compress);
                zip.Write(buffer, 0, buffer.Length);
            }
            catch(Exception ex)
            {
                Response.Write(ex.Message);
            }
            finally
            {
                zip.Close();
                sourceFile.Close();
                destinationFile.Close();
            }
    }

  /// <summary>
    /// To Decompress an already compressed file
    /// </summary>
    /// <param name="filePath">The complete file path of the already compressed file</param>
    protected void Decompress(string filePath)
    {
        FileStream sourceFile = File.OpenRead(filePath);
        FileStream destinationFile = File.Create(Server.MapPath("~") + "/tv1.xml");
        GZipStream unzip = null;
        byte[] buffer = new byte[sourceFile.Length];
        try
        {
            unzip = new GZipStream(sourceFile, CompressionMode.Decompress, false);
            int numberOfBytes = unzip.Read(buffer, 0, buffer.Length);

            destinationFile.Write(buffer, 0, numberOfBytes);
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }
        finally
        {
            sourceFile.Close();
            destinationFile.Close();
            unzip.Close();
        }    
    }

N.B: Do not forget to import two namespaces

  1. System.IO
  2. System.IO.Compression

Best Regards,

Published 02 October 2007 12:57 AM by haissam

Comments

# Rafael Cabral said on 17 August, 2009 03:42 PM

Why files compressed by GZip are bigger than their uncompressed versions???

It's junk!!!

# justin.coco said on 02 October, 2009 04:22 AM

when decompressing, the destination file is bigger than compressed file,but in Decompress method,the buffer's

length is same as compressed file's length,i think this is a bug

# Hariom said on 01 November, 2009 02:25 AM

Aftere compressing a text file and uncompressing I noticed some of the text from the buttom has been missing on the uncompressed file while compared with the Original Text file

This site

Search

Go

This Blog

Syndication

Sponsors

  • MaximumASP