我一直在尝试让.NET 4.5中包含的“新”ZipArchive(System.IO.Compression.ZipArchive
)在ASP.NET网站上运行。但似乎它不喜欢写入HttpContext.Response.OutputStream
的流。
我的下面示例代码将抛出
System.NotSupportedException: 指定的方法不受支持
只要尝试在流上进行写操作,就会出现上述异常。
该流的CanWrite
属性返回true。
如果我使用指向本地目录的文件流替换OutputStream,则可以正常工作。这是为什么呢?
ZipArchive archive = new ZipArchive(HttpContext.Response.OutputStream, ZipArchiveMode.Create, false);
ZipArchiveEntry entry = archive.CreateEntry("filename");
using (StreamWriter writer = new StreamWriter(entry.Open()))
{
writer.WriteLine("Information about this package.");
writer.WriteLine("========================");
}
堆栈跟踪:
[NotSupportedException: Specified method is not supported.]
System.Web.HttpResponseStream.get_Position() +29
System.IO.Compression.ZipArchiveEntry.WriteLocalFileHeader(Boolean isEmptyFile) +389
System.IO.Compression.DirectToArchiveWriterStream.Write(Byte[] buffer, Int32 offset, Int32 count) +94
System.IO.Compression.WrappedStream.Write(Byte[] buffer, Int32 offset, Int32 count) +41