How to zip a dynamically created file

Apr 16, 2009 at 8:17 PM
Hi All

Please assist a newbie!!

I have a web app that generates a .csv file dynamically and on the fly and sends it to the user to either open or save.

I would like to use the DotNetZip library to zip the file before sending it downstreem to the client.
My current code is as follows:
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DashBoardTableAdapters;
using Ionic.Zip;

public partial class Sample1 : System.Web.UI.Page
{
 int clientid = 0;
 string usergroup = "false";
 protected void Page_Load(object sender, EventArgs e)
 {
  if (this.Request["c"] != null)
  {
   if (this.Request["c"] != "")
   {
    clientid = Convert.ToInt32(this.Request["c"].ToString());
   }
  }
if (this.Request["ug"] != null)
  {
   if (this.Request["ug"] != "")
   {
    usergroup = this.Request["ug"].ToString();
   }
  }
  if (usergroup == "false")
  {
   btnExport.Text = "Export Master Data to MasterData_" + clientid + ".csv";
  }
  else
  {
   btnExport.Text = "Export Master Data to MasterData_" + clientid + "_" + usergroup + ".csv";
  }
 }
 protected void btnExport_Click(object sender, EventArgs e)
 {
  if (usergroup == "false")
  {
   ExportCSV(CreateData(), "MasterData_" + clientid);
  }
  else
  {
   ExportCSV(CreateData(), "MasterData_" + clientid + "_" + usergroup);
  }
 }
 public void ExportCSV(DataTable data, string fileName)
 {
  HttpContext context = HttpContext.Current;
  context.Response.Clear();
  context.Response.ContentType = "text/csv";
  context.Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName + ".csv");
  //write column header names
  for (int i = 0; i < data.Columns.Count - 1; i++)
  {
   if (i > 0)
   {
    context.Response.Write(",");
   }
   context.Response.Write(data.Columns[i].ColumnName);
  }
  context.Response.Write(Environment.NewLine);
  //Write data
  foreach (DataRow row in data.Rows)
  {
   for (int i = 0; i < data.Columns.Count - 1; i++)
   {
    if (i > 0)
    {
     context.Response.Write(",");
    }
    context.Response.Write(row.ItemArray[i].ToString());
   }
   context.Response.Write(Environment.NewLine);
  }
  context.Response.End();
 }
 private DataTable CreateData()
 {
  MasterGridTableAdapter ta = new MasterGridTableAdapter();
  DataTable table1 = ta.GetMasterGrid(clientid, usergroup);
  return table1;
 }
}

Where and how would I add the code to zip before sending to the client. Please help in as much detail as possible as I am new to dotnet.

Kind Regards.

Coordinator
Apr 19, 2009 at 3:52 PM
Here's an example of an ASP.NET page that zips up a file dynamically and downloads it to the browser: 
http://code.msdn.microsoft.com/DotNetZip/Wiki/View.aspx?title=ASP.NET%20Example