My Project
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Properties
Public Member Functions | Public Attributes | Properties | List of all members
UniExtensions.Zlib.GZipStream Class Reference

A class for compressing and decompressing GZIP streams. More...

Inheritance diagram for UniExtensions.Zlib.GZipStream:

Public Member Functions

 GZipStream (System.IO.Stream stream, CompressionMode mode)
 Create a GZipStream using the specified CompressionMode. More...
 
 GZipStream (System.IO.Stream stream, CompressionMode mode, CompressionLevel level)
 Create a GZipStream using the specified CompressionMode and the specified CompressionLevel. More...
 
 GZipStream (System.IO.Stream stream, CompressionMode mode, bool leaveOpen)
 Create a GZipStream using the specified CompressionMode, and explicitly specify whether the stream should be left open after Deflation or Inflation. More...
 
 GZipStream (System.IO.Stream stream, CompressionMode mode, CompressionLevel level, bool leaveOpen)
 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. More...
 
override void Close ()
 Close the stream. More...
 
override void Flush ()
 Flush the stream. More...
 
override int Read (byte[] buffer, int offset, int count)
 Read and decompress data from the source 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...
 

Public Attributes

String Comment
 The Comment on the GZIP stream. More...
 
DateTime LastModified
 The last modified time for the GZIP stream. More...
 

Properties

String FileName [get, set]
 The FileName for the GZIP stream. More...
 
int Crc32 [get]
 The CRC on the GZIP stream. More...
 
virtual int FlushMode [get, set]
 This property sets the flush behavior on the stream. Sorry, though, not sure exactly how to describe all the various settings. More...
 
int BufferSize [get, set]
 Callers can set the buffer size of the working buffer with this property. 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]
 Reading or Writing this property always throws a NotImplementedException. More...
 

Detailed Description

A class for compressing and decompressing GZIP streams.

Like the GZipStream in the .NET Base Class Library, the Ionic.Zlib.GZipStream can compress while writing, or decompress while reading, but not vice versa.

A GZipStream can be used for Read() or Write(), and thus decompression or compression, but not both.

If you wish to use the GZipStream to compress (deflate) 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 (IETF RFC 1952) format.

If you want to decompress (inflate) 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.

For more information on GZIP, see IETF RFC 1952, "GZIP file format specification version 4.3".

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

See also
DeflateStream, ZlibStream

Constructor & Destructor Documentation

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

Create a GZipStream using the specified CompressionMode.

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().

This example shows how to use a GZipStream to compress data.

using (System.IO.Stream input = System.IO.File.OpenRead(fileToCompress))
{
using (var raw = System.IO.File.Create(outputFile))
{
using (Stream compressor = new GZipStream(raw, CompressionMode.Compress))
{
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);
}
}
}
}
Dim outputFile As String = (fileToCompress & ".compressed")
Using input As Stream = File.OpenRead(fileToCompress)
Using raw As FileStream = File.Create(outputFile)
Using compressor As Stream = New GZipStream(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 GZipStream will compress or decompress.
UniExtensions.Zlib.GZipStream.GZipStream ( System.IO.Stream  stream,
CompressionMode  mode,
CompressionLevel  level 
)
inline

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

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().

This example shows how to use a GZipStream to compress data.

using (System.IO.Stream input = System.IO.File.OpenRead(fileToCompress))
{
using (var raw = System.IO.File.Create(outputFile))
{
using (Stream compressor = new GZipStream(raw,
CompressionMode.Compress,
CompressionLevel.BEST_COMPRESSION))
{
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);
}
}
}
}
Dim outputFile As String = (fileToCompress & ".compressed")
Using input As Stream = File.OpenRead(fileToCompress)
Using raw As FileStream = File.Create(outputFile)
Using compressor As Stream = New GZipStream(raw, CompressionMode.Compress, CompressionLevel.BEST_COMPRESSION)
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 GZipStream will compress or decompress.
levelA tuning knob to trade speed for effectiveness.
UniExtensions.Zlib.GZipStream.GZipStream ( System.IO.Stream  stream,
CompressionMode  mode,
bool  leaveOpen 
)
inline

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.

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().

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.
modeIndicates whether the GZipStream will compress or decompress.
leaveOpentrue if the application would like the base stream to remain open after inflation/deflation.
UniExtensions.Zlib.GZipStream.GZipStream ( System.IO.Stream  stream,
CompressionMode  mode,
CompressionLevel  level,
bool  leaveOpen 
)
inline

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().

This example shows how to use a DeflateStream to compress data.

using (System.IO.Stream input = System.IO.File.OpenRead(fileToCompress))
{
using (var raw = System.IO.File.Create(outputFile))
{
using (Stream compressor = new GZipStream(raw, CompressionMode.Compress, CompressionLevel.BEST_COMPRESSION, 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);
}
}
}
}
Dim outputFile As String = (fileToCompress & ".compressed")
Using input As Stream = File.OpenRead(fileToCompress)
Using raw As FileStream = File.Create(outputFile)
Using compressor As Stream = New GZipStream(raw, CompressionMode.Compress, CompressionLevel.BEST_COMPRESSION, 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
End Using
Parameters
streamThe stream which will be read or written.
modeIndicates whether the GZipStream 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

override void UniExtensions.Zlib.GZipStream.Close ( )
inline

Close the stream.

This may or may not close the captive stream. See the ctor's with leaveOpen parameters for more information.

override void UniExtensions.Zlib.GZipStream.Flush ( )
inline

Flush the stream.

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

Read and decompress data from the source stream.

With a GZipStream, decompression is done through reading.

byte[] working = new byte[WORKING_BUFFER_SIZE];
int n= 1;
using (System.IO.Stream input = System.IO.File.OpenRead(_CompressedFile))
{
using (Stream decompressor= new Ionic.Zlib.GZipStream(input, CompressionMode.Decompress, true))
{
using (var output = System.IO.File.Create(_DecompressedFile))
{
while (n !=0)
{
n= decompressor.Read(working, 0, working.Length);
if (n > 0)
{
output.Write(working, 0, n);
}
}
}
}
}
Parameters
bufferThe buffer into which the decompressed 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 UniExtensions.Zlib.GZipStream.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!
override void UniExtensions.Zlib.GZipStream.SetLength ( long  value)
inline

Calling this method always throws a NotImplementedException.

Parameters
valuethis is irrelevant, since it will always throw!
override void UniExtensions.Zlib.GZipStream.Write ( byte[]  buffer,
int  offset,
int  count 
)
inline

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. If you wish to use the DeflateStream to decompress data while writing, you can create a GZipStream 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 GZipStream 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

String UniExtensions.Zlib.GZipStream.Comment

The Comment on the GZIP stream.

The GZIP format allows for each file to have an associated comment stored with the file. The comment is encoded with the ISO-8859-1 code page. To include a comment in a GZIP stream you create, set this Comment before calling Write() for the first time on the GZipStream. When using GZipStream to decompress, you can retrieve this comment after the first call to Read().

DateTime UniExtensions.Zlib.GZipStream.LastModified

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().

Property Documentation

int UniExtensions.Zlib.GZipStream.BufferSize
getset

Callers can set the buffer size of the working buffer with this property.

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. I don't know, I haven't tested it.

override bool UniExtensions.Zlib.GZipStream.CanRead
get

Indicates whether the stream can be read.

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

override bool UniExtensions.Zlib.GZipStream.CanSeek
get

Indicates whether the stream supports Seek operations.

Always returns false.

override bool UniExtensions.Zlib.GZipStream.CanWrite
get

Indicates whether the stream can be written.

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

int UniExtensions.Zlib.GZipStream.Crc32
get

The CRC on the GZIP stream.

This is used for internal error checking. You probably don't need to look at this property.

String UniExtensions.Zlib.GZipStream.FileName
getset

The FileName for the GZIP stream.

The GZIP format allows each file to have an associated filename, encoded in the ISO-8859-1 code page. When compressing data (through Write()), set this FileName before calling Write() the first time on the GZipStream. When decompressing (through Read()), you can retrieve this value any time after the first Read().

virtual int UniExtensions.Zlib.GZipStream.FlushMode
getset

This property sets the flush behavior on the stream. Sorry, though, not sure exactly how to describe all the various settings.

override long UniExtensions.Zlib.GZipStream.Length
get

Reading this property always throws a NotImplementedException.

override long UniExtensions.Zlib.GZipStream.Position
getset

Reading or Writing this property always throws a NotImplementedException.

virtual long UniExtensions.Zlib.GZipStream.TotalIn
get

Returns the total number of bytes input so far.

virtual long UniExtensions.Zlib.GZipStream.TotalOut
get

Returns the total number of bytes output so far.


The documentation for this class was generated from the following file: