私はデスクトップ WPF アプリケーション (.Net Framework 4.5) を作成しています。タスクの 1 つは、複数のファイルを zip アーカイブに保存することです。私は2つの方法を作りました。最初に zip を作成し、次にそれから読み取ります。
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 がそれを開くことができるということですが、2 番目の方法を使用してコンテンツを読み取ると、取得し続けます。
End Of Central Directory で予想されるエントリ数が、Central Directory のエントリ数と一致しません。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:z:\Home Inc\Microsoft.MCS.SPPal\Microsoft.MCS の Microsoft.MCS.SPPal.MainWindow..ctor() の行 32。 SPPal\MainWindow.xaml.cs:48行目
実験の一環として、far manager で新しいアーカイブを作成し、それを GetFileContent メソッドで開いたところ、魅力的に機能しました。したがって、エラーは SetArchive メソッドにあるはずだと思います。
どんな助けでも素晴らしいでしょう、それは午前3時で、私はかなり立ち往生しています。
PS: 私はコード設計がひどいことを知っています。それは何十回も書き直されました。