Zip Unzip a folder in C#
This article describes how to Zip and Unzip a folder using System.IO.Compression namespace. This namespace contains classes that provide basic compression and decompression services for streams.
First let's create this Windows Form application. When we run this application and browse to the file and click ZipButton/UnzipButton to do zip or unzip it.
Then add these directives to the project:
Declare these variables and add zipFile, unzipFile functions:
Code for BrowseButton:
Code for ZipButton:
Code for UnzipButton:
First let's create this Windows Form application. When we run this application and browse to the file and click ZipButton/UnzipButton to do zip or unzip it.
Then add these directives to the project:
using System.IO;
using System.IO.Compression;
Declare these variables and add zipFile, unzipFile functions:
string filePath;
DirectoryInfo directorySelected;
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);
}
}
}
}
}
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);
}
}
}
}
Code for BrowseButton:
private void button1_Click(object sender, EventArgs e)
{
if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
{
textBox1.Text = folderBrowserDialog1.SelectedPath.ToString();
filePath = textBox1.Text.Trim();
}
}
Code for ZipButton:
private void ZipButton_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);
}
}
Code for UnzipButton:
private void UnzipButton_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);
}
}
Zip Unzip a folder in C#
Reviewed by Bloggeur DZ
on
05:54
Rating:
Aucun commentaire: