Nate Pet
Nate Pet

Reputation: 46322

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. 

Upvotes: 3

Views: 4861

Answers (5)

Mou
Mou

Reputation: 16312

this way we can write zip to output stream. may help

ZipFile zip = new ZipFile();
     List<Attachment> listattachments = email.Attachments;
        int acount = attachments.Count;
        for (int i = 0; i < acount; i++)
        {
            zip.AddEntry(attachments[i].FileName, listattachments[i].Content);
        }
        Response.Clear();
        Response.BufferOutput = false;
        string zipName = String.Format("{0}.zip", message.Headers.From.DisplayName);
        Response.ContentType = "application/zip";
        Response.AddHeader("content-disposition", "attachment; filename=" + zipName);
        zip.Save(Response.OutputStream);
        Response.End();     

Upvotes: 1

Chris Fulstow
Chris Fulstow

Reputation: 41902

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

Upvotes: 5

Tom Chantler
Tom Chantler

Reputation: 14951

Try creating an ActionResult a bit like this: I'm not 100% sure about the line var fileData = ms; and i don't have access to a dev environment just now, but there should be enough for you to work it out.

public ActionResult DownloadZip()
{
    using (MemoryStream 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
      }
      byte[] fileData = ms.GetBuffer();// I think this will work. Last time I did it, I did something like this instead... Zip.CreateZip("LogPosts.csv", System.Text.Encoding.UTF8.GetBytes(csv));
      var cd = new System.Net.Mime.ContentDisposition
      {
          FileName = "Whatever.zip",
          // always prompt the user for downloading, set to true if you want 
          // the browser to try to show the file inline
          Inline = false,
      };
      Response.AppendHeader("Content-Disposition", cd.ToString());
      return File(fileData, "application/octet-stream");
      }
  }

Upvotes: 1

Thorsten Dittmar
Thorsten Dittmar

Reputation: 56727

You'd have to send the content of the memory stream back as the response:

using (MemoryStream 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
    }

    context.Response.ContentType = "application/zip";
    context.Response.AddHeader("Content-Length", ms.Size);
    context.Response.AddHeader("Content-disposition", "attachment; filename=MyZipFile.zip");
    ms.Seek(0, SeekOrigin.Begin);
    ms.WriteTo(context.Response.OutputStream);
}

Upvotes: 1

Kugel
Kugel

Reputation: 19864

If this is local. you will need to save the stream in to the file and call Process.Start on it.

If this is on server. Just write your ms into Response with appropriate mime type.

Upvotes: 1

Related Questions