3

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. 
Thorsten Dittmar
  • 55,956
  • 8
  • 91
  • 139
Nate Pet
  • 44,246
  • 124
  • 269
  • 414
  • what do you mean by open up? open in another program? – Daniel A. White Dec 18 '12 at 15:19
  • What is the environment? Are you doing this in ASP.NET MVC for example? If so, I can post some example code. – Tom Chantler Dec 18 '12 at 15:19
  • @Dommer - Yes, it is in ASP.NET MVC C# - Thanks that would be very helpful – Nate Pet Dec 18 '12 at 15:23
  • @DanielA.White - When you click on a zip file, it open up. I need to do the same thing here. – Nate Pet Dec 18 '12 at 15:24
  • Just for the record, this is a followup question to [this one](http://stackoverflow.com/questions/13922342/how-to-open-up-a-zip-file-from-memorystream/13922437#13922437). It may help to read that one to get a better idea of what @WebDev is looking for. – Bobson Dec 18 '12 at 15:32

5 Answers5

5

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");
}
Chris Fulstow
  • 41,170
  • 10
  • 86
  • 110
  • i like to know why we need to return file this way because dotnetzip library support to write zip file directly to output stream. here is a small snip `zip.Save(Response.OutputStream);` would mind to explain the objective why you return zip file to client this way `return File(memoryStream, "application/octet-stream", "archive.zip");` please see my post here and answer my question if possible. thanks – Mou Apr 22 '15 at 11:21
1

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.

Kugel
  • 19,354
  • 16
  • 71
  • 103
1

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);
}
Thorsten Dittmar
  • 55,956
  • 8
  • 91
  • 139
1

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");
      }
  }
Tom Chantler
  • 14,753
  • 4
  • 48
  • 53
1

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();     
Mou
  • 15,673
  • 43
  • 156
  • 275