C# Class Crisis.Ionic.Zip.CountingStream

A decorator stream. It wraps another stream, and performs bookkeeping to keep track of the stream Position.

In some cases, it is not possible to get the Position of a stream, let's say, on a write-only output stream like ASP.NET's Response.OutputStream, or on a different write-only stream provided as the destination for the zip by the application. In this case, programmers can use this counting stream to count the bytes read or written.

Consider the scenario of an application that saves a self-extracting archive (SFX), that uses a custom SFX stub.

Saving to a filesystem file, the application would open the filesystem file (getting a FileStream), save the custom sfx stub into it, and then call ZipFile.Save(), specifying the same FileStream. ZipFile.Save() does the right thing for the zipentry offsets, by inquiring the Position of the FileStream before writing any data, and then adding that initial offset into any ZipEntry offsets in the zip directory. Everything works fine.

Now suppose the application is an ASPNET application and it saves directly to Response.OutputStream. It's not possible for DotNetZip to inquire the Position, so the offsets for the SFX will be wrong.

The workaround is for the application to use this class to wrap HttpResponse.OutputStream, then write the SFX stub and the ZipFile into that wrapper stream. Because ZipFile.Save() can inquire the Position, it will then do the right thing with the offsets.

Inheritance: System.IO.Stream
Show file Open project: teeknofil/Crisis-Wordlist-Generator Class Usage Examples

Public Methods

Method Description
Adjust ( System.Int64 delta ) : void

Adjust the byte count on the stream.

Subtract delta from the count of bytes written to the stream. This is necessary when seeking back, and writing additional data, as happens in some cases when saving Zip files.

CountingStream ( System stream ) : System

The constructor.

Flush ( ) : void

Flushes the underlying stream.

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

The read method.

Seek ( long offset, System origin ) : long

Seek in the stream.

SetLength ( long value ) : void

Set the length of the underlying stream. Be careful with this!

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

Write data into the stream.

Method Details

Adjust() public method

Adjust the byte count on the stream.

Subtract delta from the count of bytes written to the stream. This is necessary when seeking back, and writing additional data, as happens in some cases when saving Zip files.

public Adjust ( System.Int64 delta ) : void
delta System.Int64 /// the number of bytes to subtract from the count. ///
return void

CountingStream() public method

The constructor.
public CountingStream ( System stream ) : System
stream System The underlying stream
return System

Flush() public method

Flushes the underlying stream.
public Flush ( ) : void
return void

Read() public method

The read method.
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

Seek in the stream.
public Seek ( long offset, System origin ) : long
offset long the offset point to seek to
origin System the reference point from which to seek
return long

SetLength() public method

Set the length of the underlying stream. Be careful with this!
public SetLength ( long value ) : void
value long the length to set on the underlying stream.
return void

Write() public method

Write data into the stream.
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