我正在寫桌面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:我知道代碼設計很爛,它被改寫了幾十次。
問題是如何正確創建一個新的ZipArchive。 ZipArchive!= SevenZipCompressor。 – Bardicer 2015-08-26 18:42:36
這並沒有幫助回答標題中所述的問題。 – Dave 2015-09-08 11:26:41
這是我的第一篇文章之一,我唯一的意圖是幫助其他解決方案,但很抱歉,如果我的解決方案不被任何人喜歡。 – Joseph 2015-09-09 17:46:20