Here, I am explaining how to Create a zip file in ASp.Net Using C# and VB.Net. We can easily Create Zip Files Archives using DotNetZip Library.
In my previous posts, I explained Convert DataTable into List, Constructor Chainning in C#, Convert a Generic List to a Datatable, Get Property Names using Reflection in C#, Hard drive information using C#, Create Directory/Folder using C#, Check Internet Connection using C#, SQL Server Database BackUp using C# and some other articles related to C#, ASP.Net, jQuery, Java Script and SQL Server.
For creating zip file add the reference of Ionic.Zip.dll in your application and add following namespace-
ASPX Page
C# Code
In my previous posts, I explained Convert DataTable into List, Constructor Chainning in C#, Convert a Generic List to a Datatable, Get Property Names using Reflection in C#, Hard drive information using C#, Create Directory/Folder using C#, Check Internet Connection using C#, SQL Server Database BackUp using C# and some other articles related to C#, ASP.Net, jQuery, Java Script and SQL Server.
For creating zip file add the reference of Ionic.Zip.dll in your application and add following namespace-
using Ionic.Zip;Place one FileUpload control on aspx page and one button to upload files and create zip file in it's Click Event.
ASPX Page
<asp:fileupload id="FileUpload1" runat="server"/> <asp:button id="btnUpload" onclick="btnUpload_Click" runat="server" text="Upload and Create Zip"/> </asp:button/>Now write the following code on button's click event-
C# Code
protected void btnUpload_Click(object sender, EventArgs e)
{
try
{
string strFileName=string.Empty;
if (FileUpload1.HasFile)
{
//Get File Name
strFileName = FileUpload1.FileName;
//Get Location for saving uploaded file
string strFileLocation = Server.MapPath("~/UploadedFiles/"+strFileName);
//Saving the file in server
FileUpload1.SaveAs(strFileLocation);
//Create the Zip of uploaded file
using (ZipFile objZip = new ZipFile())
{
//Adding the file
objZip.AddFile(strFileLocation);
//Save the Zip file on required location
objZip.Save(Server.MapPath("~/UploadedFiles/MyZipFile.zip"));
}
//Delete the file from Server after Createing Zip
File.Delete(strFileLocation);
}
}
catch(Exception)
{
throw;
}
}
VB.NetProtected Sub btnUpload_Click(sender As Object, e As EventArgs)
Try
Dim strFileName As String = String.Empty
If FileUpload1.HasFile Then
'Get File Name
strFileName = FileUpload1.FileName
'Get Location for saving uploaded file
Dim strFileLocation As String = Server.MapPath("~/UploadedFiles/" & strFileName)
'Saving the file in server
FileUpload1.SaveAs(strFileLocation)
'Create the Zip of uploaded file
Using objZip As New ZipFile()
'Adding the file
objZip.AddFile(strFileLocation)
'Save the Zip file on required location
objZip.Save(Server.MapPath("~/UploadedFiles/MyZipFile.zip"))
End Using
'Delete the file from Server after Createing Zip
File.Delete(strFileLocation)
End If
Catch generatedExceptionName As Exception
Throw
End Try
End Sub
I hope this will be helpful for you. I would like to have any feedback from you. Your valuable feedback, question, or comments about this article are always welcome.


No comments:
Post a Comment