1

I read ZipInputStream from a stream. There are 10 ZipEntries, but size of all of them is -1! I can't figure out why, because there are data, so it must be > 0. Here's my code:

var zipInputStream = new ZipInputStream(new MemoryStream(reports));
ZipEntry zipEntry;
while ((zipEntry = zipInputStream.GetNextEntry()) != null)
{
    var fileName = Path.GetFileName(zipEntry.Name);
    if (String.IsNullOrEmpty(fileName)) continue;

    var identifier = fileName.Split('.')[1];
    var buffer = new byte[zipEntry.Size];
    zipInputStream.Read(buffer, 0, buffer.Length);
    var report = encoding.GetString(buffer);
            ...
}

And on the line var buffer = new byte[zipEntry.Size] I've got an OverflowException. When I check zipEntry.Size - it's always -1. If I write var buffer = new byte[4096] for example it's ok, but not correct. Any thoughts, please? Thanks in advance!

IDeveloper
  • 1,249
  • 2
  • 13
  • 22
  • Well, did you set the `.Size` when you *created* the zip archive? IIRC it is an optional field, related to whether or not it uses Zip64 internally (which is usually configurable) – Marc Gravell Oct 28 '13 at 11:03

1 Answers1

2

Here, 0 would indicate "no data"; -1 is indicating that it doesn't know the size of the data. Your best bet, then, is to read to the end of that entry. Perhaps:

MemoryStream ms = new MemoryStream();
while ((zipEntry = zipInputStream.GetNextEntry()) != null)
{
    var fileName = Path.GetFileName(zipEntry.Name);
    if (String.IsNullOrEmpty(fileName)) continue;

    var identifier = fileName.Split('.')[1];
    ms.SetLength(0); // reset between iterations, but let it re-use the memory
    zipInputStream.CopyTo(ms);
    var report = encoding.GetString(ms.GetBuffer(), 0, (int)ms.Length);
}
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900