- outputStream (Stream)
- The System.IO.Stream to write to. It must be writable. If you created the ZipFile instanct by calling ZipFile.Read(), this stream must not be the same stream you passed to ZipFile.Read().
The ZipFile instance is written to storage - typically a zip file in a filesystem, but using this overload, the storage can be anything accessible via a writable stream - only when the caller calls Save.
Use this method to save the zip content to a stream directly. A common scenario is an ASP.NET application that dynamically generates a zip file and allows the browser to download it. The application can call Save(Response.OutputStream) to write a zipfile directly to the output stream, without creating a zip file on the disk on the ASP.NET server.
Be careful when saving a file to a non-seekable stream, including Response.OutputStream. When DotNetZip writes to a non-seekable stream, the zip archive is formatted in such a way that may not be compatible with all zip tools on all platforms. It's a perfectly legal and compliant zip file, but some people have reported problems opening files produced this way using the Mac OS archive utility.
using (var zip = new Ionic.Zip.ZipFile()) { zip.CompressionLevel= Ionic.Zlib.CompressionLevel.BestCompression; zip.Password = "VerySecret."; zip.Encryption = EncryptionAlgorithm.WinZipAes128; zip.AddFile(sourceFileName); MemoryStream output = new MemoryStream(); zip.Save(output); byte[] zipbytes = output.ToArray(); }
This example shows a pitfall you should avoid. DO NOT read from a stream, then try to save to the same stream. DO NOT DO THIS:
using (var fs = new FileSteeam(filename, FileMode.Open)) { using (var zip = Ionic.Zip.ZipFile.Read(inputStream)) { zip.AddEntry("Name1.txt", "this is the content"); zip.Save(inputStream); // NO NO NO!! } }
Better like this:
using (var zip = Ionic.Zip.ZipFile.Read(filename)) { zip.AddEntry("Name1.txt", "this is the content"); zip.Save(); // YES! }