Question

Note: I have solved the problem myself. See the below answer.

I'm using ZipStorer to zip files in ASP.NET C# 4.0 WebForm.

After I created the Zip in MemoryStream and transmitted it using httpResponse, the client user was unable to open the file as a Zip File.

Any tips? Thanks.

Below is my code:

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();
    }
}
Was it helpful?

Solution

I have solve the problem myself. Below is the codes:

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();
}
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top