Detailed Description

A class for compressing and decompressing streams using the Deflate algorithm.

The DeflateStream is a Decorator

on a System.IO.Stream. It adds DEFLATE compression or decompression to any stream.

Using this stream, applications can compress or decompress data via stream Read and Write operations. Either compresssion or decompression can occur through either reading or writing. The compression format used is DEFLATE, which is documented in IETF RFC 1951

, "DEFLATE Compressed Data Format Specification version 1.3.".

This class is similar to ZlibStream, except that ZlibStream adds the RFC 1950 - ZLIB

framing bytes to a compressed stream when compressing, or expects the RFC1950 framing bytes when decompressing. The DeflateStream does not.

See Also
ZlibStream, GZipStream
Inheritance diagram for Zlib.DeflateStream:

Public Member Functions

 DeflateStream (System.IO.Stream stream, CompressionMode mode)
 Create a DeflateStream using the specified CompressionMode. More...
 
 DeflateStream (System.IO.Stream stream, CompressionMode mode, CompressionLevel level)
 Create a DeflateStream using the specified CompressionMode and the specified CompressionLevel. More...
 
 DeflateStream (System.IO.Stream stream, CompressionMode mode, bool leaveOpen)
 Create a DeflateStream using the specified CompressionMode, and explicitly specify whether the stream should be left open after Deflation or Inflation. More...
 
 DeflateStream (System.IO.Stream stream, CompressionMode mode, CompressionLevel level, bool leaveOpen)
 Create a DeflateStream using the specified CompressionMode and the specified CompressionLevel, and explicitly specify whether the stream should be left open after Deflation or Inflation. More...
 
void SetDataToRead (int size)
 
void ChangeStream (System.IO.Stream stream)
 
void DoneRead ()
 
override void Flush ()
 Flush the stream. More...
 
override int Read (byte[] buffer, int offset, int count)
 Read data from the stream. More...
 
override long Seek (long offset, System.IO.SeekOrigin origin)
 Calling this method always throws a NotImplementedException. More...
 
override void SetLength (long value)
 Calling this method always throws a NotImplementedException. More...
 
override void Write (byte[] buffer, int offset, int count)
 Write data to the stream. More...
 

Static Public Member Functions

static byte[] CompressString (String s)
 Compress a string into a byte array using DEFLATE (RFC 1951). More...
 
static byte[] CompressBuffer (byte[] b)
 Compress a byte array into a new byte array using DEFLATE. More...
 
static String UncompressString (byte[] compressed)
 Uncompress a DEFLATE'd byte array into a single string. More...
 
static byte[] UncompressBuffer (byte[] compressed)
 Uncompress a DEFLATE'd byte array into a byte array. More...
 

Protected Member Functions

override void Dispose (bool disposing)
 Dispose the stream. More...
 

Package Attributes

ZlibBaseStream _baseStream
 
System.IO.Stream _innerStream
 

Properties

virtual FlushType FlushMode [get, set]
 This property sets the flush behavior on the stream. More...
 
int BufferSize [get, set]
 The size of the working buffer for the compression codec. More...
 
CompressionStrategy Strategy [get, set]
 The ZLIB strategy to be used during compression. More...
 
virtual long TotalIn [get]
 Returns the total number of bytes input so far. More...
 
virtual long TotalOut [get]
 Returns the total number of bytes output so far. More...
 
override bool CanRead [get]
 Indicates whether the stream can be read. More...
 
override bool CanSeek [get]
 Indicates whether the stream supports Seek operations. More...
 
override bool CanWrite [get]
 Indicates whether the stream can be written. More...
 
override long Length [get]
 Reading this property always throws a NotImplementedException. More...
 
override long Position [get, set]
 The position of the stream pointer. More...
 

Private Attributes

bool _disposed
 

Constructor & Destructor Documentation

Zlib.DeflateStream.DeflateStream ( System.IO.Stream  stream,
CompressionMode  mode 
)
inline

Create a DeflateStream using the specified CompressionMode.

When mode is CompressionMode.Compress, the DeflateStream will use the default compression level. The "captive" stream will be closed when the DeflateStream is closed.

This example uses a DeflateStream to compress data from a file, and writes the compressed data to another file.

using (System.IO.Stream input = System.IO.File.OpenRead(fileToCompress))
{
using (var raw = System.IO.File.Create(fileToCompress + ".deflated"))
{
using (Stream compressor = new DeflateStream(raw, CompressionMode.Compress))
{
byte[] buffer = new byte[WORKING_BUFFER_SIZE];
int n;
while ((n= input.Read(buffer, 0, buffer.Length)) != 0)
{
compressor.Write(buffer, 0, n);
}
}
}
}
Using input As Stream = File.OpenRead(fileToCompress)
Using raw As FileStream = File.Create(fileToCompress & ".deflated")
Using compressor As Stream = New DeflateStream(raw, CompressionMode.Compress)
Dim buffer As Byte() = New Byte(4096) {}
Dim n As Integer = -1
Do While (n <> 0)
If (n > 0) Then
compressor.Write(buffer, 0, n)
End If
n = input.Read(buffer, 0, buffer.Length)
Loop
End Using
End Using
End Using
Parameters
streamThe stream which will be read or written.
modeIndicates whether the DeflateStream will compress or decompress.
Zlib.DeflateStream.DeflateStream ( System.IO.Stream  stream,
CompressionMode  mode,
CompressionLevel  level 
)
inline

Create a DeflateStream using the specified CompressionMode and the specified CompressionLevel.

When mode is CompressionMode.Decompress, the level parameter is ignored. The "captive" stream will be closed when the DeflateStream is closed.

This example uses a DeflateStream to compress data from a file, and writes the compressed data to another file.

using (System.IO.Stream input = System.IO.File.OpenRead(fileToCompress))
{
using (var raw = System.IO.File.Create(fileToCompress + ".deflated"))
{
using (Stream compressor = new DeflateStream(raw,
CompressionMode.Compress,
CompressionLevel.BestCompression))
{
byte[] buffer = new byte[WORKING_BUFFER_SIZE];
int n= -1;
while (n != 0)
{
if (n > 0)
compressor.Write(buffer, 0, n);
n= input.Read(buffer, 0, buffer.Length);
}
}
}
}
Using input As Stream = File.OpenRead(fileToCompress)
Using raw As FileStream = File.Create(fileToCompress & ".deflated")
Using compressor As Stream = New DeflateStream(raw, CompressionMode.Compress, CompressionLevel.BestCompression)
Dim buffer As Byte() = New Byte(4096) {}
Dim n As Integer = -1
Do While (n <> 0)
If (n > 0) Then
compressor.Write(buffer, 0, n)
End If
n = input.Read(buffer, 0, buffer.Length)
Loop
End Using
End Using
End Using
Parameters
streamThe stream to be read or written while deflating or inflating.
modeIndicates whether the DeflateStream will compress or decompress.
levelA tuning knob to trade speed for effectiveness.
Zlib.DeflateStream.DeflateStream ( System.IO.Stream  stream,
CompressionMode  mode,
bool  leaveOpen 
)
inline

Create a DeflateStream using the specified CompressionMode, and explicitly specify whether the stream should be left open after Deflation or Inflation.

This constructor allows the application to request that the captive stream remain open after the deflation or inflation occurs. By default, after Close() is called on the stream, the captive stream is also closed. In some cases this is not desired, for example if the stream is a memory stream that will be re-read after compression. Specify true for the leaveOpen parameter to leave the stream open.

The DeflateStream will use the default compression level.

See the other overloads of this constructor for example code.

Parameters
streamThe stream which will be read or written. This is called the "captive" stream in other places in this documentation.
Parameters
modeIndicates whether the DeflateStream will compress or decompress.
Parameters
leaveOpentrue if the application would like the stream to remain open after inflation/deflation.
Zlib.DeflateStream.DeflateStream ( System.IO.Stream  stream,
CompressionMode  mode,
CompressionLevel  level,
bool  leaveOpen 
)
inline

Create a DeflateStream using the specified CompressionMode and the specified CompressionLevel, and explicitly specify whether the stream should be left open after Deflation or Inflation.

When mode is CompressionMode.Decompress, the level parameter is ignored.

This constructor allows the application to request that the captive stream remain open after the deflation or inflation occurs. By default, after Close() is called on the stream, the captive stream is also closed. In some cases this is not desired, for example if the stream is a System.IO.MemoryStream that will be re-read after compression. Specify true for the leaveOpen parameter to leave the stream open.

This example shows how to use a DeflateStream to compress data from a file, and store the compressed data into another file.

using (var output = System.IO.File.Create(fileToCompress + ".deflated"))
{
using (System.IO.Stream input = System.IO.File.OpenRead(fileToCompress))
{
using (Stream compressor = new DeflateStream(output, CompressionMode.Compress, CompressionLevel.BestCompression, true))
{
byte[] buffer = new byte[WORKING_BUFFER_SIZE];
int n= -1;
while (n != 0)
{
if (n > 0)
compressor.Write(buffer, 0, n);
n= input.Read(buffer, 0, buffer.Length);
}
}
}
// can write additional data to the output stream here
}
Using output As FileStream = File.Create(fileToCompress & ".deflated")
Using input As Stream = File.OpenRead(fileToCompress)
Using compressor As Stream = New DeflateStream(output, CompressionMode.Compress, CompressionLevel.BestCompression, True)
Dim buffer As Byte() = New Byte(4096) {}
Dim n As Integer = -1
Do While (n <> 0)
If (n > 0) Then
compressor.Write(buffer, 0, n)
End If
n = input.Read(buffer, 0, buffer.Length)
Loop
End Using
End Using
' can write additional data to the output stream here.
End Using
Parameters
streamThe stream which will be read or written.
modeIndicates whether the DeflateStream will compress or decompress.
leaveOpentrue if the application would like the stream to remain open after inflation/deflation.
levelA tuning knob to trade speed for effectiveness.

Member Function Documentation

void Zlib.DeflateStream.ChangeStream ( System.IO.Stream  stream)
inline
static byte [] Zlib.DeflateStream.CompressBuffer ( byte[]  b)
inlinestatic

Compress a byte array into a new byte array using DEFLATE.

Uncompress it with DeflateStream.UncompressBuffer(byte[]).

See Also
DeflateStream.CompressString(string)

DeflateStream.CompressString(string)

See Also
DeflateStream.UncompressBuffer(byte[])

DeflateStream.UncompressBuffer(byte[])

See Also
GZipStream.CompressBuffer(byte[])

GZipStream.CompressBuffer(byte[])

See Also
ZlibStream.CompressBuffer(byte[])

ZlibStream.CompressBuffer(byte[])

Parameters
bA buffer to compress.
Returns
The data in compressed form
static byte [] Zlib.DeflateStream.CompressString ( String  s)
inlinestatic

Compress a string into a byte array using DEFLATE (RFC 1951).

Uncompress it with DeflateStream.UncompressString(byte[]).

See Also
DeflateStream.UncompressString(byte[])

DeflateStream.UncompressString(byte[])

See Also
DeflateStream.CompressBuffer(byte[])

DeflateStream.CompressBuffer(byte[])

See Also
GZipStream.CompressString(string)

GZipStream.CompressString(string)

See Also
ZlibStream.CompressString(string)

ZlibStream.CompressString(string)

Parameters
sA string to compress. The string will first be encoded using UTF8, then compressed.
Returns
The string in compressed form
override void Zlib.DeflateStream.Dispose ( bool  disposing)
inlineprotected

Dispose the stream.

This may or may not result in a Close() call on the captive stream. See the constructors that have a leaveOpen parameter for more information.

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.

Parameters
disposingtrue if the Dispose method was invoked by user code.
void Zlib.DeflateStream.DoneRead ( )
inline
override void Zlib.DeflateStream.Flush ( )
inline

Flush the stream.

override int Zlib.DeflateStream.Read ( byte[]  buffer,
int  offset,
int  count 
)
inline

Read data from the stream.

If you wish to use the DeflateStream to compress data while reading, you can create a DeflateStream with CompressionMode.Compress, providing an uncompressed data stream. Then call Read() on that DeflateStream, and the data read will be compressed as you read. If you wish to use the DeflateStream to decompress data while reading, you can create a DeflateStream with CompressionMode.Decompress, providing a readable compressed data stream. Then call Read() on that DeflateStream, and the data read will be decompressed as you read.

A DeflateStream can be used for Read() or Write(), but not both.

Parameters
bufferThe buffer into which the read data should be placed.
offsetthe offset within that data array to put the first byte read.
countthe number of bytes to read.
Returns
the number of bytes actually read
override long Zlib.DeflateStream.Seek ( long  offset,
System.IO.SeekOrigin  origin 
)
inline

Calling this method always throws a NotImplementedException.

Parameters
offsetthis is irrelevant, since it will always throw!
originthis is irrelevant, since it will always throw!
Returns
irrelevant!
void Zlib.DeflateStream.SetDataToRead ( int  size)
inline
override void Zlib.DeflateStream.SetLength ( long  value)
inline

Calling this method always throws a NotImplementedException.

Parameters
valuethis is irrelevant, since it will always throw!
static byte [] Zlib.DeflateStream.UncompressBuffer ( byte[]  compressed)
inlinestatic

Uncompress a DEFLATE'd byte array into a byte array.

See Also
DeflateStream.CompressBuffer(byte[])

DeflateStream.CompressBuffer(byte[])

See Also
DeflateStream.UncompressString(byte[])

DeflateStream.UncompressString(byte[])

See Also
GZipStream.UncompressBuffer(byte[])

GZipStream.UncompressBuffer(byte[])

See Also
ZlibStream.UncompressBuffer(byte[])

ZlibStream.UncompressBuffer(byte[])

Parameters
compressedA buffer containing data that has been compressed with DEFLATE.
Returns
The data in uncompressed form
static String Zlib.DeflateStream.UncompressString ( byte[]  compressed)
inlinestatic

Uncompress a DEFLATE'd byte array into a single string.

See Also
DeflateStream.CompressString(String)

DeflateStream.CompressString(String)

See Also
DeflateStream.UncompressBuffer(byte[])

DeflateStream.UncompressBuffer(byte[])

See Also
GZipStream.UncompressString(byte[])

GZipStream.UncompressString(byte[])

See Also
ZlibStream.UncompressString(byte[])

ZlibStream.UncompressString(byte[])

Parameters
compressedA buffer containing DEFLATE-compressed data.
Returns
The uncompressed string
override void Zlib.DeflateStream.Write ( byte[]  buffer,
int  offset,
int  count 
)
inline

Write data to the stream.

If you wish to use the DeflateStream to compress data while writing, you can create a DeflateStream with CompressionMode.Compress, and a writable output stream. Then call Write() on that DeflateStream, providing uncompressed data as input. The data sent to the output stream will be the compressed form of the data written. If you wish to use the DeflateStream to decompress data while writing, you can create a DeflateStream with CompressionMode.Decompress, and a writable output stream. Then call Write() on that stream, providing previously compressed data. The data sent to the output stream will be the decompressed form of the data written.

A DeflateStream can be used for Read() or Write(), but not both.

Parameters
bufferThe buffer holding data to write to the stream.
offsetthe offset within that data array to find the first byte to write.
countthe number of bytes to write.

Member Data Documentation

ZlibBaseStream Zlib.DeflateStream._baseStream
package
bool Zlib.DeflateStream._disposed
private
System.IO.Stream Zlib.DeflateStream._innerStream
package

Property Documentation

int Zlib.DeflateStream.BufferSize
getset

The size of the working buffer for the compression codec.

The working buffer is used for all stream operations. The default size is 1024 bytes. The minimum size is 128 bytes. You may get better performance with a larger buffer. Then again, you might not. You would have to test it.

Set this before the first call to Read() or Write() on the stream. If you try to set it afterwards, it will throw.

override bool Zlib.DeflateStream.CanRead
get

Indicates whether the stream can be read.

The return value depends on whether the captive stream supports reading.

override bool Zlib.DeflateStream.CanSeek
get

Indicates whether the stream supports Seek operations.

Always returns false.

override bool Zlib.DeflateStream.CanWrite
get

Indicates whether the stream can be written.

The return value depends on whether the captive stream supports writing.

virtual FlushType Zlib.DeflateStream.FlushMode
getsetpackage

This property sets the flush behavior on the stream.

See the ZLIB documentation for the meaning of the flush behavior.

override long Zlib.DeflateStream.Length
get

Reading this property always throws a NotImplementedException.

override long Zlib.DeflateStream.Position
getset

The position of the stream pointer.

Setting this property always throws a NotImplementedException. Reading will return the total bytes written out, if used in writing, or the total bytes read in, if used in reading. The count may refer to compressed bytes or uncompressed bytes, depending on how you've used the stream.

CompressionStrategy Zlib.DeflateStream.Strategy
getsetpackage

The ZLIB strategy to be used during compression.

By tweaking this parameter, you may be able to optimize the compression for data with particular characteristics.

virtual long Zlib.DeflateStream.TotalIn
get

Returns the total number of bytes input so far.

virtual long Zlib.DeflateStream.TotalOut
get

Returns the total number of bytes output so far.