C# 如何从MemoryStream打开zip文件

C# 如何从MemoryStream打开zip文件,c#,dotnetzip,C#,Dotnetzip,我正在使用DotNetZip 我需要做的是打开一个包含来自服务器的文件的zip文件。 然后,用户可以抓取文件并将其存储在本地计算机上 我以前做的是: string path = "Q:\\ZipFiles\\zip" + npnum + ".zip"; zip.Save(path); Process.Start(path); 请注意,Q:是服务器上的一个驱动器。使用Process.Start,只需打开zip文件,用户就可以访问所有文件。我也喜欢这样做,但不是

我正在使用DotNetZip

我需要做的是打开一个包含来自服务器的文件的zip文件。 然后,用户可以抓取文件并将其存储在本地计算机上

我以前做的是:

      string path = "Q:\\ZipFiles\\zip" + npnum + ".zip";
      zip.Save(path);
      Process.Start(path);
请注意,Q:是服务器上的一个驱动器。使用Process.Start,只需打开zip文件,用户就可以访问所有文件。我也喜欢这样做,但不是将文件存储在磁盘上,而是从内存中显示它

现在,我喜欢用MemoryStream打开zip文件,而不是将其存储在服务器上

我有以下几点,但似乎不起作用

      var ms = new MemoryStream();
      zip.Save(ms);
但不确定如何进一步从内存流中打开zip文件,以便用户可以访问您可以使用的所有文件:

zip.Save(ms);

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

ZipFile newZip = ZipFile.Read(ms);
有关使用从流中获取的内容创建zip的信息,请参阅。

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

保存后,您可以正常打开它。

这是一段实时代码(逐字复制),我编写该代码是为了将一系列博客文章作为压缩的csv文件下载。它是活的,它是工作的

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

请参阅此处的其他示例-谢谢,但我无法打开zip文件,以便用户可以查看zippedHi Bobson的所有文件。当我执行您提到的操作时,它无法确定StreamToRead是什么。@WebDev-在您的示例中,您将
ms
放在其中,因为那是你的
内存流
。我不确定这是否符合我的预期。我可能解释得不好。