Thursday, 24 October 2013

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

Leave a Comment
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 =)

0 comments:

Post a Comment

Subscribe to our newsletter to get the latest updates to your inbox.

Your email address is safe with us!




Founder of developersnote.com, love programming and help others people. Work as Software Developer. Graduated from UiTM and continue study in Software Engineering at UTMSpace. Follow him on Twitter , or Facebook or .



Powered by Blogger.