注意:我自己解决了这个问题。请参阅下面的答案。
我正在使用ZipStorer压缩 ASP.NET C# 4.0 WebForm 中的文件。
在我在 MemoryStream 中创建 Zip 并使用 httpResponse 传输它后,客户端用户无法将文件作为 Zip 文件打开。
有小费吗?谢谢。
下面是我的代码:
string text = GetLongText();
byte[] ba = Encoding.UTF8.GetBytes(text);
using (MemoryStream ms = new MemoryStream())
{
using (ZipStorer zip = ZipStorer.Create(ms, "My Zip File"))
{
zip.AddStream(ZipStorer.Compression.Deflate, "MyText.txt", new MemoryStream(ba), DateTime.Now, "My Text");
Response.Clear();
Response.AppendHeader("content-disposition", "attachment; filename=MyZip.zip");
Response.ContentType = "application/zip";
ms.WriteTo(Response.OutputStream);
Response.End();
}
}