tags:

views:

1100

answers:

10

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.

Mark

+1  A: 

I'm sure others will recommend SharpZipLib

How do you intend to "send" it. .NET has built in Libraries for email via SMTP

EDIT

In that case you'll want to capture the output stream from SharpZipLib and write it directly to the Response. Just make sure you have the correct Mimetype set in the Response Headers (application/zip) and make sure you don't Response.Write anything else to the user.

Eoin Campbell
It will be sent to the ASP.NET user over HTTP while on the page.
MStodd
I'm not an ASP developer, but would Server.Redirect("UrlToFile") accomplish that?
Rich.Carpenter
I don't want the file to be around after it's been sent to the user.
MStodd
edited the answer
Eoin Campbell
A: 

Sending a zip file (I'm assuming over email) is no different than attaching any file to an email to send it. How do the examples you've seen do it?

Ian Jacobs
A: 

As I understand it, Windows does not expose the objects necessary to "send to a compressed folder". I believe this was a stipulation in their licensing agreement with the company who wrote it. If you have WinZip installed, you can utilize it from the command line to create .zip files.

As far as sending it goes, I assume you mean via email, in which case normal SMTP functionality in Visual Studio should suffice.

Rich.Carpenter
+6  A: 

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.

http://aspalliance.com/259

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";
Response.WriteFile(pathToFile);

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

Adam Pope
A: 

Agree with above, SharpZipLib , for creating .zip files in .NET, it seems a very popular option.

As for 'send'ing, if you mean via SMTP/Email, you will need to use the System.Net.Mail name space. The System.Net.Mail.Attachment Class documentation has an example of how to send a file via email

Scratch the above, by the time I posted this I see you meant return via HTTP Response.

Clinton
+3  A: 

DotNetZip lets you do this easily, without ever writing to a disk file on the server. You can write a zip archive directly out to the Response stream, which will cause the download dialog to pop on the browser.

Example ASP.NET code for DotNetZip

More example ASP.NET code for DotNetZip

snip:

    Response.Clear();
    Response.BufferOutput = false; // false = stream immediately
    System.Web.HttpContext c= System.Web.HttpContext.Current;
    String ReadmeText= String.Format("README.TXT\n\nHello!\n\n" + 
                                     "This is text for a readme.");
    string archiveName= String.Format("archive-{0}.zip", 
                                      DateTime.Now.ToString("yyyy-MMM-dd-HHmmss")); 
    Response.ContentType = "application/zip";
    Response.AddHeader("content-disposition", "filename=" + archiveName);

    using (ZipFile zip = new ZipFile())
    {
        zip.AddFiles(f, "files");            
        zip.AddFileFromString("Readme.txt", "", ReadmeText);
        zip.Save(Response.OutputStream);
    }
    Response.Close();

or in VB.NET:

    Response.Clear
    Response.BufferOutput= false
    Dim ReadmeText As String= "README.TXT\n\nHello!\n\n" & _
                              "This is a zip file that was generated in ASP.NET"
    Dim archiveName as String= String.Format("archive-{0}.zip", _
               DateTime.Now.ToString("yyyy-MMM-dd-HHmmss"))
    Response.ContentType = "application/zip"
    Response.AddHeader("content-disposition", "filename=" + archiveName)

    Using zip as new ZipFile()
        zip.AddEntry("Readme.txt", "", ReadmeText, Encoding.Default)
        '' filesToInclude is a string[] or List<String>
        zip.AddFiles(filesToInclude, "files")            
        zip.Save(Response.OutputStream)
    End Using
    Response.Close
Cheeso
By far the best ZIP library for C#
kape123
A: 

But DotNetZip only lets you save, so its sent and starts downloading only when all files have been added to the zip on the server. Whereas with SharpZipLib you can start streaming immediately, dynamically adding files, till the end.

Seems like this should be a comment. It's true that DotNetZip wants must know the complete set of files to be in the zip, before writing the zip. That does not mean there is a zip constructed in memory. Do you have a need to delay the decision on which files to send to the browser, until after you started sending some of them? I can't see that being a very common scenario.
Cheeso
A: 

Commenting needs 50 reputation...
Yes it should have been a comment, and Cheeso, thanks for your answer, I hereby second the vote for DotNetZip as easier to code, (no low level), clearer to read, and does exactly what was requested in the above question: Construct, and send the zip to the user, on the fly.

A: 

One concern is the size of the file that you will be streaming to the client. If you use SharpZipLib to build the ZIP in-memory, you don't have any temp files to cleanup, but you'll soon run into memory issues if the files are large and a number of concurrent users are downloading files. (We experienced this pretty frequently when ZIP sizes got to the 200 MB+ range.) I worked around this by the temp file to disk, streaming it to the user, and deleting it when then request completed.

ProKiner
A: 

DotNetZip creates the stream without saving any resources on the server, so you don't have to remember to erase anything. As I said before, its fast and intuitive coding, with an efficient implementation.

Moshe

pashute