Frode Lillerud
Frode Lillerud

Reputation: 7394

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.

Upvotes: 5

Views: 5483

Answers (2)

Quinton Smith
Quinton Smith

Reputation: 2670

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 :)

Upvotes: 0

Darin Dimitrov
Darin Dimitrov

Reputation: 1039318

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");
}

Upvotes: 15

Related Questions