Expert advice needed!

  • Thread starter Thread starter Smith
  • Start date Start date
S

Smith

Hello,
My code below create a .CSV file and compress it with the new GZipStream
class.

My problem, is how to set the extension of the uncompressed file to .csv.
Users have to manually do this and it is not quite convenient. Any help will
be highly appreciated.

File: handler.ashx:
public void ProcessRequest(HttpContext context)

{



HttpResponse Response = context.Response;

HttpRequest Request = context.Request;


Response.Clear();

Response.Buffer = true;


//Using a new compression class of the framework

GZipStream gzStream = new GZipStream(Response.OutputStream,
CompressionMode.Compress);

StreamWriter sw = new StreamWriter(gzStream);

DataTable dt = Session["MyDataTable"];

if (dt != null)

{

int iColCount = dt.Columns.Count;

for (int i = 0; i < iColCount; i++)

{

sw.Write(dt.Columns);

if (i < iColCount - 1)

{

sw.Write(",");

}

}

sw.Write(sw.NewLine);

// Now write all the rows.

foreach (DataRow dr in dt.Rows)

{

for (int i = 0; i < iColCount; i++)

{

if (!Convert.IsDBNull(dr))

{

sw.Write(dr.ToString().Trim());

}

if (i < iColCount - 1)

{

sw.Write(",");

}

}

sw.Write(sw.NewLine);

}

sw.Close();

Response.ContentType = "application/x-Gzip";

Response.AddHeader("content-disposition", "attachment; filename=" +
"compressed_file.gz");


Response.End();

}

}



S
 
The more i got into this, the more i see that it does not solve my problem
anyway. This solution seems only to apply to file system files. I do not
have a file on disk. I have my data in memory in a data table.

I use the Chilkat Zip component for this type of thing.

It has a method specifically designed to solve your particular problem:
http://www.chilkatsoft.com/refdoc/csZipRef.html#method033

Incidentally, it also has full GZip support, though you don't need that in
this instance...
 
Back
Top