小编Dan*_*sen的帖子

使用HttpContext OutputStream写入ZipArchive

我一直试图让.NET 4.5(System.IO.Compression.ZipArchive)中包含的"新"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("========================");
}
Run Code Online (Sandbox Code Playgroud)

堆栈跟踪:

[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
Run Code Online (Sandbox Code Playgroud)

c# outputstream httpcontext .net-4.5 asp.net-4.5

36
推荐指数
3
解决办法
1万
查看次数

标签 统计

.net-4.5 ×1

asp.net-4.5 ×1

c# ×1

httpcontext ×1

outputstream ×1