Windows Forms: Zip file, folder in C#
By FoxLearn 6/26/2017 8:56:25 PM 8.94K
How to Compress/Zip File or Folder using DotNetZip, progress bar in C#
Step 1: Click New Project, then select Visual C# on the left, then Windows and then select Windows Forms Application. Name your project "ZipFile" and then click OK
Step 2: Design your form as below
You need to install Ionic.Zip from nuget. DotNetZip is a FAST, FREE class library and toolset for manipulating zip files. Use VB, C# or any .NET language to easily create, extract, or update zip files.
Step 3: Add code to button click event handler as below
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; namespace ZipFile { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void btnFolder_Click(object sender, EventArgs e) { //Select folder FolderBrowserDialog fbd = new FolderBrowserDialog(); fbd.Description = "Select your path."; if (fbd.ShowDialog() == DialogResult.OK) txtFolder.Text = fbd.SelectedPath; } private void btnFileName_Click(object sender, EventArgs e) { //Select file name using(OpenFileDialog ofd=new OpenFileDialog() { Filter="All files|*.*", ValidateNames = true, Multiselect = false }) { if (ofd.ShowDialog() == DialogResult.OK) txtFileName.Text = ofd.FileName; } } private void btnZipFolder_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(txtFolder.Text)) { MessageBox.Show("Please select your folder.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information); txtFolder.Focus(); return; } string path = txtFolder.Text; //Zip folder & update progress bar Thread thread = new Thread(t => { using(Ionic.Zip.ZipFile zip=new Ionic.Zip.ZipFile()) { zip.AddDirectory(path); System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(path); zip.SaveProgress += Zip_SaveProgress; zip.Save(string.Format("{0}{1}.zip", di.Parent.FullName, di.Name)); } }) { IsBackground = true }; thread.Start(); } private void Zip_SaveProgress(object sender, Ionic.Zip.SaveProgressEventArgs e) { if (e.EventType == Ionic.Zip.ZipProgressEventType.Saving_BeforeWriteEntry) { progressBar.Invoke(new MethodInvoker(delegate { progressBar.Maximum = e.EntriesTotal; progressBar.Value = e.EntriesSaved + 1; progressBar.Update(); })); } } private void Zip_SaveFileProgress(object sender, Ionic.Zip.SaveProgressEventArgs e) { if (e.EventType == Ionic.Zip.ZipProgressEventType.Saving_EntryBytesRead) { progressBar.Invoke(new MethodInvoker(delegate { progressBar.Maximum = 100; progressBar.Value = (int)((e.BytesTransferred * 100) / e.TotalBytesToTransfer); progressBar.Update(); })); } } private void btnZipFile_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(txtFileName.Text)) { MessageBox.Show("Please select your filename.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information); txtFileName.Focus(); return; } string fileName = txtFileName.Text; //Zip file & update process bar Thread thread = new Thread(t => { using (Ionic.Zip.ZipFile zip = new Ionic.Zip.ZipFile()) { FileInfo fi = new FileInfo(fileName); zip.AddFile(fileName); System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(fileName); zip.SaveProgress += Zip_SaveFileProgress; zip.Save(string.Format("{0}/{1}.zip", di.Parent.FullName, fi.Name)); } }) { IsBackground = true }; thread.Start(); } } }
DotNetZip works on PCs with the full .NET Framework, and also runs on mobile devices that use the .NET Compact Framework. Create and read zip files in VB, C#, or any .NET language
VIDEO TUTORIALS
- How to create a Lucky Draw games in C#
- Windows Forms: Youtube Search with Paging in C#
- Windows Forms: Text to speech in C#
- Windows Forms: Print Picture Box in C#
- Windows Forms: Advanced Filter DataGridView in C#
- Windows Forms: Progress Bar in C#
- Windows Forms: How to Create a Random Password Generator in C#
- Windows Forms: Tag Control in C#
Categories
Popular Posts
Visual Studio: Auto increment version
04/01/2024
What is ARM architecture?
04/01/2024