In this article I will explain with an example, how to create a ZIP file in ASP.Net MVC Razor.
Multiple files will be compressed into a ZIP file Archive using the DotNetZip Library in ASP.Net MVC Razor.
 
 
Adding reference of DotNetZip Library
The very first thing you will need to add reference of the DotNetZip Library in ASP.Net MVC project.
This can be done by right clicking References in the Solution Explorer and then clicking Add Reference option in the Context Menu and finally selecting the DotNetZip Library DLL.
Create ZIP File in ASP.Net MVC
 
 
Storage of Files on Server Directory
In my project, I have created a Folder named Files which contains the following files as shown in the screenshot below.
Create ZIP File in ASP.Net MVC
 
 
Model
The Model class consists of the following three properties.
public class FileModel
{
    public string FileName { get; set; }
    public string FilePath { get; set; }
    public bool IsSelected { get; set; }
}
 
 
Namespaces
You will need to import the following namespaces.
using System.IO;
using Ionic.Zip;
 
 
Controller
The Controller consists of two Action methods.
Action method for handling GET operation
Inside this Action method, the files are read from the Folder and the details of the File such as the Name and its Path are added to a Generic List collection of the FileModel class.
Finally the Generic List collection of the FileModel class is returned to the View.
 
Action method for handling POST operation
This action method handles the POST operation and when the form is submitted, a Generic List collection of the FileModel class object is received as parameter in this method.
First an object of the DotNetZip Library is created and a loop is executed over the objects of the received Generic List collection of the FileModel class.
If the IsSelected property is True i.e. the CheckBox is checked for the file, then the file is added to the object of the DotNetZip Library.
Finally the DotNetZip Library object is downloaded as Zip file.
public class HomeController : Controller
{
    // GET: Home
    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.GetFileName(filePath),
                FilePath = filePath
            });
        }
 
        return View(files);
    }
 
    [HttpPost]
    public ActionResult Index(List<FileModel> files)
    {
        using (ZipFile zip = new ZipFile())
        {
            zip.AlternateEncodingUsage = ZipOption.AsNecessary;
            zip.AddDirectoryByName("Files");
            foreach (FileModel file in files)
            {
                if (file.IsSelected)
                {
                    zip.AddFile(file.FilePath, "Files");
                }
            }
            string zipName = String.Format("Zip_{0}.zip", DateTime.Now.ToString("yyyy-MMM-dd-HHmmss"));
            using (MemoryStream memoryStream = new MemoryStream())
            {
                zip.Save(memoryStream);
                return File(memoryStream.ToArray(), "application/zip", zipName);
            }
        }
    }
}
 
 
View
Inside the View, the FileModel class is declared as IEnumerable which specifies that it will be available as a Collection.
For displaying the records, an HTML Table is used. A loop will be executed over the Model which will generate the HTML Table rows with the File records.
The View consists of an HTML Form which has been created using the Html.BeginForm method with the following parameters.
ActionName – Name of the Action. In this case the name is Index.
ControllerName – Name of the Controller. In this case the name is Home.
FormMethod – It specifies the Form Method i.e. GET or POST. In this case it will be set to POST.
There is also Submit button which when clicked, the Form gets submitted.
@using Zip_Files_MVC.Models
@model List<FileModel>
 
@{
    Layout = null;
}
 
<!DOCTYPE html>
 
<html>
<head>
    <meta name="viewport" content="width=device-width"/>
    <title>Index</title>
</head>
<body>
    @using (Html.BeginForm("Index", "Home", FormMethod.Post))
    {
        <table cellpadding="0" cellspacing="0">
            <tr>
                <th></th>
                <th>File Name</th>
            </tr>
            @for (int i = 0; i < Model.Count(); i++)
            {
                <tr>
                    <td>@Html.CheckBoxFor(m => m[i].IsSelected)</td>
                    <td>
                        @Model[i].FileName
                        @Html.HiddenFor(m => m[i].FilePath)
                        @Html.HiddenFor(m => m[i].FileName)
                    </td>
                </tr>
            }
        </table>
        <br/>
        <input type="submit" value="Download"/>
    }
</body>
</html>
 
 
Screenshot
Create ZIP File in ASP.Net MVC
 
 
Downloads