New to Telerik UI for ASP.NET AJAXStart a free 30-day trial

Download Multiple Files in a Single Archive

Environment

ProductTelerik WebForms ZipLibrary for ASP.NET AJAX

Description

This sample demonstrates how to download multiple files from various formats within a single .zip archive by making avail of the ZipLibrary control. In order to use this zip library, you can include the required assemblies as explained in the Included Assemblies article

You can find the original more advanced version of this implementation in the RadZipLibrary demo

Download Multiple Files in a Single Archive

Solution

Suppose you have several files in a folder, each of different type (.png, .gif, .pdf, etc.). By clicking on the Download All Files button, we'll create a ZipArchiveEntry, which will be responsible for downloading all the files.

ASP.NET
<telerik:RadButton ID="RadButton1" runat="server"
    Text="Download All Files" OnClick="RadButton1_Click">
    <Icon PrimaryIconCssClass="rbDownload" />
</telerik:RadButton>
C#
protected void RadButton1_Click(object sender, EventArgs e)
{
    MemoryStream memStream = new MemoryStream();
    using (ZipArchive archive = new ZipArchive(memStream, ZipArchiveMode.Create, true, null))
    {
        string path = HttpContext.Current.Server.MapPath("~/Files");

        foreach (string docFile in Directory.GetFiles(path))
        {
            string fileName = Path.GetFileName(docFile);
            ZipArchiveEntry entry = archive.CreateEntry(fileName);

            using (BinaryWriter writer = new BinaryWriter(entry.Open()))
            {
                writer.Write(File.ReadAllBytes(docFile));
            }
        }
    }

    SendZipToClient(memStream);
}

private void SendZipToClient(MemoryStream memStream)
{
    memStream.Seek(0, SeekOrigin.Begin);

    if (memStream != null && memStream.Length > 0)
    {
        Response.Clear();
        Response.AddHeader("content-disposition", "attachment; filename=files.zip");
        Response.ContentType = "application/zip";
        Response.BinaryWrite(memStream.ToArray());
        Response.End();
    }
}
In this article
EnvironmentDescriptionSolution
Not finding the help you need?
Contact Support