Wednesday, 13 June 2012

C# Tips : Compress and Decompress file or data using C# and VB.Net

.Net framework provide the System.IO.Compression.GZipStream or System.IO.Compression.DeflateStream to compress or decompress data.
Both GZipStream and DeflateStream classes allow you to use GZIP and Deflate compression algorithms to compress or decompress data.

Here are sample examples with GZipStream class.
The sample examples there are two methods one is CompressData and other is DCompressData.
In CompressData method we create a new file and uses the GZipStream class to write compressed file to it and close that file .
In DCompressData method we open that compressed file in read mode and decompressed the file and save.
In this sample you have to provide the file path which file you want to compress and also provide file path where the compressed file stored.
In this example we store both files in application root directory.

This examples are in both C# and VB.Net .

C# Example :
    protected void Page_Load(object sender, EventArgs e)

    void CompressData()
        // Create the compression stream.
        GZipStream objGZipOut = new GZipStream(File.OpenWrite(Server.MapPath("compressed_data.gzip")), CompressionMode.Compress);
        int intBufferLength = 16 * 1024;
        byte[] objBuffer = new byte[intBufferLength];

        // Get File Stream Object
        System.IO.FileStream objFileStream = System.IO.File.Open(MapPath("TextFile.txt"), System.IO.FileMode.Open);
        int len = 0;

        while ((len = objFileStream.Read(objBuffer, 0, intBufferLength)) != 0)
            // Write file Content 
            objGZipOut.Write(objBuffer, 0, len);

    void DCompressData()
        // Open the same gzip file to decompress
        GZipStream objGZipIn = new GZipStream(File.OpenRead(Server.MapPath("compressed_data.gzip")), CompressionMode.Decompress);

        // Get stream reader of the gzip file.
        StreamReader objReader = new StreamReader(objGZipIn);
        byte[] buffer = new byte[16 * 1024];
        int len = 0;
        FileStream objFS = new FileStream(Server.MapPath("compressed_data.txt"), FileMode.Create, FileAccess.Write, FileShare.Read);
        while ((len = objReader.BaseStream.Read(buffer, 0, buffer.Length)) != 0)
            objFS.Write(buffer, 0, len);
    } Example :
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    End Sub
    Private Sub CompressData()
        ' Create the compression stream.
        Dim objGZipOut As New GZipStream(File.OpenWrite(Server.MapPath("compressed_data.gzip")), CompressionMode.Compress)

        Dim intBufferLength As Integer = 16 * 1  '1024
        Dim objBuffer As Byte() = New Byte(intBufferLength - 1) {}

        ' Get File Stream Object
        Dim objFileStream As System.IO.FileStream = System.IO.File.Open(MapPath("TextFile.txt"), System.IO.FileMode.Open)
        Dim len As Integer = 0
        len = objFileStream.Read(objBuffer, 0, intBufferLength)
        While len <> 0
            ' Write file Content 
            objGZipOut.Write(objBuffer, 0, len)
            len = objFileStream.Read(objBuffer, 0, intBufferLength)
        End While


    End Sub
    Private Sub DCompressData()
        ' Open the same gzip file to decompress
        Dim objGZipIn As New GZipStream(File.OpenRead(Server.MapPath("compressed_data.gzip")), CompressionMode.Decompress)

        ' Get stream reader of the gzip file.
        Dim objReader As New StreamReader(objGZipIn)

        Dim buffer As Byte() = New Byte(16 * 1024 - 1) {}
        Dim len As Integer = 0
        Dim objFS As New FileStream(Server.MapPath("compressed_data.txt"), FileMode.Create, FileAccess.Write, FileShare.Read)
        len = objReader.BaseStream.Read(buffer, 0, buffer.Length)
        While len <> 0
            objFS.Write(buffer, 0, len)
            len = objReader.BaseStream.Read(buffer, 0, buffer.Length)
        End While

    End Sub

You can also provide other data like simple string to compress data , for that you have to use stream writer and use it's WriteLine method.

In this example we provide "TextFile.txt" text file. Size of this text file is 3 kb.
After the compressing new file "compressed_data.gzip" generated and it's size is 1 kb.

Output :

Before compression 

After compression 


  1. Lot of questions have raised after the release of dot Net core and dot net framework. I have seen some reasonable answer for all those questions. Thanks for your answers and differences between .Net Core and .Net framework.
    DOT NET Training in Chennai | DOT NET Course in Chennai

  2. Very Informative...Glad to find your blog...Keep Sharing...
    Testing & Training on Selenium

  3. wonderful article. Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article. This article resolved my all queries. keep it up.

    Dot Net Training in Chennai | Dot Net Training in anna nagar | Dot Net Training in omr | Dot Net Training in porur | Dot Net Training in tambaram | Dot Net Training in velachery