为什么ZipEntry.Size始终为-1

时间:2013-10-28 10:59:17

标签: c# sharpziplib

我从流中读取ZipInputStream。有10个ZipEntries,但所有的大小都是-1!我无法弄清楚为什么,因为有数据,所以它必须是>这是我的代码:

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);
            ...
}

var buffer = new byte[zipEntry.Size]行上我有一个OverflowException。当我检查zipEntry.Size时 - 它总是-1。如果我写var buffer = new byte[4096]例如它没关系,但不正确。请问有什么想法吗?提前谢谢!

1 个答案:

答案 0 :(得分:2)

这里,0表示“没有数据”; -1表示不知道数据的大小。那么,您最好的选择是阅读该条目的结尾。也许:

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);
}