Table of Contents

Class CountingStream

Namespace
Ionic.Zip
Assembly
SunamoDotNetZip.dll

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

public class CountingStream : Stream, IAsyncDisposable, IDisposable
Inheritance
CountingStream
Implements
Inherited Members
Extension Methods

Remarks

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.

Constructors

CountingStream(Stream)

The constructor.

public CountingStream(Stream stream)

Parameters

stream Stream

The underlying stream

Properties

BytesRead

the count of bytes that have been read from the stream.

public long BytesRead { get; }

Property Value

long

BytesWritten

The count of bytes written out to the stream.

public long BytesWritten { get; }

Property Value

long

CanRead

Whether the stream can be read.

public override bool CanRead { get; }

Property Value

bool

CanSeek

Whether it is possible to call Seek() on the stream.

public override bool CanSeek { get; }

Property Value

bool

CanWrite

Whether it is possible to call Write() on the stream.

public override bool CanWrite { get; }

Property Value

bool

ComputedPosition

Returns the sum of number of bytes written, plus the initial offset before writing.

public long ComputedPosition { get; }

Property Value

long

Length

The length of the underlying stream.

public override long Length { get; }

Property Value

long

Position

The Position of the stream.

public override long Position { get; set; }

Property Value

long

WrappedStream

Gets the wrapped stream.

public Stream WrappedStream { get; }

Property Value

Stream

Methods

Adjust(long)

Adjust the byte count on the stream.

public void Adjust(long delta)

Parameters

delta long

the number of bytes to subtract from the count.

Remarks

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.

Flush()

Flushes the underlying stream.

public override void Flush()

Read(byte[], int, int)

The read method.

public override int Read(byte[] buffer, int offset, int count)

Parameters

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.

Returns

int

the number of bytes read, after decryption and decompression.

Seek(long, SeekOrigin)

Seek in the stream.

public override long Seek(long offset, SeekOrigin origin)

Parameters

offset long

the offset point to seek to

origin SeekOrigin

the reference point from which to seek

Returns

long

The new position

SetLength(long)

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

public override void SetLength(long value)

Parameters

value long

the length to set on the underlying stream.

Write(byte[], int, int)

Write data into the stream.

public override void Write(byte[] buffer, int offset, int count)

Parameters

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.