I am writing a WPF desktop application (.Net Framework 4.5), and one of the tasks is to save several files in a zip archive. I made 2 methods. First to create a zip, the second to read from it.
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); } } } } }
The fact is that the created zip archive and the remote manager and WinRAR can open it, but when I use the second method to read its contents, I continue to receive
The number of expected entries in the End of Central Directory does not match the number of entries in the Central Directory. in System.IO.Compression.ZipArchive.ReadCentralDirectory () in System.IO.Compression.ZipArchive.get_Entries () in 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 in Microsoft.MCS.SPPal.MainWindow..ctor () in z: \ Home Inc \ Microsoft.MCS.SPPal \ Microsoft.MCS. SPPal \ MainWindow.xaml.cs: line 48
As part of the experiment, I created a new archive in the remote manager and opened it using the GetFileContent method, and it works like a charm. Therefore, I think the error should be in the SetArchive method.
Any help would be awesome, it's 3am, and I'm completely stuck.
PS: I know that code design is sucked, it has been rewritten dozens of times.