3

我喜欢做的不是将 zip 文件存储在磁盘上,而是喜欢从 MemoryStream 中打开它。

我正在查看 DotNetZip 编程示例的文档:请注意,我根据我认为可能需要的内容对其进行了微调。

    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. 
4

5 回答 5

5

试试这个:

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");
}
于 2012-12-18T15:40:11.733 回答
1

如果这是本地的。您需要将流保存到文件中并调用Process.Start它。

如果这是在服务器上。只需使用适当的 mime 类型将您的 ms 写入 Response 即可。

于 2012-12-18T15:22:22.070 回答
1

您必须将内存流的内容作为响应发回:

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);
}
于 2012-12-18T15:26:03.727 回答
1

尝试创建ActionResult一个有点像这样:我不是 100% 确定这条线var fileData = ms;,而且我现在无法访问开发环境,但应该有足够的空间让你解决它。

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");
      }
  }
于 2012-12-18T15:30:12.220 回答
1

这样我们就可以将 zip 写入输出流。可能有帮助

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();     
于 2015-04-22T11:19:20.063 回答