簡體   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