Compress and Decompress data using GZipStream


::Introduction ::

Article will teach you about how to Compress the data with GZipStream Class of .net

Technology :

CSharp .net 2.0/3.5

:: Implementation ::

For simple compression and decompression of data we will use GZipStream class of the .NET.

In this article,  I will explain how to compress and decompress data by building small sample application.


 

So lets import the following namespaces in your application. 

using System.IO;

using System.IO.Compression;


Here when working with GZipStream we need to specify Stream to Open for writing as first argument and CompressionMode as second argument that specifies weather we want to Compress the Data or Decompress the data we can do this by Specifying enumeration CompressMode.Compress or CompressMode.Decompress as second argument of the GZipStream Class object.

Here in example I am compressing textdata into file test.zip file and reread it from the created test.zip file.

private void btnCompressData_Click(object sender, EventArgs e)

        {

            //Write Data To Zip File

            string data = txtData.Text;

            GZipStream outStream = new GZipStream(File.OpenWrite("test.zip"), CompressionMode.Compress);

            StreamWriter sw = new StreamWriter(outStream);

            sw.Write(data);

            sw.Close();

            MessageBox.Show("Data Compressed to file test.zip!!");

            txtData.Text = "";

        }


:: explanation of above code ::

In first line of Compressing Data Part I have stored the data to be written in variable "data" from textBox now in second line creates a new GZipStream class and As constructor argument of that object of GZipStream we have passed File.OpenWrite("test.zip") which will return FileStream object associated with "test.zip" file and in Second argument we have passed CompressionModel.Compress as we want to compress the data ..

now We have created one StreamWriter object to write the data to GZipStream object we have by using Write() method of the StreamWriter class we can write data to the GZipStream object

and finally we have closed the StreamWriter Object to end the writing

 private void btnDecompressData_Click(object sender, EventArgs e)

        {

           //Read the Data From zip

            string ReadData = "";

            GZipStream instream = new GZipStream(File.OpenRead("test.zip"), CompressionMode.Decompress);

 

            StreamReader reader = new StreamReader(instream);

 

            ReadData = reader.ReadToEnd();

            reader.Close();

            txtData.Text = ReadData;

            MessageBox.Show("Data Read Successfully!!");

 

        }


Same kind of logic is there for reading data also we open the test.zip file using GZipStream object and read the data in stream using ReadToEnd() method and displayed the read data to the TextBox again. and closed the StreamReader object.

That's it :)

:: Conclusion ::


This article explains how to compress and decompress data using GZipStream class of .NET.

Up Next
    Ebook Download
    View all
    Learn
    View all