How to create zip file to download asp.net - C#

Today i want to show you how to create file on the fly from server side and pass it to client to download. The idea is to create the zip file when the button clicked, and then save the zip file and pass the zip file to client side. After file finish to download, you may be delete the zip file to reduce the resource of the server.

Some application will allow user to download multiple file, but to download file by clicking one by one button is not necessary as you can zip the file and download all the file as one single zip file.

In this tutorial i will use library DotNet Zip Library :

This is The Sample program , you can freely enhance or change the part of the program to meet your program requirement.

The ASPX Page

 <asp:Label ID="Label1" Text="DirectoryPath : " runat="server"></asp:Label>
    <asp:TextBox ID="TextBox1" runat="server" Height="25px" Width="362px"></asp:TextBox>
    <br />
    <asp:Button ID="Button1" runat="server" Text="Download"
        onclick="Button1_Click" />

Code Behind ASPX Page

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string path = TextBox1.Text;
            string[] FileList = Directory.GetFiles(path);
            string[] directoryList = Directory.GetDirectories(path);
            using (ZipFile zip = new ZipFile())
            {
                AddFileToZip(zip, FileList); //add root file to zip
                for (int level = 0; level < directoryList.Length; level++)
                {
                    AddFolderToZip(zip, directoryList[level]);
                }

                zip.Save(Server.MapPath("~") + "MyZipFile.zip");              

                HttpContext.Current.Response.ContentType = "application/zip";
                HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=\"MyZipFile.zip\"");
                HttpContext.Current.Response.WriteFile(Server.MapPath("~") + "MyZipFile.zip");
            }
        }
        bool HasSubfoldersAlternate(string path)
        {
            IEnumerable<string> subfolders = Directory.EnumerateDirectories(path);
            return subfolders != null;
        }
        void AddFileToZip(ZipFile zip, string[] listFile)
        {
            for (int i = 0; i < listFile.Length; i++)
            {
                zip.AddFile(listFile[i]);
            }
        }
        void AddFolderToZip(ZipFile zip, string path)
        {
            string[] list = Directory.GetDirectories(path);
            if (list.Length > 0)
            {
                for (int i = 0; i < list.Length; i++)
                {
                    if (HasSubfoldersAlternate(list[i].ToString()))
                    {
                        AddFolderToZip(zip, list[i]);
                    }
                    else
                    {
                        AddFileToZip(zip, Directory.GetFiles(path));

                    }
                }
            }

            AddFileToZip(zip, Directory.GetFiles(path));//to add files from current folder

        }

Have a try. Feel free to check on How to create zip file to download in JSP- Servlet


By
NOTE : – If You have Found this post Helpful, I will appreciate if you can Share it on Facebook, Twitter and Other Social Media Sites. Thanks =)

Popular posts from this blog

Example to disable save as certain file type in SSRS Report Viewer

Phone book application example - Java

Modal Popup Message Box ASP.NEt C# Example