2013-04-25 99 views
4

我正在寫桌面WPF應用程序(.Net Framework 4.5),其中一個任務是將多個文件保存到zip壓縮文件中。我做了2種方法。首先創建zip,然後從中讀取。如何正確創建ZipArchive?

public static String GetFileContent(String zipPath, String entityName) 
    { 
     String retVal = String.Empty; 

     using (ZipArchive zipfile = ZipFile.OpenRead(zipPath)) 
     {      
      foreach (ZipArchiveEntry entry in zipfile.Entries) 
      { 
       if (entry.Name.ToLower() == entityName) 
       { 
        using (StreamReader s = new StreamReader(entry.Open())) 
        { 
         retVal = s.ReadToEnd(); 
         break; 
        } 
       } 
      } 
     } 

     return retVal; 
    } 

    public static void SetArchive(String path, String zipName, Dictionary<String, String> files) 
    { 
     using (var fileStream = new FileStream(Path.Combine(path, zipName), FileMode.OpenOrCreate)) 
     { 
      using (ZipArchive zip = new ZipArchive(fileStream, ZipArchiveMode.Create)) 
      {      
       foreach (KeyValuePair<String, String> file in files) 
       { 
        var entry = zip.CreateEntry(file.Key, CompressionLevel.Optimal); 
        using (Stream s = entry.Open()) 
        {   
         byte[] data = Encoding.UTF8.GetBytes(file.Value); 
         s.Write(data, 0, data.Length); 
        } 
       }      
      }    
     } 
    } 

的是ZIP壓縮文件創建和遠經理和WinRAR的可以打開它,但是當我使用第二種方法來讀取其內容我不斷收到

預計在結束中央目錄條目數與中央目錄中的條目數量不一致。 在System.IO.Compression.ZipArchive.ReadCentralDirectory() 在System.IO.Compression.ZipArchive.get_Entries() 在Microsoft.MCS.SPPal.Storage.StorageObject.GetFileContent(字符串zipPath,字符串的entityName)中的Z:\ Home Inc \ Microsoft.MCS.SPPal \ Microsoft.MCS.SPPal \ Storage \ StorageObject.cs:line 32 at Microsoft.MCS.SPPal.MainWindow..ctor()in z:\ Home Inc \ Microsoft.MCS.SPPal \ Microsoft.MCS.SPPal \ MainWindow.xaml.cs:行48

作爲試驗的一部分,我在遙遠管理器創建新的存檔,並與GetFileContent方法打開它,它就像一個魅力。所以我認爲錯誤應該在SetArchive方法中。

任何幫助都會很棒,上午3點,我很困難。

P.S:我知道代碼設計很爛,它被改寫了幾十次。

回答

-4

爲了更好的壓縮,你可以使用7zip的庫。通過這種方式:

public void AddToArchive(string fileToBeZipped, string zipDestination) 
{ 
    DirectoryInfo Di = new DirectoryInfo(zipDestination); 
    StringBuilder sb_archiveFile = new StringBuilder(zipDestination + Path.DirectorySeparatorChar + Di.Name + @".7z"); 
    string archiveFile = sb_archiveFile.ToString(); 


    SevenZip.SevenZipCompressor compressor = new SevenZipCompressor(); 

    Console.WriteLine("zip destination : " + Di.Name); 
    if (!File.Exists(fileToBeZipped)) 
    { 
     Console.WriteLine("Appending {0} to Archive ", fileToBeZipped); 
     compressor.CompressionMode = SevenZip.CompressionMode.Append; 
    } 
    else 
    { 
     Console.WriteLine("Creating {0} at Destination {1}....", fileToBeZipped, archiveFile); 
     Console.WriteLine("CREATING:: "); 
     compressor.CompressionMode = SevenZip.CompressionMode.Create; 
    } 
    compressor.CompressionLevel = CompressionLevel.Normal; 
    compressor.CompressionMethod = CompressionMethod.Lzma; 
    compressor.CompressionMode = CompressionMode.Append; 

    compressor.CompressDirectory(zipDestination, archiveFile); 



    compressor.CompressStream(streamer, streamer2); 
} 

,並調用方法有:AddToArchive(inFolder,splitIntoDir);

您可以下載7zip here的源代碼。

您可以爲Visual Studio安裝7zip here的Nuget包。

+0

問題是如何正確創建一個新的ZipArchive。 ZipArchive!= SevenZipCompressor。 – Bardicer 2015-08-26 18:42:36

+0

這並沒有幫助回答標題中所述的問題。 – Dave 2015-09-08 11:26:41

+0

這是我的第一篇文章之一,我唯一的意圖是幫助其他解決方案,但很抱歉,如果我的解決方案不被任何人喜歡。 – Joseph 2015-09-09 17:46:20

3

在SetArchive()方法超出範圍之前,我可以通過在ZipArchive上添加對Dispose()的顯式調用來獲得此功能。

zip.Dispose(); 
+1

你的意思是使用(ZipArchive zip = new ZipArchive(...))'沒有完成它的工作嗎? 「使用」塊專門用於一次性物品...... – 2013-07-23 00:17:52

+0

好點。我沒有使用「使用」。切換到「使用」也很好。 – 2013-07-23 18:43:57

+0

OP的代碼中包含所有一次性使用的''使用'塊,隨時編輯您的文章並添加完整的工作代碼:) – 2013-07-23 18:48:00

0

當試圖打開包含均大於4 GB的壓縮前的已被使用的Deflate64壓縮(通過Windows外殼或7-ZIP創建)與System.IO.Compression.ZipFile上解壓縮,當我

Number of entries expected in End Of Central Directory does not correspond to number of entries in Central Directory 

文件的zip文件Windows 2012 R2。這些文件似乎是合法的zip文件,因爲Windows Shell,7-zip和Info-zip可以解壓縮它們。唯一的大文件zip壓縮文件解壓縮與System.IO.Compression.ZipFile沒有錯誤是用Info-zip的Zip64創建的。