2012-05-20 25 views
10

當在Windows中解壓縮文件,我會偶爾有路徑的問題如何處理與過長的路徑解壓的ZipFile /複製

  1. 是爲Windows在原有OS太長(但還好的是創建該文件)。
  2. 被「複製」,由於不區分大小寫

使用DotNetZip,該ZipFile.Read(path)呼叫廢話了,只要用閱讀這些問題的一個zip文件。這意味着我甚至無法嘗試過濾出來。

using (ZipFile zip = ZipFile.Read(path)) 
{ 
    ... 
} 

什麼是處理讀取這些文件進行排序的最佳方式?

更新:從這裏

例ZIP: https://github.com/MonoReports/MonoReports/zipball/master

重複: https://github.com/MonoReports/MonoReports/tree/master/src/MonoReports.Model/DataSourceType.cs https://github.com/MonoReports/MonoReports/tree/master/src/MonoReports.Model/ DatasourceType.cs

這裏是例外的更多詳細信息:

Ionic.Zip.ZipException:無法讀取,作爲一個ZipFile的
---> System.ArgumentException:一個>具有相同的鍵項已被添加。
在System.ThrowHelper.ThrowArgumentException(ExceptionResource資源)
在System.Collections.Generic.Dictionary 2.Insert(TKey key, TValue value, Boolean add)
at System.Collections.Generic.Dictionary
2.添加(TKEY的鍵,TValue值)
在Ionic.Zip.ZipFile.ReadCentralDirectory(ZipFile的ZF)
在Ionic.Zip.ZipFile.ReadIntoInstance(ZipFile的ZF)

分辨率:

基於@ Cheeso的建議,我可以從流中讀取的一切,那些避免重複,路徑問題:

//using (ZipFile zip = ZipFile.Read(path)) 
using (ZipInputStream stream = new ZipInputStream(path)) 
{ 
    ZipEntry e; 
    while((e = stream.GetNextEntry()) != null) 
    //foreach(ZipEntry e in zip) 
    { 
     if (e.FileName.ToLower().EndsWith(".cs") || 
      e.FileName.ToLower().EndsWith(".xaml")) 
     { 
      //var ms = new MemoryStream(); 
      //e.Extract(ms); 
      var sr = new StreamReader(stream); 
      { 
       //ms.Position = 0; 
       CodeFiles.Add(new CodeFile() { Content = sr.ReadToEnd(), FileName = e.FileName }); 
      } 
     } 
    } 
} 
+0

是文件.zip還是.gz? – SimpleVar

+0

.zip文件(特別是從GitHub的壓縮文件下載) – gameweld

+0

可以告訴你這個錯誤嗎?它是文件內的路徑名嗎?目標文件位置是否太長? – yamen

回答

3

用ZipInputStream閱讀。

zip文件級保留使用文件名作爲指標的集合。重複的文件名會打破該模式。

但是你可以使用ZipInputStream您的zip文件閱讀。這種情況下沒有收集或索引。

8

對於PathTooLongException問題,我發現,你不能使用DotNetZip。相反,我所做的是調用command-line version of 7-zip;那創造奇蹟。

public static void Extract(string zipPath, string extractPath) 
{ 
    try 
    { 
     ProcessStartInfo processStartInfo = new ProcessStartInfo 
     { 
      WindowStyle = ProcessWindowStyle.Hidden, 
      FileName = Path.GetFullPath(@"7za.exe"), 
      Arguments = "x \"" + zipPath + "\" -o\"" + extractPath + "\"" 
     }; 
     Process process = Process.Start(processStartInfo); 
     process.WaitForExit(); 
     if (process.ExitCode != 0) 
     { 
      Console.WriteLine("Error extracting {0}.", extractPath); 
     } 
    } 
    catch (Exception e) 
    { 
     Console.WriteLine("Error extracting {0}: {1}", extractPath, e.Message); 
     throw; 
    } 
} 
+2

這實際上是我找到的最簡單的解決方案。在此評論時,知道7za.exe在描述中帶有「7-zip extra:....」的7-zip下載可能會有幫助。 (http://www.7-zip.org/download.html) –

+0

「參數」行不正確;它應該是'Arguments =「x \」「+ zipPath +」\「-o \」「+ extractPath +」\「」'(注意** o **開關和'extractPath'之間沒有空格 – sigil

+0

Thanks @根據[this](http://superuser.com/questions/95902/7-zip-and-unzipping-from-command-line)你是對的,我更新了我的答案。 –