This project is read-only.

how to open an existing archive and insert into output stream?

Feb 23, 2009 at 11:00 PM
Edited Feb 23, 2009 at 11:26 PM
I need to open an existing archive, add a file (without altering the original saved archive) and insert the whole thing into the output stream for download. The following code will get the job done but with a poor user experience.

Response.ContentType = "application/zip";
Response.AddHeader("content-disposition", "filename=" + _FileName);
Response.AppendHeader("Content-Encoding", "application/x-zip-compressed");
Response.BufferOutput = false;

// create a new zip and insert into output stream
using (ZipFile zip = new ZipFile(Response.OutputStream))
    // add the original zip file requested
    zip.AddFile(Server.MapPath(_FilePath + _FileName));
    // add readme file
    zip.AddFile(Server.MapPath(_FilePath + "ReadMe.txt"));

The result is a zip file that contains both the original requested download zip + the new file, so the user has to unzip an archive that contains yet another zipped archive containing the files they originally requested.

Is it possible to simply open an archive and insert that into the filestream, rather than creating a new archive directly into the filestream?  I found a post on another forum that indicated that the following code should work, but it does not -- the Save() method will not allow Response.OutputStream as a valid parameter.

// open existing saved zip file
using (ZipFile zip = ZipFile.Read(Server.MapPath(_FilePath + _FileName)))
    // add readme file
    zip.AddFile(Server.MapPath(_FilePath + "ReadMe.txt"));
    // insert into output stream

Please help. Thanks!
Feb 24, 2009 at 12:23 AM
Edited Feb 24, 2009 at 12:24 AM
"the Save method will not allow...."

Why won't it allow that? What is the error you are getting?  a compile-time error or a Runtime exception?
more info please?
I believe what you are trying should work.
Feb 24, 2009 at 12:28 AM
Thanks, Cheeso. The error I'm getting is compile-time:

The best overloaded method match for 'Ionic.Utils.Zip.ZipFile.Save(string)' has some invalid arguments
Argument '1': cannot convert from 'System.IO.Stream' to 'string'

The Save() method seems to only want a string.

Feb 24, 2009 at 12:41 AM
What version of DotNetZip are you using?  In v1.6, there was no overload of the Save() method that accepted a Stream.  This was added in v1.7, which is the latest stable version.
You can download it from the releases tab.
Feb 24, 2009 at 1:10 AM
Thanks, Cheeso! I downloaded 1.7 and now it works perfectly.
Feb 24, 2009 at 2:19 AM
Super!  That was easy.