Click here to Skip to main content
13,794,368 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


19 bookmarked
Posted 18 Jul 2013
Licenced CPOL

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

, 18 Jul 2013
Rate this:
Please Sign up or sign in to vote.
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)


About the Author

Mannava Siva Aditya
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.

You may also be interested in...


Comments and Discussions

GeneralMy vote of 5 Pin
Mulukala Lakshmi24-Jul-13 3:22
memberMulukala Lakshmi24-Jul-13 3:22 
QuestionNice One Pin
crazie.coder19-Jul-13 2:59
professionalcrazie.coder19-Jul-13 2:59 
Nice article .
I've implemented this code when it ran successfully ,then i modified code for multifile uploder..
and done it's successfully...
Smile | :) Smile | :)
eNJOY c0ding....

modified 24-Jul-13 3:37am.

GeneralMy vote of 5 Pin
Sanjay K. Gupta18-Jul-13 4:20
professionalSanjay K. Gupta18-Jul-13 4: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.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web06 | 2.8.181207.3 | Last Updated 19 Jul 2013
Article Copyright 2013 by Mannava Siva Aditya
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid