How to create ZipArchive?

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.

+4
source share
3 answers

For better compression, you can use the 7zip library. In this way:

 public void AddToArchive(string fileToBeZipped, string zipDestination) { DirectoryInfo Di = new DirectoryInfo(zipDestination); StringBuilder sb_archiveFile = new StringBuilder(zipDestination + Path.DirectorySeparatorChar + Di.Name + @".7z"); string archiveFile = sb_archiveFile.ToString(); SevenZip.SevenZipCompressor compressor = new SevenZipCompressor(); Console.WriteLine("zip destination : " + Di.Name); if (!File.Exists(fileToBeZipped)) { Console.WriteLine("Appending {0} to Archive ", fileToBeZipped); compressor.CompressionMode = SevenZip.CompressionMode.Append; } else { Console.WriteLine("Creating {0} at Destination {1}....", fileToBeZipped, archiveFile); Console.WriteLine("CREATING:: "); compressor.CompressionMode = SevenZip.CompressionMode.Create; } compressor.CompressionLevel = CompressionLevel.Normal; compressor.CompressionMethod = CompressionMethod.Lzma; compressor.CompressionMode = CompressionMode.Append; compressor.CompressDirectory(zipDestination, archiveFile); compressor.CompressStream(streamer, streamer2); } 

And call the method with: AddToArchive (inFolder, splitIntoDir);

You can download the 7zip source code here .

You can install the Nuget package for 7zip here for Visual Studio.

-4
source

I managed to get this to work by adding an explicit call to Dispose () to ZipArchive before it went out of scope in the SetArchive () method.

 zip.Dispose(); 
+3
source

When I tried to open zip files containing files larger than 4 GB before compression compressed using Deflate64 (created using the Windows shell or 7-zip), I got

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

when unpacking System.IO.Compression.ZipFile on Windows 2012 R2. These files seemed like legit fasteners because Windows Shell, 7-zip and Info-zip could unzip them. The only large zip file archives that were unpacked without errors using System.IO.Compression.ZipFile were those created using Info-zip Zip64.

0
source

All Articles