C# – How to generate and send a .zip file to a user in C# ASP.NET


I need to construct and send a zip to a user.

I've seen examples doing one or the other, but not both, and am curious if there are any 'best practices' or anything.

Sorry for the confusion. I'm going to generating the zip on the fly for the web user, and sending it to them in the HTTP response. Not in an email.


Best Solution

I would second the vote for SharpZipLib to create the Zip file. Then you'll want to append a response header to the output to force the download dialog.


should give you a good starting point to achieve that. You basically need to add a response header, set the content type and write the file to the output stream:

Response.AppendHeader( "content-disposition", "attachment; filename=" + name );
Response.ContentType = "application/zip";

That last line could be changed to a Response.Write(filecontents) if you don't want to save to a temp file.