1

我从流中读取 ZipInputStream。有 10 个 ZipEntries,但它们的大小都是 -1!我不知道为什么,因为有数据,所以它必须> 0。这是我的代码:

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]它没关系,但不正确。请问有什么想法吗?提前致谢!

4

1 回答 1

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);
}
于 2013-10-28T11:06:28.237 回答