How to upload file and create Zip file in asp.net using C#,VB.Net

Introduction: In previous articles i explained How to Drag & drop to upload multiple files using AjaxFileUpload like Facebook in asp.net and How to Validate and upload image files in asp.net and Bind,upload,download,delete image files from the GridView  and How to bind and implement search gridview records and How to create thumbnail, small and large version of the uploaded image in Asp.net and How to resize image in Asp.net and How to check and validate file extension before uploading a file through FileUpload control in asp.net
In this article I will explain with example How easily you can upload the file and create Zip archives file of that uploaded file.

Description and Implementation: We can create Zip files using DotNetZip Library. For this you need to add the reference of IconicZip.dll in to you website.

upload file and create zip file in asp.net example

Step 1: Download the IconicZip.dll from here and extract the folder. You will find some folders and files in that. 


Step 2: Just open the zip-v1.9 folder and copy the IconicZip.dll file from there and paste in your web site root directory. 

Step 3: Now in the solution explorer right click on your project name and click on Add Reference

Step 4: A new window will appear from where you have to browse and select the location of your IconicZip.dll file and then click on Add button and then Ok button. 

Add two folders in the root directory of your website. Right click on your project name in the solution explore and click Add -> New Folder. Rename this folder to Uploads. Similarly add another folder and name it ZippedFiles.

Uploads folder will have all the uploaded files (if not deleted after zipping).
ZippedFiles folder will have Zipped Files of the uploaded file.

C#.Net Code to upload file and create Zip archives in asp.net
  • In the design page (.aspx) place a FileUpload control to select the file and a Button control to upload the file that you want to Zip.
Source Code:

<fieldset style="width:410px;">
            <legend>Upload and Create Zip file Example in Asp.net</legend>
             <asp:FileUpload ID="FileUpload1" runat="server" />
        <asp:Button ID="btnUploadAndZip" runat="server" Text="Upload and Create Zip file" OnClick="btnUploadAndZip_Click" />
        </fieldset>

Include the following namespaces

using System.IO;
using Ionic.Zip;

and write the following code on Upload and Create Zip file’s  click event in the code behind file(.aspx.cs)

protected void btnUploadAndZip_Click(object sender, EventArgs e)
    {
        string strFilePath = string.Empty;
        try
        {
            if (FileUpload1.HasFile)
            {
                //Get path for saving the uploaded file
                strFilePath = Server.MapPath("~/Uploads/" + FileUpload1.FileName);
                //Saving the file in the server
                FileUpload1.SaveAs(strFilePath);
                //Create the Zip of uploaded file
                using (ZipFile objZip = new ZipFile())
                {
                    //Adding the uploaded file to create zip file
                    objZip.AddFile(strFilePath);
                    //Saving the Zipped file in  ZippedFiles folder in the root directory
                    objZip.Save(Server.MapPath("~/ZippedFiles/DemoZipFile.zip"));
                }
                //If you want to delete the file from Server after finishing the task of createing Zip then
                //write the  code mentioned below otherwise skip this  code.
                File.Delete(strFilePath);
            }
        }
        catch (Exception ex)
        {
            Response.Write("Folllowing Error Ocuured: " + ex.Message.ToString());
        }
        finally
        {
            strFilePath = string.Empty;
        }
    }

VB.Net Code to upload and  create Zip archives in asp.net
  • In the design page (.aspx) place a FileUpload control to select the file and a Button control to upload the file that you want to Zip.
   <fieldset style="width:410px;">
            <legend>Upload and Create Zip file Example in Asp.net</legend>
             <asp:FileUpload ID="FileUpload1" runat="server" />
        <asp:Button ID="btnUploadAndZip" runat="server" Text="Upload and Create Zip file" />
        </fieldset>

Import following namespaces

Imports Ionic.Zip
Imports System.IO

and write the following code on Upload and Create Zip file’s  click event in the code behind file(.aspx.vb)

Protected Sub btnUploadAndZip_Click(sender As Object, e As EventArgs) Handles btnUploadAndZip.Click
        Dim strFilePath As String = String.Empty
        Try
            If FileUpload1.HasFile Then
                'Get path for saving the uploaded file
                strFilePath = Server.MapPath("~/Uploads/" + FileUpload1.FileName)
                'Saving the file in the server
                FileUpload1.SaveAs(strFilePath)
                'Create the Zip of uploaded file
                Using objZip As New ZipFile()
                    'Adding the uploaded file to create zip file
                    objZip.AddFile(strFilePath)
                    'Saving the Zipped file in  ZippedFiles folder in the root directory
                    objZip.Save(Server.MapPath("~/ZippedFiles/DemoZipFile.zip"))
                End Using
                'If you want to delete the file from Server after finishing the task of createing Zip then
                'write the  code mentioned below otherwise skip this  code.
                File.Delete(strFilePath)
            End If
        Catch ex As Exception
            Response.Write("Folllowing Error Ocuured: " & ex.Message.ToString())
        Finally
            strFilePath = String.Empty
        End Try
    End Sub


Now over to you:

"If you like my work; you can appreciate by leaving your comments, hitting Facebook like button, following on Google+, Twitter, Linked in and Pinterest, stumbling my posts on stumble upon and subscribing for receiving free updates directly to your inbox . Stay tuned for more technical updates."
Previous
Next Post »

If you have any question about any post, Feel free to ask.You can simply drop a comment below post or contact via Contact Us form. Your feedback and suggestions will be highly appreciated. Also try to leave comments from your account not from the anonymous account so that i can respond to you easily..