Read Method (CompressedStream)
Reads and decompresses a sequence of bytes from the inner stream.
Parameters
- buffer
- An array of bytes. A maximum of bytes are stored in this array.
- offset
- The byte offset in at which to begin storing the data decompressed from the inner stream.
- count
- The maximum number of bytes to be stored in .
Return Value
The total number of bytes read into the . This may be less than the number of bytes requested if that many bytes aren't currently available, or zero if the end of the stream has been reached. Applications must check the return value to prevent data corruption.
The following example demonstrates how to read compressed data from a MemoryStream and decompress it to a FileStream using the CompressedStream class.
Imports System.IO
Imports Xceed.Compression
' If you do not want the inner stream to be closed by the CompressedStream
' then set the CompressedStream's Transient property to true.
'
' The compressed data was compressed using the Compress example
Dim sourceStream = New MemoryStream( compressedData )
Dim compStream As New CompressedStream( sourceStream )
Dim destinationStream As New FileStream("d:\data.txt", FileMode.OpenOrCreate)
' 32K at at time.
Dim buffer(32768) As Byte
Dim bytesRead As Integer = 0
' Loop until we have nothing more to read from the source stream
Do
bytesRead = compStream.Read(buffer, 0, buffer.Length)
If bytesRead > 0 Then
destinationStream.Write(buffer, 0, bytesRead)
End If
Loop Until bytesRead = 0
' Close the destination stream and the CompressedStream.
'
' Because the CompressedStream will automatically close the source
' memory stream, there is no need to call Close once we are done with the stream.
destinationStream.Close()
compStream.Close()
using System.IO;
using Xceed.Compression;
// Because the CompressedStream will automatically close the source
// memory stream, there is no need to declare the memory stream within a using
// statement or to call Close once we are done with the stream.
//
// If you do not want the inner stream to be closed by the CompressedStream
// then set the CompressedStream's Transient property to true.
//
// The compressed data was compressed using the Compress example
MemoryStream sourceStream = new MemoryStream( compressedData );
using( CompressedStream compStream = new CompressedStream( sourceStream ) )
{
using( FileStream destinationStream = new FileStream( @"d:\data.txt", FileMode.OpenOrCreate ) )
{
// 32K at at time.
byte[] buffer = new byte[ 32768 ];
int bytesRead = 0;
// Loop until we have nothing more to read from the source stream.
while( ( bytesRead = compStream.Read( buffer, 0, buffer.Length ) ) > 0 )
{
destinationStream.Write( buffer, 0, bytesRead );
}
}
}
.NET: net5.0, net5.0-windows, net6.0, net6.0-macos, net6.0-windows, net7.0, net7.0-macos, net7.0-windows, net8.0, net8.0-browser, net8.0-macos, net8.0-windows, net9.0, net9.0-browser, net9.0-macos, net9.0-windows, net10.0, net10.0-browser, net10.0-macos, net10.0-windows.
.NET Framework: net40, net403, net45, net451, net452, net46, net461, net462, net463, net47, net471, net472, net48, net481.