我一直試圖讓「新」 ZipArchive包含在.NET 4.5(System.IO.Compression.ZipArchive
)在ASP.NET網站的工作。但它似乎不喜歡寫入HttpContext.Response.OutputStream
流。寫ZipArchive使用的HttpContext的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
你在開發環境還是服務器上嘗試它 – Alok
我目前在本地運行它,所以開發環境。我從IHttpHandler獲取HttpContext。 –
好的,MVC或Web表單? – Alok