Click here to Skip to main content
15,919,422 members
Articles / Web Development / ASP.NET

Uploading a file and creating a Zip File in ASP.NET with C#

Rate me:
Please Sign up or sign in to vote.
5.00/5 (11 votes)
18 Jul 2013CPOL1 min read 29.1K   19   3
Uploading a file and creating a Zip file in ASP.NET with C#.


Here you will get to know how to generate a zip file in ASP.NET using C#. To implement the zip file first kindly, upload the Inoc.Zip.dll from DotNetZip library and then attach the same to the bin folder like, go to solution explorer – click on bin – attach the in the bin folder. And create a new folder named ZipMyFiles in the current project.

Using the code  

Now implement the code with the uploading file and zip files. Here you can upload any sought of file with extension .jpg, .pdf, .mp3 etc.

<asp:FileUpload ID="fuPhoto" runat="server" />
<asp:Button ID="btnZipFile" Text="ZipFile" runat="server" OnClick="btnZipFile_Click" />
<asp:Label ID="myLabel" runat="server" ForeColor="#CC0000" /> 

Now in code behind we implement the btnZipFile click event.Initially check whether the file upload has a file or not and save the selected file onto the temporary folder named ZipMyFiles. At the end delete all the files which were created on the ZipMyFile. If any more files exist in the ZipMyFiles folder then those files would be deleted at the end but in while creating the zip whatever files exist on that folder it would be added on to the zip file named with the current care. If again you execute the same on the same day then the previous data would be overwritten.

if (fuPhoto.HasFile)
    string path = Server.MapPath("~/ZipMyFiles/" + 
    string NewPath = Server.MapPath("~/ZipMyFiles/");
    string[] filenames = Directory.GetFiles(NewPath);
    using (Ionic.Zip.ZipFile zip = new Ionic.Zip.ZipFile())
        string date = DateTime.Now.ToString("d");
        date = date.Replace("/", "");
        zip.AddFiles(filenames, date);
        zip.Save(Server.MapPath(date + ".zip"));
        myLabel.Text = "ZIP File Created Successfully";
    if (myLabel.Text == "ZIP File Created Successfully")
          delegate(string deleteFile) { File.Delete(deleteFile); });

Note: You can Zip/Upload up to 4MB by default. If you want to go with more with the size then you need to increase maxRequestLength and exexutionTimout in web.config - based on your requirement. 

    <httpRuntime executionTimeout="240" maxRequestLength="40960"  />

Here I had given the execution time for processing this we had taking up to 4 minutes and the max request length with 40MB.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
Web Developer
India India
I am a 29 year old software Web Developer from Hyderabad, India. I have been working since approximately age 25. Where as in IT Development industry since 27. I am Microsoft Certified Technology Specialist.

I have taught myself in development, beginning with Microsoft's technologies ASP.NET, Approximately 3 years ago, I was given an opportunity to work as a freelance in the tech field. Now I am working as a web developer where my roles make me purely in web based technology solutions which manage and control access to applications and patient information stored in legacy systems, client-server applications.

I too had an opportunity to train some IT professionals with technical skills in development area. Which became my passion.

I have worked on various .NET framework versions(2.0 , 3.5, 4.0) and have been learning every new technology being introduced. Currently, I am looking forward to working in R & D in .Net to create distributed, reusable applications.

Comments and Discussions

GeneralMy vote of 5 Pin
Mulukala Lakshmi24-Jul-13 2:22
Mulukala Lakshmi24-Jul-13 2:22 
QuestionNice One Pin
crazie.coder19-Jul-13 1:59
professionalcrazie.coder19-Jul-13 1:59 
GeneralMy vote of 5 Pin
Sanjay K. Gupta18-Jul-13 3:20
professionalSanjay K. Gupta18-Jul-13 3:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.