How to Zip and Unzip File(s)

using System; using System.ComponentModel; using System.Text; using System.Windows.Forms; using System.IO.Compression; using System.IO; using System.Security.Cryptography; using System.Collections.Generic; using System.Management; namespace OmrokZipper { public partial class OmrokZipper : Form { public OmrokZipper() { InitializeComponent(); } private void CreateOmrokZip_Click(object sender, EventArgs e) { try { DialogResult CreateOmrokZipFile = saveFileDialog1.ShowDialog(); if (CreateOmrokZipFile == DialogResult.OK) { if (txtOmrokZipSourceFile.Text == folderBrowserDialog1.SelectedPath) { ZipFile.CreateFromDirectory(txtOmrokZipSourceFile.Text, saveFileDialog1.FileName + ".zip"); } else { string[] omrokZipperFile = txtOmrokZipSourceFile.Text.Split(','); ZipArchive zip = ZipFile.Open(saveFileDialog1.FileName, ZipArchiveMode.Create); foreach (string file in omrokZipperFile) { if (txtOmrokZipPassword.Enabled == true) { using (Ionic.Zip.ZipFile zipPass = new Ionic.Zip.ZipFile(txtOmrokZipSourceFile.Text)) { zipPass.Password = txtOmrokZipPassword.Text; zipPass.AddDirectoryByName(file); zipPass.Save(file + ".zip"); //zip.CreateEntryFromFile(file, Path.GetFileName(file), CompressionLevel.Optimal); } } } zip.Dispose(); } MessageBox.Show("Zip created successfully"); } } catch(Exception ex) { MessageBox.Show(ex.ToString()); } } private void omrokZipFile_Click(object sender, EventArgs e) { DialogResult omrokZipResult = openFileDialog1.ShowDialog(); if (omrokZipResult == DialogResult.OK) { string[] omrokFiles = openFileDialog1.FileNames; txtOmrokZipSourceFile.Text = string.Join(",", omrokFiles); omrokAchieveFilesList.Items.Clear(); //string[] omrokFileDirectory = Directory.GetFiles(txtOmrokZipSourceFile.Text); for (int i = 0; i < omrokFiles.Length; i++) { omrokAchieveFilesList.Items.Add(omrokFiles[i]); } } } private void omrokZipFolder_Click(object sender, EventArgs e) { DialogResult omrokZipFolder = folderBrowserDialog1.ShowDialog(); if (omrokZipFolder == DialogResult.OK) { txtOmrokExtractFileName.Enabled = true; txtOmrokZipSourceFile.Text = folderBrowserDialog1.SelectedPath; omrokAchieveFilesList.Items.Clear(); string[] omrokDirectoryDisplay = System.IO.Directory.GetDirectories(txtOmrokZipSourceFile.Text); int i; for(i = 0; i < omrokDirectoryDisplay.Length; i++) { omrokAchieveFilesList.Items.Add(omrokDirectoryDisplay[i]); } omrokDirectoryDisplay = Directory.GetFiles(txtOmrokZipSourceFile.Text); for (i = 0; i < omrokDirectoryDisplay.Length; i++) { omrokAchieveFilesList.Items.Add(omrokDirectoryDisplay[i]); } } } private void omrokOpen_Click(object sender, EventArgs e) { DialogResult omrokOpenZip = openFileDialog1.ShowDialog(); if (omrokOpenZip == DialogResult.OK) { string[] omrokFiles = openFileDialog1.FileNames; txtOmrokZipSourceFile.Text = string.Join(",", omrokFiles); ZipArchive omrokAchieve = ZipFile.OpenRead(openFileDialog1.FileName); omrokAchieveFilesList.Items.Clear(); foreach(ZipArchiveEntry omrokAchieveEntry in omrokAchieve.Entries) { using (Ionic.Zip.ZipFile zpExtract = new Ionic.Zip.ZipFile()) { txtOmrokZipPassword.Enabled = true; zpExtract.Password = txtOmrokZipPassword.Text; omrokAchieveFilesList.Items.Add(omrokAchieveEntry.FullName); zpExtract.ExtractAll(txtOmrokZipSourceFile.Text, Ionic.Zip.ExtractExistingFileAction.DoNotOverwrite); omrokAchieveFilesList.Items.Add(txtOmrokZipPassword.Text); } } } } private void omrokAchieveFiles_SelectedIndexChanged(object sender, EventArgs e) { } private void omrokExtract_Click(object sender, EventArgs e) { try { DialogResult omrokZipExtract = openFileDialog1.ShowDialog(); if (omrokZipExtract == DialogResult.OK) { txtOmrokExtractFileName.Text = openFileDialog1.FileName; DialogResult omrokZipExtractFolder = folderBrowserDialog1.ShowDialog(); if (omrokZipExtractFolder == DialogResult.OK) { using(Ionic.Zip.ZipFile extractZip = new Ionic.Zip.ZipFile(txtOmrokZipSourceFile.Text)) { extractZip.Password = txtOmrokZipPassword.Text; extractZip.Encryption = Ionic.Zip.EncryptionAlgorithm.WinZipAes256; extractZip.StatusMessageTextWriter = Console.Out; extractZip.ExtractAll(txtOmrokZipSourceFile.Text, Ionic.Zip.ExtractExistingFileAction.DoNotOverwrite); MessageBox.Show("Zip extracted successfully !"); } //ZipFile.ExtractToDirectory(openFileDialog1.FileName, folderBrowserDialog1.SelectedPath); //decryptOmrokFile(Process.GetCurrentProcess().ProcessName, txtOmrokZipPassword.Text); } } } catch(Exception ex) { MessageBox.Show(ex.ToString()); } } private void helpToolStripMenuItem_Click(object sender, EventArgs e) { } private void omrokAchiveFileItemList_SelectedIndexChanged(object sender, EventArgs e) { } private void ZipPasswordEnabled_CheckedChanged(object sender, EventArgs e) { if (ZipPasswordEnabled.Checked) { txtOmrokZipPassword.Enabled = true; } else { txtOmrokZipPassword.Enabled = false; } } } }
The above code is a complete C# Windows Forms code program to Zip and Unzip File(s) and Folder(s).

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.