Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to create ZipArchive correctly?
    text
    copied!<p>i'm writing Desktop WPF application (.Net Framework 4.5) and one of tasks is to save multiple files to zip archive. I made 2 methods. First to create zip, second to read from it.</p> <pre><code> 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&lt;String, String&gt; files) { using (var fileStream = new FileStream(Path.Combine(path, zipName), FileMode.OpenOrCreate)) { using (ZipArchive zip = new ZipArchive(fileStream, ZipArchiveMode.Create)) { foreach (KeyValuePair&lt;String, String&gt; 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); } } } } } </code></pre> <p>Thing is that zip archive created and far manager and WinRAR can open it, but when I use second method to read its content I keep getting</p> <blockquote> <p>Number of entries expected in End Of Central Directory does not correspond to number of entries in Central Directory. at System.IO.Compression.ZipArchive.ReadCentralDirectory() at System.IO.Compression.ZipArchive.get_Entries() at Microsoft.MCS.SPPal.Storage.StorageObject.GetFileContent(String zipPath, String entityName) in 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:line 48</p> </blockquote> <p>As a part of experiment i created new archive in far manager and opened it up with GetFileContent method, and it works like a charm. So i think error should be in SetArchive method.</p> <p>Any help would be awesome, it's 3 a.m. and i'm quite stuck.</p> <p>P.S: I know code design suck, it was rewritten dozens of times.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload