ZipArchive.Entries throws an exception if the zipped folder has nested folders

0

On my server, I expect to receive some zipped JSON documents via TCP. Here is the method that attempts to unzip the received folder:

public static void Unzip(this Stream zippedFolderStream)
{
    var zipArchive = new ZipArchive(zippedFolderStream);
    ZipArchiveEntry[] archiveEntries = zipArchive.Entries.ToArray();

    Console.WriteLine($"Received {archiveEntries.Length} documents.");

    foreach (ZipArchiveEntry entry in archiveEntries)
    {
        // Do stuff.
    }
}

If the zipped folder does not contain any nested folders, the code runs smoothly -- everything that is replaced by // Do stuff. in the example above is executed.

However, if the zipped folder contains nested folders (which are themselves not zipped), I encounter this error:

System.IO.InvalidDataException
  HResult=0x80131501
  Message=Number of entries expected in End Of Central Directory does not correspond to number of entries in Central Directory.
  Source=System.IO.Compression
  StackTrace:
   at System.IO.Compression.ZipArchive.ReadCentralDirectory()
   at System.IO.Compression.ZipArchive.get_Entries()
   at TcpServer.StreamExtensions.Unzip(Stream zippedFolderStream) in C:\Users\Me\source\repos\TcpServer\TcpServer\StreamExtensions.cs:line 16
   ... etc.

My question is: How can I traverse through all the JSON documents inside the zipped folder, even when they are stored within nested folders?

c#
.net
zip
system.io.compression
asked on Stack Overflow Apr 25, 2019 by MY_G • edited Jul 15, 2019 by Stephen Kennedy

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0