How to zip file in ASP.NET with secured password?


In this article I have explained about how to create Zip file in theASP .NET. For example we can create zip files in using winrar software instead of that how we can create zip file using C# code. This article is help you to know create zip file operation. How to create password for open or unzip files.

Learn how to zip file in ASP.NET with secured password?


Description:
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 ASP.NET concept.
1) Using file upload control and take file and zip with secured password it based on user selection
2) Zip whole folder and files with secured password

Refer below explanation

Client side : I placed one file upload control to collect file using user selection

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>ASP.NET Archieve Files</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table cellpadding="0" cellspacing="0" align="center" width="700">
<tr>
<td height="30" colspan="2">
<b>ASP.NET File Zip Operations</b><br /><br />
</td>
</tr>
<tr>
<td height="60" colspan="2">
<asp:Label ID="lblmsg" runat="server"></asp:Label>
</td>
</tr>
<tr>
<td height="30" align="left">
Select File to be Zip
</td>
<td>
<asp:FileUpload ID="FileUpload1" runat="server" />
</td>
</tr>
<tr>
<td height="50" colspan="2" align="center">
<asp:Button ID="btnZip" runat="server" Text="Upload and Zip"
onclick="btnZip_Click" />
</td>
</tr>
</table>
</div>
</form>
</body>
</html>

Here I select file using file upload control
Zip1

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

using Ionic.Zip;
using System.IO;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void btnZip_Click(object sender, EventArgs e)
{
// SINGLE FILE UPLOAD AND ZIP EXAMPLE START //

try
{
if (FileUpload1.HasFile)
{
// Mention path where your zip file save
var path = "E:\\" + FileUpload1.FileName + ".zip";

// Zip File and save in your mentioned path
using (var zip = new ZipFile())
{
zip.AddEntry(FileUpload1.FileName, FileUpload1.FileContent);
zip.Save(path);
}

lblmsg.Text = "Single file zipped successfully here : " + path + "

";
}
else
{
lblmsg.Text = "Select file to be archieve!";
}
}
catch(Exception ex)
{
lblmsg.Text="Error During Zip operation " + ex.ToString();
}

// SINGLE FILE UPLOAD AND ZIP EXAMPLE END //

// FOLDER ZIP EXAMPLE START //

//Metion your File Path here
string folderpath = "E:\\test";

try
{
string fname;
using (var zip = new ZipFile())
{
//Here i set password property to the zip file
zip.Password = "test123";
zip.AddDirectory(folderpath);
fname = folderpath + DateTime.Now.ToString("ddMMyyyyHHmmss") + ".rar";
zip.Save(fname);
lblmsg.Text += "Multiple files are zipped here: " + folderpath + "<br/><br/><br/>";
}
// extract entries that use encryption
using (ZipFile zip = ZipFile.Read(fname))
{
zip.Password = "test123";
zip.ExtractAll("extractDir");
}


}
catch(Exception ex)
{
lblmsg.Text="Error During Multiple file zip operation!";
}
// MULITPLE FILE UPLOAD AND ZIP EXAMPLE END //
}
}

After upload it display message like this
ZipWeb2

If you open that zip file password asking like this to open
ZipWeb3

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

Front End : ASP.NET
Code Behind : C#

Conclusion:
I hope this article help to know about archive view concept in ASP.NET.


Attachments

  • Source_code (43505-221822-ArchieveFileWeb.rar)
  • Comments

    No responses found. Be the first to comment...


  • 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: