Zip Unzip using GZipStream in C#

In this blog we are going to create our own application to zip & unzip a file in C#.

Open Visual Studio and create a Windows Form Application

Add these namespaces to use the GZipStream

using System.IO;

using System.IO.Compression;

Now, add one textbox, three buttons and one folderBrowserDialog1 on a form like this:

Add this code on the browse button

private void btnBrowse_Click(object sender, EventArgs e)

{

    if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)

    {

        txtPath.Text = folderBrowserDialog1.SelectedPath.ToString();

        filePath = txtPath.Text.Trim();

    }

}

//Code for zip

public static void zipFile(FileInfo fileToCompress)

{

    using (FileStream originalFileStream = fileToCompress.OpenRead())

    {

        if ((File.GetAttributes(fileToCompress.FullName) & FileAttributes.Hidden) != FileAttributes.Hidden & fileToCompress.Extension != ".gz")

        {

            using (FileStream compressedFileStream = File.Create(fileToCompress.FullName + ".gz"))

         {

         using (GZipStream compressionStream = new GZipStream(compressedFileStream, CompressionMode.Compress))

         {

            originalFileStream.CopyTo(compressionStream);

         }

      }

    }

  }

//Code for unzip

 

public static void unzipFile(FileInfo fileToDecompress)

{

    using (FileStream originalFileStream = fileToDecompress.OpenRead())

    {

         string currentFileName = fileToDecompress.FullName;

         string newFileName = currentFileName.Remove(currentFileName.Length - fileToDecompress.Extension.Length);

         using (FileStream decompressedFileStream = File.Create(newFileName))

         {

            using (GZipStream decompressionStream = new GZipStream(originalFileStream, CompressionMode.Decompress))

            {

                decompressionStream.CopyTo(decompressedFileStream);

            }

         }

     }

}

 

And on the zip button click event, write these codes:

 

private void btnZip_Click(object sender, EventArgs e)

{

    try

    {

        directorySelected = new DirectoryInfo(filePath);

        foreach (FileInfo fileToCompress in directorySelected.GetFiles())

        {

            zipFile(fileToCompress);

        }

        MessageBox.Show("Zip Created Successfully...");

    }

    catch (Exception Ex)

    {

        MessageBox.Show(Ex.Message,Application.ProductName,MessageBoxButtons.OK,MessageBoxIcon.Information);

    }

} 

 
Lastly, on the unzip button, write these codes to call the unzipFile method:

private void btnUnzip_Click(object sender, EventArgs e)

{

    try

    {

        directorySelected = new DirectoryInfo(filePath);

        foreach (FileInfo fileToDecompress in directorySelected.GetFiles("*.gz"))

        {

            unzipFile(fileToDecompress);

        }

        MessageBox.Show("Unzip Created Successfully...");

    }

    catch (Exception Ex)

    {

        MessageBox.Show(Ex.Message,Application.ProductName,MessageBoxButtons.OK,MessageBoxIcon.Information);

    }

 

Now run the application and browse the folder to zip or unzip and click on the zip button to compress. And to decompress the compressed file, browse the folder and click on the unzip Button.