Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using memorystream and DotNetZip in MVC gives "Cannot access a closed Stream"

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.

like image 886
Frode Lillerud Avatar asked Oct 16 '12 16:10

Frode Lillerud


1 Answers

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");
}
like image 156
Darin Dimitrov Avatar answered Sep 27 '22 18:09

Darin Dimitrov