1

我需要在保存文件之前检查文件是否被压缩。我创建了以下扩展方法来检查文件是否使用ZipFile(来自SharpLibZip库)压缩:

public static bool IsCompressed(this HttpPostedFile postedFile)
{
    Stream stream = postedFile.InputStream;

    using (var zipFile = new ZipFile(stream))
    {
        return zipFile.TestArchive(true);
    }
}

问题是当我调用HttpPostedFile.SaveAs()它时将文件保存为 0kb:

if(uploadedFile.IsCompressed())
{
    uploadedFile.InputStream.Seek(0, SeekOrigin.Begin);
    uploadedFile.SaveAs(physicalZipFile.FullName);
}

这与在IsCompressed(). 我曾尝试在保存之前从头开始,但问题仍然存在。

有人可以在这里解释这个问题吗?

4

1 回答 1

1

对于偶然发现这一点的任何人......这是我的解决方案:

使用 ILSpy 我反编译了程序集,发现它ZipFile.Dispose()实际上关闭了流。还有其他清理代码,所以我不建议不要处理该ZipFile实例。

另一方面,ZipArchive( System.IO.Compression) 实际上具有以下重载构造函数:

public ZipArchive(Stream stream, ZipArchiveMode mode, bool leaveOpen)

设置leaveOpentrue将在正确处理实例后使流保持打开状态。

于 2013-05-17T13:25:05.220 回答