How to create zip file in C#.NET with secured password?


In this article I have explained about how to create Zip file in the .NET. For example we can create zip files in using winrar software instead of that how we can create zip file using C#.NET code. This article is help you to know create zip file operation. Find how to create zip file in C#.NET with secured password?

Learn how to create zip file in C#.NET with secured password?


Description:
In my previous article I have explained about zip process in ASP.NET. Here I explained in depth about zip process in C#.NET with open file dialogue, Save file dialogue etc.
For example I have folder and files in my system, I want to create zip file and set password. If anyone open/ unzip that file it should ask password to open/unzip.

Here I used ionic.dll to zip files.
In this article I have explained in detail with C#.NET concept.
1) Using OpenFileDialog control take file and zip with secured password
2) Using FolderBrowserDialog to Zip entire folder and files with secured password
3) Using FolderBrowserDialog to Zip only condition based files folder and files like (.jpg/jpeg)

Refer below explanation

Client side: I have design like this,
Zipwin1

Server side
Here I zip single file and whole folder with secured password

using System.IO;
using Ionic.Zip;

namespace ArchieveFiles
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnOpen1_Click(object sender, EventArgs e)
{
OpenFileDialog fDialog = new OpenFileDialog();
fDialog.Title = "Select file to be zip";
//fDialog.Filter = "JPEG Files|*.jpeg|GIF Files|*.gif";
if (fDialog.ShowDialog() == DialogResult.OK)
{
textBox1.Text = fDialog.FileName.ToString();
}
}

private void btnZip1_Click(object sender, EventArgs e)
{
if (textBox1.Text == "")
{
MessageBox.Show("select file to be Zip","Alert");
return;
}

SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = "zip files (*.zip)|*.zip|rar files (*.rar)|*.rar";
saveFileDialog1.FilterIndex = 2;
saveFileDialog1.RestoreDirectory = true;

if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
//Zip one file
string filename = textBox1.Text;

try
{
/*** ZIP Single file - START HERE */

using (var zip = new ZipFile())
{
//Set password for your zip file
zip.Password = "test123";
zip.AddFile(filename,"");
zip.Save(saveFileDialog1.FileName.ToString());
}

// extract entries that use encryption
using (ZipFile zip = ZipFile.Read(saveFileDialog1.FileName.ToString()))
{
//check password before file is extract
zip.Password = "test123";
zip.ExtractAll("extractDir");

}
/*** ZIP Single file - END HERE */
MessageBox.Show("Files are Zipped Successfully!", "Alert");
}
catch (Exception ex)
{
MessageBox.Show("Error during Zip operation!", "Alert");
}
clear();
}
}

private void btnOpen2_Click(object sender, EventArgs e)
{
//Open Folder Browser Dialog

FolderBrowserDialog FBDialog = new FolderBrowserDialog();
{

FBDialog.RootFolder = Environment.SpecialFolder.Desktop;
FBDialog.SelectedPath = "c:\\windows";
FBDialog.Description = "Select the directory to save Zip";
if (FBDialog.ShowDialog() == DialogResult.OK)
{
textBox2.Text = FBDialog.SelectedPath;
}
}
}

private void btnZip2_Click(object sender, EventArgs e)
{
if (textBox2.Text == "")
{
MessageBox.Show("select folder to be Zip","Alert");
return;
}

SaveFileDialog saveFileDialog2 = new SaveFileDialog();

saveFileDialog2.Filter = "zip files (*.zip)|*.zip|rar files (*.rar)|*.rar";
saveFileDialog2.FilterIndex = 2;
saveFileDialog2.RestoreDirectory = true;

if (saveFileDialog2.ShowDialog() == DialogResult.OK)
{
//Zip directory including all files
string filename = textBox1.Text;
try
{

using (var zip = new ZipFile())
{
zip.AddDirectory(textBox2.Text,"");
zip.Save(saveFileDialog2.FileName.ToString());
}
MessageBox.Show("Files are Zipped Successfully!", "Alert");
}
catch (Exception ex)
{
MessageBox.Show("Error during Zip operation!", "Alert");
}
clear();
}
}

private void btnOpen3_Click(object sender, EventArgs e)
{
//Open Folder Browser Dialog

FolderBrowserDialog FBDialog = new FolderBrowserDialog();
{

FBDialog.RootFolder = Environment.SpecialFolder.Desktop;
FBDialog.SelectedPath = "c:\\windows";
FBDialog.Description = "Select the directory to save Zip";
if (FBDialog.ShowDialog() == DialogResult.OK)
{
textBox3.Text = FBDialog.SelectedPath;
}
}
}

private void button2_Click(object sender, EventArgs e)
{


if (textBox3.Text == "")
{
MessageBox.Show("select file to be Zip", "Alert");
return;
}


/*** ZIP Multiple .jpg file - START HERE */

//Metion your File Path here
string path = textBox3.Text;

SaveFileDialog saveFileDialog3 = new SaveFileDialog();
saveFileDialog3.Filter = "zip files (*.zip)|*.zip|rar files (*.rar)|*.rar";
saveFileDialog3.FilterIndex = 2;
saveFileDialog3.RestoreDirectory = true;

if (saveFileDialog3.ShowDialog() == DialogResult.OK)
{
//Zip directory including all files
string filename = textBox1.Text;

try
{
DirectoryInfo datafile = new DirectoryInfo(path);
FileInfo[] arfiles = datafile.GetFiles("*.*");
FileInfo finfo = default(FileInfo);

if (System.IO.Directory.Exists(path))
{
using (var zip = new ZipFile())
{
for (int i = 0; i <= arfiles.Length - 1; i++)
{
//Read Each files and ZIP it and check jog files

if (arfiles.Length > 0)
{
finfo = arfiles[i];
if (finfo.Extension == ".jpg")
{
zip.AddFile(finfo.FullName,"");
}
}
}
zip.Save(saveFileDialog3.FileName.ToString());
}
MessageBox.Show("Files are Zipped Successfully!", "Alert");
}
}
catch (Exception ex)
{
MessageBox.Show("Error during Zip operation!", "Alert");
}
clear();
}
/*** ZIP Multiple file - END HERE */
}
void clear()
{
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
}

private void label6_Click(object sender, EventArgs e)
{

}
}
}

After click open select file using like below

Zipwin2

After click Zip Single file button to choose save path location to save your zip file

Zipwin3

If user click that zip file it will ask password to open the zip like below screen if you give correct password open that file otherwise not open.

Zipwin4

Source Code Detail:
Here with I have attached source code download it and try to learn about zip process in C#.NET .

Front End : Form Design
Code Behind : C#

Conclusion:
I hope this article help to know about archive concept in C#.NET.


Attachments

  • Source_code (43506-221833-ArchieveFiles.rar)
  • Comments

    Guest Author: Ravikumar20 Sep 2012

    i used this Dll in my application, and I have given password, but it is not working. Can you tell me ,where I am doing mistake..



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: