Upload Multiple Files and Then Create a Zip File


This article explains how to upload multiple files and then create a Zip File.
Step 1
In today's application, we will use "Ionic.Zip.dll" and this will be available under the DotNetZip Library, you need to download it from this link: Download DotNetZip Developer's Kit.
After downloading it Unzip it so that the various libraries can be available for you.
Now create a new application in Visual Studio 2012.
Right-click on your application and then click on "Add reference".
zip 1.jpg
Step 2
Now in the Reference Manager Wizard click on the "Browse" button since we will add the reference from the Folder where we had stored the DotNetZip Library.
zip 2.jpg
Browse to the "Ionic.Zip.dll" and then click on the "Add" button, this will add this DLL file to your project.
zip 3.jpg
Step 3
Now we will add a File Upload, a Button, and two labels to our Application.
  1. <asp:FileUpload ID="fileupload" runat="server" class="multi" accept="gif|jpg|jpeg" maxlength="5" />  
  2. <asp:Button ID="createzip" Text="ZipFile" runat="server" OnClick="createzip_Click" />  
  3. <asp:Label ID="Label1" runat="server" ForeColor="#CC0000" />  
  4. <asp:Label ID="Label2" runat="server" ForeColor="#CC0000" /> 
After adding these, add a new folder to the application and name it "ZipFolder", this will be the folder where all the Zip Files will be stored.
Step 4
Now Add "System.IO" Assembly to your application and then add the following code to the aspx.cs file of your application.
  1. HttpFileCollection filecollect = Request.Files;  
  2. for (int i = 0; i < filecollect.Count; i++)  
  3. {  
  4.     HttpPostedFile hpf = filecollect[i];  
  5.     if (hpf.ContentLength > 0)  
  6.     {  
  7.         hpf.SaveAs(Server.MapPath("ZipFolder") + "\\" +  
  8.           System.IO.Path.GetFileName(hpf.FileName));  
  9.         Label2.Text += " <br/> <b>File: </b>" + hpf.FileName +  
  10.           " <b>Size:</b> " + hpf.ContentLength + " <b>Type:</b> " +  
  11.           hpf.ContentType + "File Uploaded!";  
  12.     }  
  13. }  
  14. string zipfilepath = Server.MapPath("~/ZipFolder/");  
  15. string[] x = Directory.GetFiles(zipfilepath);  
  16. using (Ionic.Zip.ZipFile compress = new Ionic.Zip.ZipFile())  
  17. {  
  18.     string dateofcreation = DateTime.Now.ToString("y");  
  19.     dateofcreation = dateofcreation.Replace("/""");  
  20.     compress.AddFiles(x, dateofcreation);  
  21.     compress.Save(Server.MapPath(dateofcreation + ".zip"));  
  22.     Label1.Text = "ZIP Created Successfully";  
  23. }  
  24. if (Label1.Text == "ZIP Created Successfully")  
  25. {  
  26.     Array.ForEach(Directory.GetFiles(zipfilepath),  
  27.       delegate(string deleteFile) { File.Delete(deleteFile); });  

Here HttpFileCollection is used to retrieve all the files that are uploaded. When the images are uploaded it will add the file in the Zip Folder. For naming the file it will check the System Current Date and will add ".Zip" as its extension. After creating the Zip File it will give you a message as "Zip Created Successfully".
On the Output Window first select the files by clicking on the "Browse" button, if you want to select more than one file then you need to click on the Browse button again.
After selecting all the files, click on the "Zip" button, it will create the Zip File of all the files selected.