簡體   English   中英

c#-來自MemoryStream的DotNetZip打開zip文件

[英]c# - DotNetZip open zip file from MemoryStream

我想做的是代替將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. 

嘗試這個:

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

如果這是本地的。 您將需要將流保存到文件中並調用Process.Start

如果在服務器上。 只需將您的ms寫入具有適當mime類型的Response中即可。

您必須將內存流的內容發送回作為響應:

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

嘗試像這樣創建一個ActionResult :我不確定行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");
      }
  }

這樣,我們可以將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();     

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM