Hi Friends ,
I have written code that also generate password protected zip file.
protected void btnExtract_Click(object sender, EventArgs e)
{
if (fuBrowse.HasFile)
{
string uploadedFile = Path.GetFileName(fuBrowse.PostedFile.FileName);
string location = Server.MapPath("~/ZipFiles/" + uploadedFile);
fuBrowse.SaveAs(location);
ZipFile fileToExtract = ZipFile.Read(location);
fileToExtract.Password = "";
fileToExtract.ExtractAll(Server.MapPath("~/extracted_file"), ExtractExistingFileAction.DoNotOverwrite);
gridviewExtractedFiles.DataSource = fileToExtract.Entries;
gridviewExtractedFiles.DataBind();
lblMessage.Text = "Archive extracted successfully and containes following files";
}
else
{
lblMessage.Text = "No File Choosen";
}
}
protected void btnZip_Click(object sender, EventArgs e)
{
if (fuBrowse.HasFile)
{
Response.ContentType = "application/zip";
Response.AddHeader("content-disposition",
"attachment;filename=a.zip");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
string fileName = Path.GetFileName(fuBrowse.PostedFile.FileName);
string fileLocation = Server.MapPath("~/zip_uploadedfile/" + fileName);
fuBrowse.SaveAs(fileLocation);
ZipFile createZipFile = new ZipFile();
if (txtPassword.Text == string.Empty)
{
lblMessage.Text = "Please put the password";
}
createZipFile.Password = txtPassword.Text.ToString();
createZipFile.Encryption = EncryptionAlgorithm.PkzipWeak;
createZipFile.AddFile(fileLocation, string.Empty);
createZipFile.Save(Server.MapPath("~/zip_zipfile/a.zip"));
gridviewExtractedFiles.DataSource = createZipFile.Entries;
gridviewExtractedFiles.DataBind();
lblMessage.Text = "Zip sucessfully created";
Response.TransmitFile("~/zip_zipfile/a.zip");
Response.End();
}
else
{
lblMessage.Text = "No File Choseen";
}
}