簡體   English   中英

ZipStorer-MemoryStream中的Zip文本,通過httpResponse傳輸,無法作為Zip文件打開

[英]ZipStorer - Zip Text in MemoryStream, Transmit through httpResponse, unable open as Zip File

注意:我已經解決了問題。 請參閱以下答案。

我正在使用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();
    }
}

我已經解決了自己的問題。 以下是代碼:

string text = GetLongText();
byte[] ba = Encoding.UTF8.GetBytes(text);
using (MemoryStream ms = new MemoryStream())
{
    using (ZipStorer zip = ZipStorer.Create(ms, "My Zip"))
    {
        zip.AddStream(ZipStorer.Compression.Deflate, "text.txt", new MemoryStream(ba), DateTime.Now, "My Text");
    }
    Response.AppendHeader("content-disposition", "attachment; filename=MyZip.zip");
    Response.ContentType = "application/zip";
    Response.BinaryWrite(ms.ToArray());
    Response.End();
}
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM