Question

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!

Was it helpful?

Solution

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);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top