C# Exercises

Home AgriMetSoft About Contact

Compress and Decompress File in WinForms C#

	    
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Compress_and_Decompress
{
  public partial class Form1 : Form
  {
    public Form1()
    {
      InitializeComponent();
    }

    private void compress_Click(object sender, EventArgs e)
    {
      OpenFileDialog opf = new OpenFileDialog();
      if (opf.ShowDialog() == DialogResult.OK)
      {
        var bytes = System.IO.File.ReadAllBytes(opf.FileName);
        int L1 = bytes.Length;
        bytes = Utilities.GZipCompressor.CompressBytes(bytes);
        int L2 = bytes.Length;
        SaveFileDialog sfd = new SaveFileDialog();
        sfd.Filter = "GZip File|*.gzip";
        if (sfd.ShowDialog() == DialogResult.OK)
        {
          System.IO.File.WriteAllBytes(sfd.FileName, bytes);
          report.Text = string.Format("Report: INPUT FILE IS {0} Bytes and COMPRESSED FILE IS {1} bytes", L1, L2);
        }
      }
    }

    private void decompress_Click(object sender, EventArgs e)
    {
      OpenFileDialog opf = new OpenFileDialog();
      opf.Filter = "GZip File|*.gzip";
      if (opf.ShowDialog() == DialogResult.OK)
      {
        var bytes = System.IO.File.ReadAllBytes(opf.FileName);
        int L1 = bytes.Length;
        bytes = Utilities.GZipCompressor.DecompressBytes(bytes);
        int L2 = bytes.Length;

        SaveFileDialog sfd = new SaveFileDialog();
        if (sfd.ShowDialog() == DialogResult.OK)
        {
          System.IO.File.WriteAllBytes(sfd.FileName, bytes);
          report.Text = string.Format("Report: INPUT FILE IS {0} Bytes and DECOMPRESSED FILE IS {1} bytes", L1, L2);
        }
      }
    }
  }
}
//===================================================================
using System.IO;
using System.IO.Compression;
using System.Threading;
using System.Threading.Tasks;

namespace Utilities
{
 public class GZipCompressor
 {
   public static byte[] CompressBytes(byte[] bytes)
   {
     using (var outputStream = new MemoryStream())
     {
       using (var compressionStream = new GZipStream(outputStream, CompressionLevel.Optimal))
       {
         compressionStream.Write(bytes, 0, bytes.Length);
       }
       return outputStream.ToArray();
     }
   }

   public static byte[] DecompressBytes(byte[] bytes)
   {
     using (var inputStream = new MemoryStream(bytes))
     {
       using (var outputStream = new MemoryStream())
       {
         using (var compressionStream = new GZipStream(inputStream, CompressionMode.Decompress))
         {
           compressionStream.CopyTo(outputStream);
         }
         return outputStream.ToArray();
       }
     }
   }

 }
}
		
	 


Download the project of Visual Studio 2013 in DropBox Download


How to Compress and Decompress File in Winforms C# using GZip


List of Exercises