6

私はデスクトップ 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: 私はコード設計がひどいことを知っています。それは何十回も書き直されました。

4

3 に答える 3

3

SetArchive() メソッドで範囲外になる前に、ZipArchive で Dispose() への明示的な呼び出しを追加することで、これを機能させることができました。

zip.Dispose();
于 2013-07-23T00:12:18.493 に答える
1

Deflate64 (Windows シェルまたは 7-zip で作成) を使用して圧縮された、圧縮前に 4 GB を超えるファイルを含む zip ファイルを開こうとすると、

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

System.IO.Compression.ZipFileWindows 2012 R2 で解凍した場合。これらのファイルは、Windows Shell、7-zip、および Info-zip で解凍できるため、正当な zip であると思われました。エラーなしで解凍された唯一の大きなファイル zip アーカイブはSystem.IO.Compression.ZipFile、Info-zip の Zip64 で作成されたものでした。

于 2014-06-10T16:22:50.743 に答える