Export (0) Print
Expand All

DeflateStream.Read Method

Reads a number of decompressed bytes into the specified byte array.

Namespace:  System.IO.Compression
Assembly:  System (in System.dll)

Public Overrides Function Read ( _
	array As Byte(), _
	offset As Integer, _
	count As Integer _
) As Integer


Type: System.Byte()

The array to store decompressed bytes.

Type: System.Int32

The byte offset in array at which the read bytes will be placed.

Type: System.Int32

The maximum number of decompressed bytes to read.

Return Value

Type: System.Int32
The number of bytes that were read into the byte array.


array is Nothing.


The CompressionMode value was Compress when the object was created.

- or -

The underlying stream does not support reading.


offset or count is less than zero.


array length minus the index starting point is less than count.


The data is in an invalid format.


The stream is closed.

The following example shows how to compress and decompress bytes by using the Read and Write methods.

Imports System.IO
Imports System.IO.Compression
Imports System.Text

Module Module1

    Sub Main()
        Dim uniEncode As UnicodeEncoding = New UnicodeEncoding()

        Dim bytesToCompress = uniEncode.GetBytes("example text to compress and decompress")
        Console.WriteLine("starting with: " + uniEncode.GetString(bytesToCompress))

        Using fileToCompress As FileStream = File.Create("examplefile.gz")
            Using compressionStream As DeflateStream = New DeflateStream(fileToCompress, CompressionMode.Compress)
                compressionStream.Write(bytesToCompress, 0, bytesToCompress.Length)
            End Using 
        End Using 

        Dim decompressedBytes(bytesToCompress.Length - 1) As Byte 
        Using fileToDecompress As FileStream = File.Open("examplefile.gz", FileMode.Open)
            Using decompressionStream As DeflateStream = New DeflateStream(fileToDecompress, CompressionMode.Decompress)
                decompressionStream.Read(decompressedBytes, 0, bytesToCompress.Length)
            End Using 
        End Using

        Console.WriteLine("ending with: " + uniEncode.GetString(decompressedBytes))
    End Sub 
End Module

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

.NET for Windows Phone apps

Supported in: Windows Phone 8.1

Windows Phone 8.1, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

© 2014 Microsoft