DEV Community

Cover image for Create ZIP files on HTTP request without intermediate files using ASP.NET MVC Framework
Niels Swimburger.NET ๐Ÿ”
Niels Swimburger.NET ๐Ÿ”

Posted on • Originally published at swimburger.net on

Create ZIP files on HTTP request without intermediate files using ASP.NET MVC Framework

.NET has multiple built-in APIs to create ZIP files. The ZipFile class has static methods to create and extract ZIP files without dealing with streams and byte-arrays. ZipFile is a great API for simple use-cases where the source and target are both on disk.

On the other hand, the ZipArchive class uses streams to read and write ZIP files. The latter is more complicated to use but provides more flexibility because you can accept any stream to read from or write to whether the data comes from disk, from an HTTP request, or from a complicated data pipeline.

Using ZipArchive you can create a ZIP file on the fly and send it to the client from ASP.NET without having to save the ZIP file to disk.

In this blog post, you'll learn how to do just that from an ASP.NET MVC Action. But first, let's learn how to create the ZIP archive.

You can find all the source code on this GitHub Repository.

Create a ZIP using streams and the ZipArchive class

For this demo, I have downloaded all the .NET bots from the .NET Brand repository and put them in a folder:

Screenshot of folder full of .NET bots

The following code will put all these files into a ZIP archive stored in a MemoryStream:

var botFilePaths = Directory.GetFiles("/path/to/bots");
using (var zipFileMemoryStream = new MemoryStream())
{
    using (ZipArchive archive = new ZipArchive(zipFileMemoryStream, ZipArchiveMode.Update, leaveOpen: true))
    {
        foreach (var botFilePath in botFilePaths)
        {
            var botFileName = Path.GetFileName(botFilePath);
            var entry = archive.CreateEntry(botFileName);
            using (var entryStream = entry.Open())
            using (var fileStream = System.IO.File.OpenRead(botFilePath))
            {
                await fileStream.CopyToAsync(entryStream);
            }
        }
    }

    zipFileMemoryStream.Seek(0, SeekOrigin.Begin);
    // use stream as needed
}

Enter fullscreen mode Exit fullscreen mode

The code does the following:

  • Get all paths to the bot files in the folder using Directory.GetFiles and store it in botFilePaths.
  • You can use any stream which supports synchronous read and write operations, but for this snippet a MemoryStream is used which will store the entire ZIP file in memory. This can consume a lot of memory which is why a different stream will be used in later samples. But for now, create a new MemoryStream and store it in zipFileMemoryStream. The ZIP archive will be written to zipfileMemoryStream. You can also use a FileStream instead of a MemoryStream if you want to write the ZIP archive to a file on disk.
  • Create a new ZipArchive and store it in archive. Instantiate the ZipArchive with the following parameters
    • stream : pass in zipFileMemoryStream. The ZipArchive wraps the stream and uses it to read/write.
    • mode : pass in ZipArchiveMode.Update to allow both reading and writing. Other options are Read and Create
    • leaveOpen : pass in true to leave open the underlying stream after disposing the ZipArchive. Otherwise zipfileMemoryStream will be disposed when ZipArchive is disposed which prevents you from interacting with the stream.
  • For every path in botFilePaths:
    • Extract the file name from the bot file path and store it in botFileName.
    • Create a ZipArchiveEntry by calling archive.CreateEntry passing in the file name. This entry is where the file data will be stored.
    • Get a writable Stream to write to the ZipArchiveEntry by calling entry.Open().
    • Open a readable FileStream to read the bot file by calling System.IO.File.OpenRead passing in the bot file path. At this point, you don't need to use the full namespace, but it will be required later to avoid collision between System.IO.File and the File method inside of MVC controllers and Razor Pages.
    • Copy the data from the file stream to the zip entry stream.
  • If you now want to read from the memory stream, you have to set the position of the stream back to the beginning. You can do this using .Seek(0, SeekOrigin.Begin).
  • Lastly, do whatever you need to do with your in-memory ZIP file.

Warning: Your mileage may vary depending on the size and amount of files you're trying to archive and the amount of resources available on your machine. Saving the ZIP file to disk may be favorable in some scenarios so you can save some rework when the same files are requested again.

If you eventually end up saving or downloading this ZIP archive to disk, it will look like this:

Screenshot of opening a ZIP file using Windows file explorer

Now that you know how to generate a ZIP file into a stream using ZipArchive and FileStreams, let's take a look at how you can send the stream to the browser using ASP.NET MVC Framework.

Create a ZIP file and send it to the browser from an ASP.NET MVC Controller

Version 1

The solution below (first version) stores the entire ZIP file in memory (MemoryStream) before sending the entire file to the client. Feel free to skip to the next section to see the improved version.

The code below shows how you can send the zipFileMemoryStream from the previous code sample to the browser using the File method inherited from the Controller class:

using System;
using System.IO;
using System.IO.Compression;
using System.Web.Mvc;

namespace WebZipItFramework.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult ZipUnoptimized()
        {
            var contentPath = Server.MapPath("~/bots/");
            var files = Directory.GetFiles(contentPath);
            var zipFileMemoryStream = new MemoryStream();
            using (ZipArchive archive = new ZipArchive(zipFileMemoryStream, ZipArchiveMode.Update, leaveOpen: true))
            {
                foreach (var file in files)
                {
                    var entry = archive.CreateEntry(Path.GetFileName(file));
                    using (var entryStream = entry.Open())
                    using (var fileStream = System.IO.File.OpenRead(file))
                    {
                        fileStream.CopyTo(entryStream);
                    }
                }
            }

            zipFileMemoryStream.Seek(0, SeekOrigin.Begin);
            return File(zipFileMemoryStream, "application/octet-stream", "Bots.zip");
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

The majority of the code is the same as the prior sample, but there are a couple of changes to take note off:

  • The bot files have been stored in a folder in the ASP.NET project called "bots". To get the path to the folder, use the Server.MapPath method.
  • The zipFileMemoryStream is not in a using block anymore. That's because the stream will be returned to the invoker and disposed of later.
  • The File method inherited from the Controller is invoked which returns a FileStreamResult. You could create the FileStreamResult yourself, but the File method is provided as a more convenient way to do it. The File method uses the following signature with these parameters:
    • fileStream: Pass in the stream holding the data you want to send to the client which in this case is zipFileMemoryStream.
    • contentType: Provide the content-type of the file. Pass in "application/octet-stream" for binary files or any files you want to force the browser to download instead of display.
    • fileDownloadName: Tells the browser which file name to use for the downloaded file.
  • At the end of the ZipUnoptimized action, the FileStreamResult is returned to MVC and MVC will send the data to the client and dispose of the stream.

The associated view generates the link to the ZipUnoptimized action as follows:

<div class="text-center">
    <a class="btn btn-primary" href="@Url.Action("ZipUnoptimized")">
        Download .NET Bots unoptimized
    </a>
</div>
Enter fullscreen mode Exit fullscreen mode

To give this a try, download the sample repository and run it locally. Then, open the browser and browse to 'https://localhost:44334'.

On this page, click on the "Download .NET Bots unoptimized" which will execute the code above, and download the .NET Bots ZIP file.

Version 2: A more memory efficient way

Instead of writing the entire zip file into memory and then sending it to the browser, you can write to Response.OutputStream to send data to the client immediately.

For this data to be sent immediately, you need to set Response.BufferOutput to false.

Unfortunately, there's a bug in the ZipArchive implementation in .NET Framework which causes issues when passing in Response.OutputStream directly to the ZipArchive constructor.

That's why it is being wrapped in this PositionWrapperStream which resolves the issue. This solution was provided by svick on StackOverflow.

using System;
using System.IO;
using System.IO.Compression;
using System.Web.Mvc;

namespace WebZipItFramework.Controllers
{
    public class HomeController : Controller
    {
        // omitted existing code for brevity

        public void ZipOptimized()
        {
            Response.ContentType = "application/octet-stream";
            Response.Headers.Add("Content-Disposition", "attachment; filename=\"Bots.zip\"");
            Response.BufferOutput = false;

            var contentPath = Server.MapPath("~/bots/");
            var files = Directory.GetFiles(contentPath);
            using (ZipArchive archive = new ZipArchive(new PositionWrapperStream(Response.OutputStream), ZipArchiveMode.Create))
            {
                foreach (var file in files)
                {
                    var entry = archive.CreateEntry(Path.GetFileName(file));
                    using (var entryStream = entry.Open())
                    using (var fileStream = System.IO.File.OpenRead(file))
                    {
                        fileStream.CopyTo(entryStream);
                    }
                }
            }
        }
    }

    // from https://stackoverflow.com/a/21513194/2919731
    public class PositionWrapperStream : Stream
    {
        private readonly Stream wrapped;

        private long pos = 0;

        public PositionWrapperStream(Stream wrapped)
        {
            this.wrapped = wrapped;
        }

        public override bool CanSeek { get { return false; } }

        public override bool CanWrite { get { return true; } }

        public override long Position
        {
            get { return pos; }
            set { throw new NotSupportedException(); }
        }

        public override void Write(byte[] buffer, int offset, int count)
        {
            pos += count;
            wrapped.Write(buffer, offset, count);
        }

        public override void Flush()
        {
            wrapped.Flush();
        }

        protected override void Dispose(bool disposing)
        {
            wrapped.Dispose();
            base.Dispose(disposing);
        }

        // all the other required methods can throw NotSupportedException

        public override bool CanRead => throw new NotImplementedException();

        public override long Length => throw new NotImplementedException();

        public override long Seek(long offset, SeekOrigin origin)
        {
            throw new NotImplementedException();
        }

        public override void SetLength(long value)
        {
            throw new NotImplementedException();
        }

        public override int Read(byte[] buffer, int offset, int count)
        {
            throw new NotImplementedException();
        }
    }
}

Enter fullscreen mode Exit fullscreen mode

There are some important differences compared to the previous solution to take note off:

  • There's no return type because the File method and FileStreamResult class is not being used anymore. Instead of using File and FileStreamResult, the data will be written to the body inside the action.
  • Instead of passing the filename and content-type to the File method, the headers are set directly on the Response object.
  • Instead of using ZipArchiveMode.Update, you need to use ZipArchiveMode.Create.
  • Instead of using leaveOpen: true, the parameter is omitted and the stream will be disposed of when the ZipArchive is disposed.
  • The .Seek and File method are no longer necessary.

The associated view generates the link to the ZipOptimized action as follows:

<div class="text-center">
    <a class="btn btn-primary" href="@Url.Action("ZipUnoptimized")">
        Download .NET Bots unoptimized
    </a>
    <a class="btn btn-primary" href="@Url.Action("ZipOptimized")">
        Download .NET Bots optimized
    </a>
</div>

Enter fullscreen mode Exit fullscreen mode

To give this a try, download the sample repository and run it locally. Then, open the browser and browse to 'https://localhost:44334'.

On this page, click on the "Download .NET Bots optimized" which will execute the code above, and download the .NET Bots ZIP file.

Comparing version 1 and 2

The biggest downside of the first version is that the entire ZIP file is being stored in memory as it is generated and then send to the client. As more bot files are stored in the archive, more memory is accumulated.

On the other hand, the second version writes file by file to the output stream and ASP.NET takes care of streaming the data to the client. This prevents the data from accumulating in memory.

The difference is huge, especially with lots of large files.

From a user experience standpoint, the unoptimized version will result in the browser waiting for a long time while the optimized version will pop up the save file dialog immediately and stream file by file.

Summary

The ZipArchive wraps any stream to read, create, and update ZIP archives whether the data is coming from disk, from an HTTP request, from a long data-pipeline, or from anywhere else. This makes ZipArchive very flexible and you can avoid saving a ZIP file to disk as an intermediary step. You can send the resulting stream to the browser using ASP.NET MVC as demonstrated above.

Top comments (0)