download multiple Files from bytes as a zip-file in c# code example

Example: download multiple Files from bytes as a zip-file in c#

protected void btnDownloadAll_Click(object sender, EventArgs e)        {            // Here we will create zip file & download            string zipFileName = "MyZipFiles.zip";            Response.ContentType = "application/zip";            Response.AddHeader("content-disposition","fileName="+ zipFileName);            byte[] buffer = new byte[4096];            ZipOutputStream zipOutputStream = new ZipOutputStream(Response.OutputStream);            zipOutputStream.SetLevel(3);            try            {                DirectoryInfo DI = new DirectoryInfo(Server.MapPath("~/DownloadFolder"));                foreach (var i in DI.GetFiles())                {                    Stream fs = File.OpenRead(i.FullName);                    ZipEntry zipEntry = new ZipEntry(ZipEntry.CleanName(i.Name));                    zipEntry.Size = fs.Length;                    zipOutputStream.PutNextEntry(zipEntry);                    int count = fs.Read(buffer, 0, buffer.Length);                    while (count > 0)                    {                        zipOutputStream.Write(buffer, 0, count);                        count = fs.Read(buffer, 0, buffer.Length);                        if (!Response.IsClientConnected)                        {                            break;                        }                        Response.Flush();                    }                    fs.Close();                }                zipOutputStream.Close();                Response.Flush();                Response.End();            }            catch (Exception)            {                throw;            }                    }

Tags:

Misc Example