Create Zip file in MVC Using C#

In this article I will explain how to download multiple file As Zip Archive file in ASP.Net MVC.


First,Go to Visual Studio, open new ASP.NET Web application with relavent name.


Go to Controller And Add Controller for to implement Action method.


Then you need to Add DotNetZip library to application.For this Go to ->

Tools >>NuGet Package Manager >> Manage NuGet Packages for solution.

It will open NuGet – Solution tab and browse for DotNetZip Package and install it in project as following:

DotnetZip Package


Add class for File as FileModel as following:

namespace CreateZipfile.Models
public class FileModel
public string FileName { get; set; }
public string FilePath { get; set; }
public bool IsSelected { get; set; }


Add following method in ZipFile controller,

using CreateZipfile.Models;
using Ionic.Zip;
using System;
using System.Collections.Generic;
using System.IO;
using System.Web.Mvc;

namespace CreateZipfile.Controllers
    public class ZipFileController : Controller
        // GET: ZipFile
        public ActionResult Index()
            string[] filePaths = Directory.GetFiles(Server.MapPath("~/Files/"));
            List<FileModel> files = new List<FileModel>();
            foreach (string filePath in filePaths)
                files.Add(new FileModel()
                    FileName = Path.GetFileNameWithoutExtension(filePath),
                    FilePath = filePath
            return View(files);
        public ActionResult Index(List<FileModel> files)
            using (ZipFile zip = new ZipFile())
                List<string> filelist = new List<string>();
                zip.AlternateEncodingUsage = ZipOption.AsNecessary;
                foreach (FileModel file in files)
                    if (file.IsSelected)
                        zip.AddFile(file.FilePath, "Files");
                string filenames =string.Join("&",filelist.ToArray());
                string zipFileName = String.Format("ZipFile_{0}.zip", filenames);
                using (MemoryStream memoryStream = new MemoryStream())
                    return File(memoryStream.ToArray(), "application/zip", zipFileName);

DownloadZip()  => method return file which are available in Files Folder.

Downloadzip(List<FileModel> files) , This method convert all selected file to zip file(compressed file).

filelist contains name of selected file.

zipName  contains all name of selected filename with (&) join.


Add View for This method As below:

@using CreateZipfile.Models
@model List<FileModel>
    ViewBag.Title = "Index";
<!DOCTYPE html>
    <meta name="viewport" content="width=device-width" />
        table td, th {
            border: 1px solid #b5b0b0;
            padding: 10px;

    @using (Html.BeginForm("Index", "ZipFile", FormMethod.Post))
                <th>File Name</th>
            @for (int i = 0; i < Model.Count(); i++)
                    <td>@Html.CheckBoxFor(model => model[i].IsSelected)</td>
                        @Html.HiddenFor(model => model[i].FilePath)
                        @Html.HiddenFor(model => model[i].FileName)
        <button id="download" value="Download Zip" >Download Zip</button>


Submit a Comment

Your email address will not be published. Required fields are marked *


Select Categories