// Display the decompressed data MessageBox.Show( System.Text.Encoding.Default.GetString( decompressedData );
FileSystem Example
The following example demonstrates how to decompress an array of bytes using the FileSystem object model.
VB.NET
Copy Code
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 MemoryStream()
' 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()
' To get access to the MemoryStream's decompressed data, you can use: byte[] decompressedData = destinationStream.ToArray(); ' ToArray() works even when the memory stream is closed.
C#
Copy Code
using System.IO; using System.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( MemoryStream destinationStream = new MemoryStream() ) { // 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 ); }
// To get access to the MemoryStream's decompressed data, you can use: byte[] decompressedData = destinationStream.ToArray(); // ToArray() works even when the memory stream is closed. } }