Code2night
  • Home
  • Guest Posts
  • Tutorial
  • Languages
    • Angular
    • C
    • C#
    • HTML/CSS
    • Java
    • JavaScript
    • Node.js
    • Python
    • React
    • SQL Server
    • TypeScript
  • Post Blog
  • Tools
    • JSON Beautifier
    • HTML Beautifier
    • XML Beautifier
    • CSS Beautifier
    • JS Beautifier
    • PDF Editor
  • Register
  • Login
  1. Home
  2. Blogpost

Download Files as Zip file in Asp.Net

Date- May 21,2023

7137

Free Download Pay & Download
ZipArchieve AspNet MVC

Hello guys and welcome to Code2night! In today's article, we'll explore a common requirement in Asp.Net MVC where we often need to download all files as a zip. Fortunately, with the help of the ZipArchive class, accomplishing this task becomes a breeze. Throughout this article, we will guide you through the necessary steps to download files as a zip archive in your Asp.Net MVC application. So, let's dive right in and learn how to efficiently handle file downloads in zip format using the ZipArchive feature.

For downloading the files in zip format we will use ZipArchieve class. For using this we have to install the following Nuget package.

asp.netOnce you install this package you can go to the controller and add the following namespace


using System.IO;
using System.IO.Compression;

Now, we have to add the following code for download files as a zip


public ActionResult DownloadFilesAsZip()
        {
            // Get the paths of the files to be included in the zip
            string[] filePaths = new string[]
            {
            Server.MapPath("~/Content/Screenshot_1.png"),
            Server.MapPath("~/Content/Screenshot_2.jpg"),
            Server.MapPath("~/Content/Screenshot_3.png")
            };

            // Create a memory stream to store the zip file
            MemoryStream memoryStream = new MemoryStream();

            using (ZipArchive zipArchive = new ZipArchive(memoryStream, ZipArchiveMode.Create, true))
            {
                // Add each file to the zip archive
                foreach (string filePath in filePaths)
                {
                    string fileName = Path.GetFileName(filePath);
                    zipArchive.CreateEntryFromFile(filePath, fileName);
                }
            }

            // Set the position of the memory stream back to the beginning
            memoryStream.Position = 0;

            // Return the zip file for download
            return File(memoryStream, "application/zip", "Files.zip");
        }

So, in the file paths, you can add any file paths that you want to add inside the zip file.

Now we will add a link on the view to call this method.

@Html.ActionLink("Download Files as Zip", "DownloadFilesAsZip", "Home")

So, Now you can run the application and you have to click on the download button and it will download files as zip.

In the screenshots, you can see the files getting downloaded as zip files.

In this example, the DownloadFilesAsZip action method takes an array of file paths and creates a zip archive using ZipArchive. Each file is added to the archive using CreateEntryFromFile

The resulting zip archive is then stored in a MemoryStream, and the position of the stream is set back to the beginning before returning the file using the File method. The File method takes the MemoryStream, the content type (in this case, "application/zip"), and the desired file name for the download.

So this is how we can Download Files as a Zip file in Asp.Net.

S
Shubham Batra
Programming author at Code2Night — sharing tutorials on ASP.NET, C#, and more.
View all posts →

Related Articles

How to Convert Text to Speech in Asp.Net
Nov 06, 2023
How to export table data in pdf using itextsharp in asp.net mvc
Sep 16, 2023
How to generate pdf using itextsharp in asp.net mvc
Aug 06, 2023
Status Code 413 Request Entity Too Large
Jul 02, 2023

Comments

Tags

Swagger UI
Swashbuckle
SwashbuckleAspNetCore
Rest API
Postman
Api Testing
ITextSharp
Export to Pdf
AspNet Core
AspNet
C#
View to Pdf in Aspnet
Scheduler
Fibonacci series in Java
Display Fibonacci Series
First C# Program
What is C?
C
C Programming
CodeLobster
Free Download for Youtube Subscribers!

First click on Subscribe Now and then subscribe the channel and come back here.
Then Click on "Verify and Download" button for download link

Subscribe Now | 1760
Download
Support Us....!

Please Subscribe to support us

Thank you for Downloading....!

Please Subscribe to support us

Continue with Downloading
Be a Member
Join Us On Whatsapp Join Us On Facebook
Code2Night

A community platform for sharing programming knowledge, tutorials, and blogs. Learn, write, and grow with developers worldwide.

Panipat, India   info@code2night.com

Quick Links
  • Home
  • Blogs
  • Tutorials
  • About Us
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Guest Posts
Dev Tools
  • JSON Beautifier
  • HTML Beautifier
  • XML Beautifier
  • CSS Beautifier
  • JS Beautifier
  • PDF Editor
By Language
  • Angular
  • C
  • C#
  • HTML/CSS
  • Java
  • JavaScript
  • Node.js
  • Python
  • React
  • SQL Server
  • TypeScript
© 2026 Code2Night. All Rights Reserved.
Built with for developers