繁体   English   中英

如何从MemoryStream打开一个zip文件

[英]How to open up a zip file from MemoryStream

我正在使用DotNetZip。

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

我之前所做的是:

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

请注意,Q:是服务器上的驱动器。 使用Process.Start,它只需打开zip文件,以便用户可以访问所有文件。 我喜欢这样做,但不将文件存储在磁盘上,而是从内存中显示出来。

现在,我不希望将zip文件存储在服务器上,而是使用MemoryStream打开它

我有以下内容,但似乎无法正常工作

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

但不确定如何从内存流中打开zip文件,以便用户可以访问所有文件

这是一段实时代码(逐字复制),我编写了这些代码,以压缩的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.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);
  }

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM