我在 ASP.NET 中有一个应用程序,用户可以在其中上传 ZIP 文件。我正在尝试使用 ICSharpZipLib 提取文件(我也尝试过 DotNetZip,但遇到了同样的问题)。
此 zip 文件包含单个 xml 文档(压缩前 9KB)。
当我使用桌面上的其他应用程序(7zip、Windows 资源管理器)打开此文件时,它似乎没问题。我的 unzip 方法抛出 System.OutOfMemoryException,我不知道为什么会这样。当我调试我的解压缩方法时,我注意到 zipInputStreams 的 Length 属性抛出异常并且不可用:
Stream UnZipSingleFile(Stream memoryStream)
{
var zipInputStream = new ZipInputStream(memoryStream);
memoryStream.Position = 0;
zipInputStream.GetNextEntry();
MemoryStream unzippedStream = new MemoryStream();
int len;
byte[] buf = new byte[4096];
while ((len = zipInputStream.Read(buf, 0, buf.Length)) > 0)
{
unzippedStream.Write(buf, 0, len);
}
unzippedStream.Position = 0;
memoryStream.Position = 0;
return unzippedStream;
}
以下是我获取 unzippedStream 字符串的方法:
string GetString()
{
var reader = new StreamReader(unzippedStream);
var result = reader.ReadToEnd();
unzippedStream.Position = 0;
return result;
}