A class for compressing and decompressing streams using the Deflate algorithm.
More...
|
| 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...
|
|
override void | Close () |
| Close the stream. More...
|
|
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...
|
|
|
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...
|
|
A class for compressing and decompressing streams using the Deflate algorithm.
Data can be compressed or decompressed, and either of those can be through reading or writing. For more information on the Deflate algorithm, see IETF RFC 1951, "DEFLATE Compressed Data
Format Specification version 1.3."
This class is similar to ZlibStream, except that ZlibStream
adds the RFC1950 header bytes to a compressed stream when compressing, or expects the RFC1950 header bytes when decompressing. The DeflateStream
does not.
- See also
- DeflateStream, GZipStream
Create a DeflateStream using the specified CompressionMode and the specified CompressionLevel.
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 DeflateStream(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 DeflateStream(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
-
stream | The stream to be read or written while deflating or inflating. |
mode | Indicates whether the DeflateStream will compress or decompress. |
level | A tuning knob to trade speed for effectiveness. |
UniExtensions.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
-
stream | The stream which will be read or written. This is called the "captive" stream in other places in this documentation. |
mode | Indicates whether the DeflateStream will compress or decompress. |
leaveOpen | true if the application would like the stream to remain open after inflation/deflation. |
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.
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.
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))
{
{
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)
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
-
stream | The stream which will be read or written. |
mode | Indicates whether the DeflateStream will compress or decompress. |
leaveOpen | true if the application would like the stream to remain open after inflation/deflation. |
level | A tuning knob to trade speed for effectiveness. |
override int UniExtensions.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. 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.
A DeflateStream can be used for Read() or Write(), but not both.
- Parameters
-
buffer | The buffer into which the read data should be placed. |
offset | the offset within that data array to put the first byte read. |
count | the number of bytes to read. |
- Returns
- the number of bytes actually read
override void UniExtensions.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
-
buffer | The buffer holding data to write to the stream. |
offset | the offset within that data array to find the first byte to write. |
count | the number of bytes to write. |