Upload Multiple Files and Then Create a Zip File

Introduction

This article explains about 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, thus will add this DLL file to your project.

zip 3.jpg

Step 3

Now we will add a File Upload, a Button and two label's to our Application.

        <asp:FileUpload ID="fileupload" runat="server" class="multi" accept="gif|jpg|jpeg" maxlength="5" />

<asp:Button ID="createzip" Text="ZipFile" runat="server" OnClick="createzip_Click" />

<asp:Label ID="Label1" runat="server" ForeColor="#CC0000" />

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

            HttpFileCollection filecollect = Request.Files;

            for (int i = 0; i < filecollect.Count; i++)

            {

                HttpPostedFile hpf = filecollect[i];

                if (hpf.ContentLength > 0)

                {

                    hpf.SaveAs(Server.MapPath("ZipFolder") + "\\" +

                      System.IO.Path.GetFileName(hpf.FileName));

                    Label2.Text += " <br/> <b>File: </b>" + hpf.FileName +

                      " <b>Size:</b> " + hpf.ContentLength + " <b>Type:</b> " +

                      hpf.ContentType + "File Uploaded!";

                }

            }

            string zipfilepath = Server.MapPath("~/ZipFolder/");

            string[] x = Directory.GetFiles(zipfilepath);

            using (Ionic.Zip.ZipFile compress = new Ionic.Zip.ZipFile())

            {

                string dateofcreation = DateTime.Now.ToString("y");

                dateofcreation = dateofcreation.Replace("/", "");

                compress.AddFiles(x, dateofcreation);

                compress.Save(Server.MapPath(dateofcreation + ".zip"));

                Label1.Text = "ZIP Created Successfully";

            }

            if (Label1.Text == "ZIP Created Successfully")

            {

                Array.ForEach(Directory.GetFiles(zipfilepath),

                  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 it's extension. After creating the Zip File it will give you a message as "Zip Created Successfully".

Output

On the Output Window first select the files by clicking on the "Browse" button, if you want to select more then one file then you need to click on the Browse button again.

anu1.jpg

After selecting all the files, click on the "Zip" button, it will create the Zip File of all the files selected.

anu2.jpg

Up Next
    Ebook Download
    View all

    milton

    Read by 0 people
    Download Now!
    Learn
    View all