C# Class Ionic.Zlib.GZipStream

A class for compressing and decompressing GZIP streams.

The GZipStream is a Decorator on a Stream. It adds GZIP compression or decompression to any stream.

Like the System.IO.Compression.GZipStream in the .NET Base Class Library, the Ionic.Zlib.GZipStream can compress while writing, or decompress while reading, but not vice versa. The compression method used is GZIP, which is documented in IETF RFC 1952, "GZIP file format specification version 4.3".

A GZipStream can be used to decompress data (through Read()) or to compress data (through Write()), but not both.

If you wish to use the GZipStream to compress data, you must wrap it around a write-able stream. As you call Write() on the GZipStream, the data will be compressed into the GZIP format. If you want to decompress data, you must wrap the GZipStream around a readable stream that contains an IETF RFC 1952-compliant stream. The data will be decompressed as you call Read() on the GZipStream.

Though the GZIP format allows data from multiple files to be concatenated together, this stream handles only a single segment of GZIP format, typically representing a single file.

This class is similar to ZlibStream and DeflateStream. ZlibStream handles RFC1950-compliant streams. DeflateStream handles RFC1951-compliant streams. This class handles RFC1952-compliant streams.

Inheritance: System.IO.Stream
Datei anzeigen Open project: PlayFab/PlayFab-Samples Class Usage Examples

Public Properties

Property Type Description
LastModified DateTime?

Public Methods

Method Description
CompressBuffer ( byte b ) : byte[]

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

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

CompressString ( String s ) : byte[]

Compress a string into a byte array using GZip.

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

Flush ( ) : void

Flush the stream.

GZipStream ( Stream stream, CompressionMode mode ) : System

Create a GZipStream using the specified CompressionMode.

When mode is CompressionMode.Compress, the GZipStream will use the default compression level.

As noted in the class documentation, the CompressionMode (Compress or Decompress) also establishes the "direction" of the stream. A GZipStream with CompressionMode.Compress works only through Write(). A GZipStream with CompressionMode.Decompress works only through Read().

GZipStream ( Stream stream, CompressionMode mode, CompressionLevel level ) : System

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

The CompressionMode (Compress or Decompress) also establishes the "direction" of the stream. A GZipStream with CompressionMode.Compress works only through Write(). A GZipStream with CompressionMode.Decompress works only through Read().

GZipStream ( Stream stream, CompressionMode mode, CompressionLevel level, bool leaveOpen ) : System

Create a GZipStream using the specified CompressionMode and the specified CompressionLevel, 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 compressed data has been written to it. Specify true for the leaveOpen parameter to leave the stream open.

As noted in the class documentation, the CompressionMode (Compress or Decompress) also establishes the "direction" of the stream. A GZipStream with CompressionMode.Compress works only through Write(). A GZipStream with CompressionMode.Decompress works only through Read().

GZipStream ( Stream stream, CompressionMode mode, bool leaveOpen ) : System

Create a GZipStream 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 compressed data has been written to it. Specify true for the leaveOpen parameter to leave the stream open.

The CompressionMode (Compress or Decompress) also establishes the "direction" of the stream. A GZipStream with CompressionMode.Compress works only through Write(). A GZipStream with CompressionMode.Decompress works only through Read().

The GZipStream will use the default compression level. If you want to specify the compression level, see .

See the other overloads of this constructor for example code.

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

Read and decompress data from the source stream.

With a GZipStream, decompression is done through reading.

Seek ( long offset, SeekOrigin origin ) : long

Calling this method always throws a NotImplementedException.

SetLength ( long value ) : void

Calling this method always throws a NotImplementedException.

UncompressBuffer ( byte compressed ) : byte[]

Uncompress a GZip'ed byte array into a byte array.

UncompressString ( byte compressed ) : String

Uncompress a GZip'ed byte array into a single string.

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

Write data to the stream.

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

A GZipStream can be used for Read() or Write(), but not both. Writing implies compression. Reading implies decompression.

Protected Methods

Method Description
Dispose ( bool disposing ) : void

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.

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
EmitHeader ( ) : int

Method Details

CompressBuffer() public static method

Compress a byte array into a new byte array using GZip.
Uncompress it with GZipStream.UncompressBuffer(byte[]).
public static CompressBuffer ( byte b ) : byte[]
b byte /// A buffer to compress. ///
return byte[]

CompressString() public static method

Compress a string into a byte array using GZip.
Uncompress it with GZipStream.UncompressString(byte[]).
public static CompressString ( String s ) : byte[]
s String /// A string to compress. The string will first be encoded /// using UTF8, then compressed. ///
return byte[]

Dispose() protected method

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.

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 /// indicates whether the Dispose method was invoked by user code. ///
return void

Flush() public method

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

GZipStream() public method

Create a GZipStream using the specified CompressionMode.

When mode is CompressionMode.Compress, the GZipStream will use the default compression level.

As noted in the class documentation, the CompressionMode (Compress or Decompress) also establishes the "direction" of the stream. A GZipStream with CompressionMode.Compress works only through Write(). A GZipStream with CompressionMode.Decompress works only through Read().

public GZipStream ( Stream stream, CompressionMode mode ) : System
stream Stream The stream which will be read or written.
mode CompressionMode Indicates whether the GZipStream will compress or decompress.
return System

GZipStream() public method

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

The CompressionMode (Compress or Decompress) also establishes the "direction" of the stream. A GZipStream with CompressionMode.Compress works only through Write(). A GZipStream with CompressionMode.Decompress works only through Read().

public GZipStream ( Stream stream, CompressionMode mode, CompressionLevel level ) : System
stream Stream The stream to be read or written while deflating or inflating.
mode CompressionMode Indicates whether the GZipStream will compress or decompress.
level CompressionLevel A tuning knob to trade speed for effectiveness.
return System

GZipStream() public method

Create a GZipStream using the specified CompressionMode and the specified CompressionLevel, 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 compressed data has been written to it. Specify true for the leaveOpen parameter to leave the stream open.

As noted in the class documentation, the CompressionMode (Compress or Decompress) also establishes the "direction" of the stream. A GZipStream with CompressionMode.Compress works only through Write(). A GZipStream with CompressionMode.Decompress works only through Read().

public GZipStream ( Stream stream, CompressionMode mode, CompressionLevel level, bool leaveOpen ) : System
stream Stream The stream which will be read or written.
mode CompressionMode Indicates whether the GZipStream will compress or decompress.
level CompressionLevel A tuning knob to trade speed for effectiveness.
leaveOpen bool true if the application would like the stream to remain open after inflation/deflation.
return System

GZipStream() public method

Create a GZipStream 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 compressed data has been written to it. Specify true for the leaveOpen parameter to leave the stream open.

The CompressionMode (Compress or Decompress) also establishes the "direction" of the stream. A GZipStream with CompressionMode.Compress works only through Write(). A GZipStream with CompressionMode.Decompress works only through Read().

The GZipStream will use the default compression level. If you want to specify the compression level, see .

See the other overloads of this constructor for example code.

public GZipStream ( Stream stream, CompressionMode mode, bool leaveOpen ) : System
stream Stream /// The stream which will be read or written. This is called the "captive" /// stream in other places in this documentation. ///
mode CompressionMode Indicates whether the GZipStream will compress or decompress. ///
leaveOpen bool /// true if the application would like the base stream to remain open after /// inflation/deflation. ///
return System

Read() public method

Read and decompress data from the source stream.
With a GZipStream, decompression is done through reading.
public Read ( byte buffer, int offset, int count ) : int
buffer byte The buffer into which the decompressed data should be placed.
offset int the offset within that data array to put the first byte read.
count int the number of bytes to read.
return int

Seek() public method

Calling this method always throws a NotImplementedException.
public Seek ( long offset, SeekOrigin origin ) : long
offset long irrelevant; it will always throw!
origin SeekOrigin irrelevant; it will always throw!
return long

SetLength() public method

Calling this method always throws a NotImplementedException.
public SetLength ( long value ) : void
value long irrelevant; this method will always throw!
return void

UncompressBuffer() public static method

Uncompress a GZip'ed byte array into a byte array.
public static UncompressBuffer ( byte compressed ) : byte[]
compressed byte /// A buffer containing data that has been compressed with GZip. ///
return byte[]

UncompressString() public static method

Uncompress a GZip'ed byte array into a single string.
public static UncompressString ( byte compressed ) : String
compressed byte /// A buffer containing GZIP-compressed data. ///
return String

Write() public method

Write data to the stream.

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

A GZipStream can be used for Read() or Write(), but not both. Writing implies compression. Reading implies decompression.

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

Property Details

LastModified public_oe property

The last modified time for the GZIP stream.
GZIP allows the storage of a last modified time with each GZIP entry. When compressing data, you can set this before the first call to Write(). When decompressing, you can retrieve this value any time after the first call to Read().
public DateTime? LastModified
return DateTime?