Friday, May 3, 2013

How To- Create Zip FIle in ASP.Net Using C#, VB.Net

Zip File in Asp.Net
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-
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.
Create Zip File in ASP.Net


<asp:fileupload id="FileUpload1" runat="server"/>
<asp:button id="btnUpload" onclick="btnUpload_Click" runat="server" text="Upload and Create Zip"/>
Now write the following code on button's click event-
C# Code

protected void btnUpload_Click(object sender, EventArgs e)
     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
       //Create the Zip of uploaded file
       using (ZipFile objZip = new ZipFile())
          //Adding the file
          //Save the Zip file on required location
        //Delete the file from Server after Createing Zip

Protected Sub btnUpload_Click(sender As Object, e As EventArgs)
    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
 'Create the Zip of uploaded file
 Using objZip As New ZipFile()
 'Adding the file
 'Save the Zip file on required location
 End Using
 'Delete the file from Server after Createing Zip
   End If
Catch generatedExceptionName As Exception
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

If You Enjoyed This Post Please Take 5 Seconds To Share It.

^ Scroll to Top hgpromo