2017-08-02 69 views
1

我有一個名爲abc.ZIP如何提取從離子

內ZIP文件夾結構一個ZIP文件內的ZIP C#具體路徑ZIP文件如下:

--abc 
---pqr 
----a 
----b 
----c 

我想在提取該ZIP D:/文件夾名稱

但我想只提取名爲a,b,c的文件夾及其內容。文件夾名稱也不固定。我不想提取根文件夾abc及其子文件夾pqr。

我用下面的代碼,但它不工作:

using (ZipFile zipFile = ZipFile.Read(@"temp.zip")) 
{ 
    foreach (ZipEntry entry in zipFile.Entries) 
    { 
    entry.Extract(@"D:/folder_name"); 
    } 
} 

回答

0

以下應該工作,但我不知道,如果它是最好的選擇。

string rootPath = "abc/pqr/"; 
using (ZipFile zipFile = ZipFile.Read(@"abc.zip")) 
{ 
    foreach (ZipEntry entry in zipFile.Entries) 
    { 
     if (entry.FileName.StartsWith(rootPath) && entry.FileName.Length > rootPath.Length) 
     { 
      string path = Path.Combine(@"D:/folder_name", entry.FileName.Substring(rootPath.Length)); 
      if (entry.IsDirectory) 
      { 
       Directory.CreateDirectory(path); 
      } 
      else 
      { 
       using (FileStream stream = new FileStream(path, FileMode.Create)) 
        entry.Extract(stream); 
      }     
     } 
    } 
} 

其他選項是將臨時目錄中的完整文件解壓縮並將子目錄移動到目標目錄。

+0

我正在做第二個選項(使用臨時目錄),但現在的問題是在部署服務器上我沒有訪問臨時。 –

+0

回答您建議不起作用。 :( –

+0

您可以在任何文件夾中創建臨時文件夾。如果您無權訪問臨時文件夾,請使用其他文件夾。 – Ben