2012-12-17 34 views
0

我正在使用DotNetZip。如何從MemoryStream打開一個zip文件

我需要做的是打開一個zip文件與服務器的文件。 用戶可以抓住這些文件並將其存儲在本地機器上。

我以前做什麼是以下幾點:

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

注意,問:在服務器上的驅動器。使用Process.Start,它只需打開zip文件,以便用戶可以訪問所有文件。我喜歡做同樣的事情,但不會將文件存儲在磁盤上,而是從內存中顯示出來。現在

,而不是存儲在服務器上的zip文件,我喜歡用的MemoryStream

打開它

我有以下的,但似乎並沒有工作

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

,但不知道如何進一步繼續從內存流開放的zip文件的條款,以便用戶可以訪問所有文件

+0

見其他例子在這裏 - http://stackoverflow.com/questions/2324626/extract-a-zip-file-programmatically-by-dotnetzip-library – MethodMan

回答

1

下面是一段活動代碼(複製逐字),我寫下來將一系列博文作爲壓縮的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"); 
} 
0

您可以使用:

zip.Save(ms); 

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

ZipFile newZip = ZipFile.Read(ms); 
+1

謝謝,但我無法得到zip文件打開以便用戶可以查看所有壓縮的文件 –

0

請參閱the documentation for 使用從流中獲取的內容創建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); 
    } 

保存後,您可以正常打開它。

+0

您好Bobson,當我按照您所提到的,它無法弄清楚StreamToRead是什麼。 –

+0

@WebDev - 就你的情況而言,你會把'ms'放在那裏,因爲那是你的'MemoryStream'。 – Bobson

+0

不知道這樣做是否符合我的預期。我可能沒有解釋清楚。 –