Search code examples
c#asp.net-mvcdotnetzip

c# - DotNetZip open zip file from MemoryStream


What I like to do is instead of storing a zip file on disk, I like to open it up from a MemoryStream.

I am looking at the documentation for DotNetZip programming example: Note that I tweaked it slightly based on what I thought may be needed.

    var ms = new MemoryStream();
    using (ZipFile zip = new ZipFile())
    {
       zip.AddFile("ReadMe.txt");
       zip.AddFile("7440-N49th.png");
       zip.AddFile("2008_Annual_Report.pdf");        
       zip.Save(ms); // this will save the files in memory steam
    }


  // now what I need is for the zip file to open up so that 
     the user can view all the files in it. Not sure what to do next after 
     zip.Save(ms) for this to happen. 

Solution

  • Try this:

    public ActionResult Index()
    {
        var memoryStream = new MemoryStream();
    
        using (var zip = new ZipFile())
        {
            zip.AddFile("ReadMe.txt");
            zip.AddFile("7440-N49th.png");
            zip.AddFile("2008_Annual_Report.pdf"); 
            zip.Save(memoryStream);
        }
    
        memoryStream.Seek(0, 0);
        return File(memoryStream, "application/octet-stream", "archive.zip");
    }