Download SharpZipLib:
SharpZipLib is a free .NET API which is used to perform zipping operations. We will use SharpZipLib to perform the file zip.
Displaying the Files on the GridView Control:
We have created a folder in our application called "Files" which contains several text files. The first task is to display these files in the GridView control. The implementation is shown below:
private void BindData()
{
var files = Directory.GetFiles(Server.MapPath("~/Files"));
gvFiles.DataSource = from f in files
select new
{
FileName = Path.GetFileName(f),
FilePath = f
};
gvFiles.DataBind();
}
The GridView ASPX code is shown below:
<asp:GridView ID="gvFiles" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chkSelect" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="File Name">
<ItemTemplate>
<asp:Label ID="lblFileName" runat="server" Text='<%# Eval("FileName") %>' />
<asp:Label ID="lblFilePath" Visible="false" runat="server" Text='<%# Eval("FilePath") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
The first column contains CheckBoxes which will be used to select particular files to download. The second column displays the name of the file and also contains the file path as a hidden Label.
The output is shown below:

Next, we will add the ZipAllFiles custom method which will iterate through the selected files zip them as a single file and download it to the client's machine.
Zipping all Files and Downloading:
We have a folder called TempFiles in our server's folder where we will first create the temporary zip file. Later we will delete the zip file from the TempFolder. You can use any random name for the temporary file. We will be using GUID as the temporary file name. Let's see the complete implementation of the ZipAllFiles method.
private void ZipAllFiles()
{
byte[] buffer = new byte[4096];
// the path on the server where the temp file will be created!
var tempFileName = Server.MapPath(@"TempFiles/" + Guid.NewGuid().ToString() + ".zip");
var zipOutputStream = new ZipOutputStream(File.Create(tempFileName));
var filePath = String.Empty;
var fileName = String.Empty;
var readBytes = 0;
foreach(GridViewRow row in gvFiles.Rows)
{
var isChecked = (row.FindControl("chkSelect") as CheckBox).Checked;
if (!isChecked) continue;
filePath = (row.FindControl("lblFilePath") as Label).Text;
fileName = (row.FindControl("lblFileName") as Label).Text;
var zipEntry = new ZipEntry(fileName);
zipOutputStream.PutNextEntry(zipEntry);
using(var fs = File.OpenRead(filePath))
{
do
{
readBytes = fs.Read(buffer, 0, buffer.Length);
zipOutputStream.Write(buffer,0,readBytes);
} while (readBytes > 0);
}
}
if (zipOutputStream.Length == 0)
{
lblMessage.Text = "Please select at least one file!";
return;
}
zipOutputStream.Finish();
zipOutputStream.Close();
Response.ContentType = "application/x-zip-compressed";
Response.AppendHeader("Content-Disposition", "attachment; filename=YourFile.zip");
Response.WriteFile(tempFileName);
Response.Flush();
Response.Close();
// delete the temp file
if(File.Exists(tempFileName))
File.Delete(tempFileName);
}
The following line create a random unique file name:
var tempFileName = Server.MapPath(@"TempFiles/" + Guid.NewGuid().ToString() + ".zip");
Next, we iterate through the selected files and create a ZipEntry for each file. All ZipEnteries are placed in the ZipOutputStream by using the PutNextEntry method.
Finally, the ContentType for the response is set and header is adjusted to support the zip file format and the file is sent to the browser by using the Response.WriteFile method.
The screenshot below shows the client experiences a dialog box which allows them to open or save the zip file.

Conclusion:
In this article we demonstrated how to download multiple files as a single zip file.
[Download Sample]