0

I am using DotNetZip.

What I need to do is to open up a zip files with files from the server. The user can then grab the files and store it locally on their machine.

What I did before was the following:

      string path = "Q:\\ZipFiles\\zip" + npnum + ".zip";
      zip.Save(path);
      Process.Start(path);

Note that Q: is a drive on the server. With Process.Start, it simply open up the zip file so that the user can access all the files. I like to do the same but not store the file on disk but show it from memory.

Now, instead of storing the zip file on the server, I like to open it up with MemoryStream

I have the following but does not seem to work

      var ms = new MemoryStream();
      zip.Save(ms);

but not sure how to proceed further in terms of opening up the zip file from a memory stream so that the user can access all the files

Fabio
  • 3,020
  • 4
  • 39
  • 62
Nate Pet
  • 44,246
  • 124
  • 269
  • 414
  • See other examples here - http://stackoverflow.com/questions/2324626/extract-a-zip-file-programmatically-by-dotnetzip-library – MethodMan Dec 17 '12 at 21:26

3 Answers3

1

Here is a live piece of code (copied verbatim) which I wrote to download a series of blog posts as a zipped csv file. It's live and it works.

public ActionResult L2CSV()
{
    var posts = _dataItemService.SelectStuff();
    string csv = CSV.IEnumerableToCSV(posts);
    // These first two lines simply get our required data as a long csv string
    var fileData = Zip.CreateZip("LogPosts.csv", System.Text.Encoding.UTF8.GetBytes(csv));
    var cd = new System.Net.Mime.ContentDisposition
    {
        FileName = "LogPosts.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
0

You can use:

zip.Save(ms);

// Set read point to beginning of stream
ms.Position = 0;

ZipFile newZip = ZipFile.Read(ms);
Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
  • 1
    Thanks but I could not get the zip file to open up so that the user can view all the files that were zipped – Nate Pet Dec 17 '12 at 21:40
0

See the documentation for Create a zip using content obtained from a stream.

  using (ZipFile zip = new ZipFile())
  {
    ZipEntry e= zip.AddEntry("Content-From-Stream.bin", "basedirectory", StreamToRead);
    e.Comment = "The content for entry in the zip file was obtained from a stream";
    zip.AddFile("Readme.txt");
    zip.Save(zipFileToCreate);
  }

After saving it, you can then open it up as normal.

Bobson
  • 13,498
  • 5
  • 55
  • 80