5

I'm trying to create a zipfile in a MVC method using the DotNetZip components.

Here is my code:

    public FileResult DownloadImagefilesAsZip()
    {
        using (var memoryStream = new MemoryStream())
        {
            using (var zip = new ZipFile())
            {
                zip.AddDirectory(Server.MapPath("/Images/"));
                zip.Save(memoryStream);

                return File(memoryStream, "gzip", "images.zip");
            }
        }
    }

When I run it I get a "Cannot access a closed Stream" error, and I'm not sure why.

Frode Lillerud
  • 7,324
  • 17
  • 58
  • 69

2 Answers2

15

Don't dispose the MemoryStream, the FileStreamResult will take care once it has finished writing it to the response:

public ActionResult DownloadImagefilesAsZip()
{
    var memoryStream = new MemoryStream();
    using (var zip = new ZipFile())
    {
        zip.AddDirectory(Server.MapPath("~/Images"));
        zip.Save(memoryStream);
        return File(memoryStream, "application/gzip", "images.zip");
    }
}

By the way I would recommend you writing a custom action result to handle this instead of writing plumbing code inside your controller action. Not only that you will get a reusable action result but bear in mind that your code is hugely inefficient => you are performing the ZIP operation inside the memory and thus loading the whole ~/images directory content + the zip file in memory. If you have many users and lots of files inside this directory you will very quickly run out of memory.

A much more efficient solution is to write directly to the response stream:

public class ZipResult : ActionResult
{
    public string Path { get; private set; }
    public string Filename { get; private set; }

    public ZipResult(string path, string filename)
    {
        Path = path;
        Filename = filename;
    }

    public override void ExecuteResult(ControllerContext context)
    {
        if (context == null)
        {
            throw new ArgumentNullException("context");
        }

        var response = context.HttpContext.Response;
        response.ContentType = "application/gzip";
        using (var zip = new ZipFile())
        {
            zip.AddDirectory(Path);
            zip.Save(response.OutputStream);
            var cd = new ContentDisposition
            {
                FileName = Filename,
                Inline = false
            };
            response.Headers.Add("Content-Disposition", cd.ToString());
        }
    }
}

and then:

public ActionResult DownloadImagefilesAsZip()
{
    return new ZipResult(Server.MapPath("~/Images"), "images.zip");
}
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • 1
    I had to add `memoryStream.Seek(0, SeekOrigin.Begin);` before `return File(...);` to get it to work. – pqvst Nov 17 '14 at 15:18
0

Couldn't comment.

Darin's answer is great! Still received a memory exception though so had to add response.BufferOutput = false; and because of that had to move content-disposition code higher.

So you have:

...
        var response = context.HttpContext.Response;
        response.ContentType = "application/zip";
        response.BufferOutput = false;

        var cd = new ContentDisposition
        {
            FileName = ZipFilename,
            Inline = false
        };
        response.Headers.Add("Content-Disposition", cd.ToString());

        using (var zip = new ZipFile())
        {
...

Just in case it wasn't obvious :)

Quinton Smith
  • 2,550
  • 18
  • 17