CompressionStream Class

Definition

Provides methods and properties for compressing and decompressing streams by using the deflate algorithm.

public class CompressionStream : System.IO.Stream
type CompressionStream = class
    inherit Stream
Inheritance
CompressionStream

Remarks

The CompressionStream uses the Compression Framework to compress and decompress the data using the Streams API.

// sample zlib data 
static byte [] compressed_data = { 0xf3, 0x48, 0xcd, 0xc9, 0xc9, 0xe7, 0x02, 0x00 };

using (var backing = new MemoryStream (compressed_data)) // compress data to read
using (var decompressing = new CompressionStream (backing, CompressionMode.Decompress, CompressionAlgorithm.Zlib)) // create decompressin stream with the correct algorithm
using (var reader = new StreamReader (decompressing))
{
    // perform the required stream operations
    Console.WriteLine (reader.ReadLine ());
}
}

Constructors

CompressionStream(Stream, CompressionAlgorithm)

Initializes a new instance of the CompressionStream class by using the specified stream and algorithm.

CompressionStream(Stream, CompressionAlgorithm, Boolean)

Initializes a new instance of the CompressionStream class by using the specified stream and algorithm, and optionally leaves the stream open.

CompressionStream(Stream, CompressionMode, CompressionAlgorithm)

Initializes a new instance of the CompressionStream class by using the specified stream, algorithm, and compression mode.

CompressionStream(Stream, CompressionMode, CompressionAlgorithm, Boolean)

Initializes a new instance of the CompressionStream class by using the specified stream, algorithm, and compression mode, and optionally leaves the stream open.

Properties

BaseStream

A stream object that represents the underlying stream.

CanRead

Gets a value indicating whether the stream supports reading while decompressing a file.

CanSeek

Gets a value indicating whether the stream supports seeking.

CanWrite

Gets a value indicating whether the stream supports writing.

Length

Gets the length in bytes of the stream.

Position

This property is not supported and always throws a NotSupportedException.

Methods

BeginRead(Byte[], Int32, Int32, AsyncCallback, Object)

Begins an asynchronous read operation.

BeginWrite(Byte[], Int32, Int32, AsyncCallback, Object)

Begins an asynchronous write operation.

CopyToAsync(Stream, Int32, CancellationToken)

Asynchronously reads the bytes from the current stream and writes them to another stream.

Dispose(Boolean)

Releases the unmanaged resources used by the CompressionStream and optionally releases the managed resources.

EndRead(IAsyncResult)

Waits for the pending asynchronous read to complete.

EndWrite(IAsyncResult)

Ends an asynchronous write operation.

Flush()

Clears all buffers for this stream and causes any buffered data to be written to the underlying device.

FlushAsync(CancellationToken)

Asynchronously clears all buffers for this stream and causes any buffered data to be written to the underlying device.

Read(Byte[], Int32, Int32)

Reads a sequence of bytes from the current stream and advances the position within the stream by the number of bytes read.

Read(Span<Byte>)
ReadAsync(Byte[], Int32, Int32, CancellationToken)

Asynchronously reads a sequence of bytes from the current stream and advances the position within the stream by the number of bytes read.

ReadAsync(Memory<Byte>, CancellationToken)
ReadByte()

Reads a byte from the stream and advances the position within the stream by one byte, or returns -1 if at the end of the stream.

Seek(Int64, SeekOrigin)

This operation is not supported and always throws a NotSupportedException.

SetLength(Int64)

This operation is not supported and always throws a NotSupportedException.

Write(Byte[], Int32, Int32)

Writes compressed bytes to the underlying stream from the specified byte array.

Write(ReadOnlySpan<Byte>)
WriteAsync(Byte[], Int32, Int32, CancellationToken)

Asynchronously writes compressed bytes to the underlying stream from the specified byte array.

WriteAsync(ReadOnlyMemory<Byte>, CancellationToken)

Applies to