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-
Place one FileUpload control on aspx page and one button to upload files and create zip file in it's Click Event.
ASPX Page
Now write the following code on button's click event-
C# Code
VB.Net
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.
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-
1 | using Ionic.Zip; |
ASPX Page
1 2 3 | < asp:fileupload id = "FileUpload1" runat = "server" /> < asp:button id = "btnUpload" onclick = "btnUpload_Click" runat = "server" text = "Upload and Create Zip" /> </ asp:button /> |
C# Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | 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 ; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | Protected 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 |
No comments:
Post a Comment