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


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.Net

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

^ Scroll to Top