Save Method (outputStream)

DotNetZip

Ionic Zip Library v1.9.1.6 Save Method (outputStream)
ReferenceIonic.ZipZipFileSave(Stream)
Save the zip archive to the specified stream.
Declaration Syntax
C# Visual Basic Visual C++
public void Save(
	Stream outputStream
)
Public Sub Save ( _
	outputStream As Stream _
)
public:
void Save(
	Stream^ outputStream
)
Parameters
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().
Remarks

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.

Examples
This example saves the zipfile content into a MemoryStream, and then gets the array of bytes from that MemoryStream.
CopyC#
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();
}
Examples

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:

CopyC#
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:

CopyC#
using (var zip = Ionic.Zip.ZipFile.Read(filename))
{
    zip.AddEntry("Name1.txt", "this is the content");
    zip.Save();  // YES!
}

Assembly: Ionic.Zip (Module: Ionic.Zip) Version: 1.9.1.8 (1.9.1.8)