C# Class Ionic.Zip.ZipInputStream

Provides a stream metaphor for reading zip files.

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

Some application designs require a readable stream for input. This stream can be used to read a zip file, and extract entries.

Both the ZipInputStream class and the ZipFile class can be used to read and extract zip files. Both of them support many of the common zip features, including Unicode, different compression levels, and ZIP64. The programming models differ. For example, when extracting entries via calls to the GetNextEntry() and Read() methods on the ZipInputStream class, the caller is responsible for creating the file, writing the bytes into the file, setting the attributes on the file, and setting the created, last modified, and last accessed timestamps on the file. All of these things are done automatically by a call to ZipEntry.Extract(). For this reason, the ZipInputStream is generally recommended for when your application wants to extract the data, without storing that data into a file.

Aside from the obvious differences in programming model, there are some differences in capability between the ZipFile class and the ZipInputStream class.

ZipFile can be used to create or update zip files, or read and extract zip files. ZipInputStream can be used only to read and extract zip files. If you want to use a stream to create zip files, check out the ZipOutputStream. ZipInputStream cannot read segmented or spanned zip files. ZipInputStream will not read Zip file comments. When reading larger files, ZipInputStream will always underperform ZipFile. This is because the ZipInputStream does a full scan on the zip file, while the ZipFile class reads the central directory of the zip file.
Inheritance: Stream
Show file Open project: puckipedia/MagisterAPI Class Usage Examples

Private Properties

Property Type Description
SetupStream void
_Init void

Public Methods

Method Description
Flush ( ) : void

This is a no-op.

GetNextEntry ( ) : ZipEntry

Read the next entry from the zip file.

Call this method just before calling Read(byte[], int, int), to position the pointer in the zip file to the next entry that can be read. Subsequent calls to Read(), will decrypt and decompress the data in the zip file, until Read() returns 0.

Each time you call GetNextEntry(), the pointer in the wrapped stream is moved to the next entry in the zip file. If you call , and thus re-position the pointer within the file, you will need to call GetNextEntry() again, to insure that the file pointer is positioned at the beginning of a zip entry.

This method returns the ZipEntry. Using a stream approach, you will read the raw bytes for an entry in a zip file via calls to Read(). Alternatively, you can extract an entry into a file, or a stream, by calling ZipEntry.Extract(), or one of its siblings.

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

Read the data from the stream into the buffer.

The data for the zipentry will be decrypted and uncompressed, as necessary, before being copied into the buffer.

You must set the Password property before calling Read() the first time for an encrypted entry. To determine if an entry is encrypted and requires a password, check the ZipEntry.Encryption property.

Seek ( long offset, SeekOrigin origin ) : long

This method seeks in the underlying stream.

Call this method if you want to seek around within the zip file for random access.

Applications can intermix calls to Seek() with calls to . After a call to Seek(), GetNextEntry() will get the next ZipEntry that falls after the current position in the input stream. You're on your own for finding out just where to seek in the stream, to get to the various entries.

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

This method always throws a NotSupportedException.

ZipInputStream ( Stream stream ) : System

Create a ZipInputStream, wrapping it around an existing stream.

While the ZipFile class is generally easier to use, this class provides an alternative to those applications that want to read from a zipfile directly, using a System.IO.Stream.

Both the ZipInputStream class and the ZipFile class can be used to read and extract zip files. Both of them support many of the common zip features, including Unicode, different compression levels, and ZIP64. The programming models differ. For example, when extracting entries via calls to the GetNextEntry() and Read() methods on the ZipInputStream class, the caller is responsible for creating the file, writing the bytes into the file, setting the attributes on the file, and setting the created, last modified, and last accessed timestamps on the file. All of these things are done automatically by a call to ZipEntry.Extract(). For this reason, the ZipInputStream is generally recommended for when your application wants to extract the data, without storing that data into a file.

Aside from the obvious differences in programming model, there are some differences in capability between the ZipFile class and the ZipInputStream class.

ZipFile can be used to create or update zip files, or read and extract zip files. ZipInputStream can be used only to read and extract zip files. If you want to use a stream to create zip files, check out the . ZipInputStream cannot read segmented or spanned zip files. ZipInputStream will not read Zip file comments. When reading larger files, ZipInputStream will always underperform ZipFile. This is because the ZipInputStream does a full scan on the zip file, while the ZipFile class reads the central directory of the zip file.
ZipInputStream ( Stream stream, bool leaveOpen ) : System

Create a ZipInputStream, explicitly specifying whether to keep the underlying stream open.

See the documentation for the ZipInputStream(Stream) constructor for a discussion of the class, and an example of how to use the class.

ZipInputStream ( String fileName ) : System

Create a ZipInputStream, given the name of an existing zip file.

This constructor opens a FileStream for the given zipfile, and wraps a ZipInputStream around that. See the documentation for the ZipInputStream(Stream) constructor for full details.

While the ZipFile class is generally easier to use, this class provides an alternative to those applications that want to read from a zipfile directly, using a System.IO.Stream.

Protected Methods

Method Description
Dispose ( bool disposing ) : void

Dispose the stream.

This method disposes the ZipInputStream. It may also close the underlying stream, depending on which constructor was used.

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

Application code won't call this code directly. This method may be invoked in two distinct scenarios. If disposing == true, the method has been called directly or indirectly by a user's code, for example via the public Dispose() method. In this case, both managed and unmanaged resources can be referenced and disposed. If disposing == false, the method has been called by the runtime from inside the object finalizer and this method should not reference other objects; in that case only unmanaged resources must be referenced or disposed.

Private Methods

Method Description
SetupStream ( ) : void
_Init ( Stream stream, bool leaveOpen, string name ) : void

Method Details

Dispose() protected method

Dispose the stream.

This method disposes the ZipInputStream. It may also close the underlying stream, depending on which constructor was used.

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

Application code won't call this code directly. This method may be invoked in two distinct scenarios. If disposing == true, the method has been called directly or indirectly by a user's code, for example via the public Dispose() method. In this case, both managed and unmanaged resources can be referenced and disposed. If disposing == false, the method has been called by the runtime from inside the object finalizer and this method should not reference other objects; in that case only unmanaged resources must be referenced or disposed.

protected Dispose ( bool disposing ) : void
disposing bool /// true if the Dispose method was invoked by user code. ///
return void

Flush() public method

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

GetNextEntry() public method

Read the next entry from the zip file.

Call this method just before calling Read(byte[], int, int), to position the pointer in the zip file to the next entry that can be read. Subsequent calls to Read(), will decrypt and decompress the data in the zip file, until Read() returns 0.

Each time you call GetNextEntry(), the pointer in the wrapped stream is moved to the next entry in the zip file. If you call , and thus re-position the pointer within the file, you will need to call GetNextEntry() again, to insure that the file pointer is positioned at the beginning of a zip entry.

This method returns the ZipEntry. Using a stream approach, you will read the raw bytes for an entry in a zip file via calls to Read(). Alternatively, you can extract an entry into a file, or a stream, by calling ZipEntry.Extract(), or one of its siblings.

public GetNextEntry ( ) : ZipEntry
return ZipEntry

Read() public method

Read the data from the stream into the buffer.

The data for the zipentry will be decrypted and uncompressed, as necessary, before being copied into the buffer.

You must set the Password property before calling Read() the first time for an encrypted entry. To determine if an entry is encrypted and requires a password, check the ZipEntry.Encryption property.

public Read ( byte buffer, int offset, int count ) : int
buffer byte The buffer to hold the data read from the stream.
offset int the offset within the buffer to copy the first byte read.
count int the number of bytes to read.
return int

Seek() public method

This method seeks in the underlying stream.

Call this method if you want to seek around within the zip file for random access.

Applications can intermix calls to Seek() with calls to . After a call to Seek(), GetNextEntry() will get the next ZipEntry that falls after the current position in the input stream. You're on your own for finding out just where to seek in the stream, to get to the various entries.

public Seek ( long offset, SeekOrigin origin ) : long
offset long the offset point to seek to
origin SeekOrigin the reference point from which to seek
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

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

ZipInputStream() public method

Create a ZipInputStream, wrapping it around an existing stream.

While the ZipFile class is generally easier to use, this class provides an alternative to those applications that want to read from a zipfile directly, using a System.IO.Stream.

Both the ZipInputStream class and the ZipFile class can be used to read and extract zip files. Both of them support many of the common zip features, including Unicode, different compression levels, and ZIP64. The programming models differ. For example, when extracting entries via calls to the GetNextEntry() and Read() methods on the ZipInputStream class, the caller is responsible for creating the file, writing the bytes into the file, setting the attributes on the file, and setting the created, last modified, and last accessed timestamps on the file. All of these things are done automatically by a call to ZipEntry.Extract(). For this reason, the ZipInputStream is generally recommended for when your application wants to extract the data, without storing that data into a file.

Aside from the obvious differences in programming model, there are some differences in capability between the ZipFile class and the ZipInputStream class.

ZipFile can be used to create or update zip files, or read and extract zip files. ZipInputStream can be used only to read and extract zip files. If you want to use a stream to create zip files, check out the . ZipInputStream cannot read segmented or spanned zip files. ZipInputStream will not read Zip file comments. When reading larger files, ZipInputStream will always underperform ZipFile. This is because the ZipInputStream does a full scan on the zip file, while the ZipFile class reads the central directory of the zip file.
public ZipInputStream ( Stream stream ) : System
stream Stream /// The stream to read. It must be readable. This stream will be closed at /// the time the ZipInputStream is closed. ///
return System

ZipInputStream() public method

Create a ZipInputStream, explicitly specifying whether to keep the underlying stream open.
See the documentation for the ZipInputStream(Stream) constructor for a discussion of the class, and an example of how to use the class.
public ZipInputStream ( Stream stream, bool leaveOpen ) : System
stream Stream /// The stream to read from. It must be readable. ///
leaveOpen bool /// true if the application would like the stream /// to remain open after the ZipInputStream has been closed. ///
return System

ZipInputStream() public method

Create a ZipInputStream, given the name of an existing zip file.

This constructor opens a FileStream for the given zipfile, and wraps a ZipInputStream around that. See the documentation for the ZipInputStream(Stream) constructor for full details.

While the ZipFile class is generally easier to use, this class provides an alternative to those applications that want to read from a zipfile directly, using a System.IO.Stream.

public ZipInputStream ( String fileName ) : System
fileName String /// The name of the filesystem file to read. ///
return System