using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.IO;
using System.IO.Compression;
using System.Windows.Forms;
namespace Zipping
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnBrowse_Click(object sender, EventArgs e)
{
// Show the dialog where the user chooses the file to compress
if (openFile.ShowDialog() == DialogResult.OK)
{
txtPath.Text = openFile.FileName;
}
}
private void btnCompress_Click(object sender, EventArgs e)
{
// If the user has selected a path where to put the compressed file
if (saveFile.ShowDialog() == DialogResult.OK)
{
// Bytes array in which we're going to store the actual file to be compressed
byte[] bufferWrite;
// Will open the file to be compressed
FileStream fsSource;
// Will write the new zip file
FileStream fsDest;
GZipStream gzCompressed;
fsSource = new FileStream(txtPath.Text, FileMode.Open, FileAccess.Read, FileShare.Read);
// Set the buffer size to the size of the file
bufferWrite = new byte[fsSource.Length];
// Read the data from the stream into the buffer
fsSource.Read(bufferWrite, 0, bufferWrite.Length);
// Open the FileStream to write to
fsDest = new FileStream(saveFile.FileName, FileMode.OpenOrCreate, FileAccess.Write);
// Will hold the compressed stream created from the destination stream
gzCompressed = new GZipStream(fsDest, CompressionMode.Compress, true);
// Write the compressed stream from the bytes array to a file
gzCompressed.Write(bufferWrite, 0, bufferWrite.Length);
// Close the streams
fsSource.Close();
gzCompressed.Close();
fsDest.Close();
}
}
}
}