C# Class Crisis.Ionic.Zip.ZipOutputStream

Provides a stream metaphor for generating zip files.

This class writes zip files, as defined in the specification for zip files described by PKWare. The compression for this implementation is provided by a managed-code version of Zlib, included with DotNetZip in the classes in the Ionic.Zlib namespace.

This class provides an alternative programming model to the one enabled by the ZipFile class. Use this when creating zip files, as an alternative to the ZipFile class, when you would like to use a Stream type to write the zip file.

Both the ZipOutputStream class and the ZipFile class can be used to create zip files. Both of them support many of the common zip features, including Unicode, different compression levels, and ZIP64. They provide very similar performance when creating zip files.

The ZipFile class is generally easier to use than ZipOutputStream and should be considered a higher-level interface. For example, when creating a zip file via calls to the PutNextEntry() and Write() methods on the ZipOutputStream class, the caller is responsible for opening the file, reading the bytes from the file, writing those bytes into the ZipOutputStream, setting the attributes on the ZipEntry, and setting the created, last modified, and last accessed timestamps on the zip entry. All of these things are done automatically by a call to ZipFile.AddFile(). For this reason, the ZipOutputStream is generally recommended for use only when your application emits arbitrary data, not necessarily data from a filesystem file, directly into a zip file, and does so using a Stream metaphor.

Aside from the differences in programming model, there are other differences in capability between the two classes.

ZipFile can be used to read and extract zip files, in addition to creating zip files. ZipOutputStream cannot read zip files. If you want to use a stream to read zip files, check out the ZipInputStream class. ZipOutputStream does not support the creation of segmented or spanned zip files. ZipOutputStream cannot produce a self-extracting archive.

Be aware that the ZipOutputStream class implements the interface. In order for ZipOutputStream to produce a valid zip file, you use use it within a using clause (Using in VB), or call the Dispose() method explicitly. See the examples for how to employ a using clause.

Also, a note regarding compression performance: On the desktop .NET Framework, DotNetZip can use a multi-threaded compression implementation that provides significant speed increases on large files, over 300k or so, at the cost of increased memory use at runtime. (The output of the compression is almost exactly the same size). But, the multi-threaded approach incurs a performance hit on smaller files. There's no way for the ZipOutputStream to know whether parallel compression will be beneficial, because the ZipOutputStream does not know how much data you will write through the stream. You may wish to set the property to zero, if you are compressing large files through ZipOutputStream. This will cause parallel compression to be used, always.

Inheritance: Stream
Mostra file Open project: teeknofil/Crisis-Wordlist-Generator Class Usage Examples

Private Properties

Property Type Description
InsureUniqueEntry void
_FinishCurrentEntry void
_Init void
_InitiateCurrentEntry void

Public Methods

Method Description
ContainsEntry ( string name ) : bool

Returns true if an entry by the given name has already been written to the ZipOutputStream.

Flush ( ) : void

This is a no-op.

PutNextEntry ( String entryName ) : ZipEntry

Specify the name of the next entry that will be written to the zip file.

Call this method just before calling Write(byte[], int, int), to specify the name of the entry that the next set of bytes written to the ZipOutputStream belongs to. All subsequent calls to Write, until the next call to PutNextEntry, will be inserted into the named entry in the zip file.

If the entryName used in PutNextEntry() ends in a slash, then the entry added is marked as a directory. Because directory entries do not contain data, a call to Write(), before an intervening additional call to PutNextEntry(), will throw an exception.

If you don't call Write() between two calls to PutNextEntry(), the first entry is inserted into the zip file as a file of zero size. This may be what you want.

Because PutNextEntry() closes out the prior entry, if any, this method may throw if there is a problem with the prior entry.

This method returns the ZipEntry. You can modify public properties on the ZipEntry, such as ZipEntry.Encryption, , and so on, until the first call to ZipOutputStream.Write(), or until the next call to PutNextEntry(). If you modify the ZipEntry after having called Write(), you may get a runtime exception, or you may silently get an invalid zip archive.

Read ( byte buffer, int offset, int count ) : int

This method always throws a NotSupportedException.

Seek ( long offset, SeekOrigin origin ) : long

This method always throws a NotSupportedException.

SetLength ( long value ) : void

This method always throws a NotSupportedException.

ToString ( ) : String

Provides a string representation of the instance.

This can be useful for debugging purposes.

Write ( byte buffer, int offset, int count ) : void

Write the data from the buffer to the stream.

As the application writes data into this stream, the data may be compressed and encrypted before being written out to the underlying stream, depending on the settings of the CompressionLevel and the Encryption properties.

ZipOutputStream ( Stream stream ) : System

Create a ZipOutputStream, wrapping an existing stream.

The ZipFile class is generally easier to use when creating zip files. The ZipOutputStream offers a different metaphor for creating a zip file, based on the System.IO.Stream class.

ZipOutputStream ( Stream stream, bool leaveOpen ) : System

Create a ZipOutputStream.

See the documentation for the ZipOutputStream(Stream) constructor for an example.

ZipOutputStream ( String fileName ) : System

Create a ZipOutputStream that writes to a filesystem file.

The ZipFile class is generally easier to use when creating zip files. The ZipOutputStream offers a different metaphor for creating a zip file, based on the System.IO.Stream class.

Protected Methods

Method Description
Dispose ( bool disposing ) : void

Dispose the stream

This method writes the Zip Central directory, then closes the stream. The application must call Dispose() (or Close) in order to produce a valid zip file.

Typically the application will call Dispose() implicitly, via a using statement in C#, or a Using statement in VB.

Private Methods

Method Description
InsureUniqueEntry ( ZipEntry ze1 ) : void
_FinishCurrentEntry ( ) : void
_Init ( Stream stream, bool leaveOpen, string name ) : void
_InitiateCurrentEntry ( bool finishing ) : void

Method Details

ContainsEntry() public method

Returns true if an entry by the given name has already been written to the ZipOutputStream.
public ContainsEntry ( string name ) : bool
name string /// The name of the entry to scan for. ///
return bool

Dispose() protected method

Dispose the stream

This method writes the Zip Central directory, then closes the stream. The application must call Dispose() (or Close) in order to produce a valid zip file.

Typically the application will call Dispose() implicitly, via a using statement in C#, or a Using statement in VB.

protected Dispose ( bool disposing ) : void
disposing bool set this to true, always.
return void

Flush() public method

This is a no-op.
public Flush ( ) : void
return void

PutNextEntry() public method

Specify the name of the next entry that will be written to the zip file.

Call this method just before calling Write(byte[], int, int), to specify the name of the entry that the next set of bytes written to the ZipOutputStream belongs to. All subsequent calls to Write, until the next call to PutNextEntry, will be inserted into the named entry in the zip file.

If the entryName used in PutNextEntry() ends in a slash, then the entry added is marked as a directory. Because directory entries do not contain data, a call to Write(), before an intervening additional call to PutNextEntry(), will throw an exception.

If you don't call Write() between two calls to PutNextEntry(), the first entry is inserted into the zip file as a file of zero size. This may be what you want.

Because PutNextEntry() closes out the prior entry, if any, this method may throw if there is a problem with the prior entry.

This method returns the ZipEntry. You can modify public properties on the ZipEntry, such as ZipEntry.Encryption, , and so on, until the first call to ZipOutputStream.Write(), or until the next call to PutNextEntry(). If you modify the ZipEntry after having called Write(), you may get a runtime exception, or you may silently get an invalid zip archive.

public PutNextEntry ( String entryName ) : ZipEntry
entryName String /// The name of the entry to be added, including any path to be used /// within the zip file. ///
return ZipEntry

Read() public method

This method always throws a NotSupportedException.
public Read ( byte buffer, int offset, int count ) : int
buffer byte ignored
offset int ignored
count int ignored
return int

Seek() public method

This method always throws a NotSupportedException.
public Seek ( long offset, SeekOrigin origin ) : long
offset long ignored
origin SeekOrigin ignored
return long

SetLength() public method

This method always throws a NotSupportedException.
public SetLength ( long value ) : void
value long ignored
return void

ToString() public method

Provides a string representation of the instance.

This can be useful for debugging purposes.

public ToString ( ) : String
return String

Write() public method

Write the data from the buffer to the stream.
As the application writes data into this stream, the data may be compressed and encrypted before being written out to the underlying stream, depending on the settings of the CompressionLevel and the Encryption properties.
public Write ( byte buffer, int offset, int count ) : void
buffer byte The buffer holding data to write to the stream.
offset int the offset within that data array to find the first byte to write.
count int the number of bytes to write.
return void

ZipOutputStream() public method

Create a ZipOutputStream, wrapping an existing stream.

The ZipFile class is generally easier to use when creating zip files. The ZipOutputStream offers a different metaphor for creating a zip file, based on the System.IO.Stream class.

public ZipOutputStream ( Stream stream ) : System
stream Stream /// The stream to wrap. It must be writable. This stream will be closed at /// the time the ZipOutputStream is closed. ///
return System

ZipOutputStream() public method

Create a ZipOutputStream.
See the documentation for the ZipOutputStream(Stream) constructor for an example.
public ZipOutputStream ( Stream stream, bool leaveOpen ) : System
stream Stream /// The stream to wrap. It must be writable. ///
leaveOpen bool /// true if the application would like the stream /// to remain open after the ZipOutputStream has been closed. ///
return System

ZipOutputStream() public method

Create a ZipOutputStream that writes to a filesystem file.
The ZipFile class is generally easier to use when creating zip files. The ZipOutputStream offers a different metaphor for creating a zip file, based on the System.IO.Stream class.
public ZipOutputStream ( String fileName ) : System
fileName String /// The name of the zip file to create. ///
return System