using Xceed.Compression;
using Xceed.Compression.Formats;
namespace FileSystemDocumentationExamples.CompressionComponent
{
public class DecompressXceedCompressedStreamExample
{
public void MyMainMethod()
{
/* Because Xceed.Compression.Formats is an optional assembly not automatically
unlocked by Xceed Zip for .NET, we must explicitly set our license key to it
before use or else an exception will be thrown. */
Xceed.Compression.Formats.Licenser.LicenseKey = "<your Xceed Zip for .NET license key>";
}
public void Example( Stream compressedData )
{
/* 'compressedData' is assumed to contain the compressed data produced by XceedCompressedStream
This can be any Stream object from any source (file, memory, network, whatever) as
long as data can be read from it. No 'seek' or 'write' capabilities are needed.
In the special case where 'compressedData' is the same Stream object that has been
used as the destination stream for compression, the stream's position will need to be
changed to the start of the compressed data. Otherwise, no compressed data will be
found since the stream will be positioned at the end of the compressed data. */
/* With XceedCompressedStream, the compression method of the compressed data is
contained as part of the compressed stream. This is convenient as we don't need
to remember the value. */
// Optional: If the data was encrypted, supply the password
string password = String.Empty;
password = "password";
// Create a XceedCompressedStream that wraps around our compressed source data
using( XceedCompressedStream xceedCompressedStream = new XceedCompressedStream( compressedData, password ) )
{
/* The XceedCompressedStream automatically closes the destination
stream. So there is no need to declare the 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 XceedCompressedStream,
set its Transient property to true. */
// Optional: Prevent XceedCompressedStream from closing 'destinationStream' automatically
//xceedCompressedStream.Transient = true;
byte[] uncompressedData = null;
/* Setup a destination stream. This can be any Stream object for any destination as
long as data can be written to it. No 'seek' or 'read' capabilities are needed. */
using( MemoryStream destinationStream = new MemoryStream() )
{
int bytesRead;
// Setup a 32K buffer
byte[] buffer = new byte[ 32 * 1024 ];
// Read from the source stream until there is no more data, this will decompress the data
while( ( bytesRead = xceedCompressedStream.Read( buffer, 0, buffer.Length ) ) > 0 )
{
// Compress the data by writing into the compressed stream
// Compressed data will be written into its InnerStream, in our case, 'destinationStream'
destinationStream.Write( buffer, 0, bytesRead );
}
/* Optional: The MemoryStream's compressed data can be copied to a byte array, you can use
MemoryStream.ToArray(). The method works even when the memory stream has been closed. */
uncompressedData = destinationStream.ToArray();
}
}
}
}
}
Imports Xceed.Compression
Imports Xceed.Compression.Formats
Namespace FileSystemDocumentationExamples.CompressionComponent
Public Class DecompressXceedCompressedStreamExample
Public Sub MyMainMethod()
' Because Xceed.Compression.Formats is an optional assembly not automatically
' unlocked by Xceed Zip for .NET, we must explicitly set our license key to it
' before use or else an exception will be thrown.
Xceed.Compression.Formats.Licenser.LicenseKey = "<your Xceed Zip for .NET license key>"
End Sub
Public Sub Example(ByVal compressedData As Stream)
' 'compressedData' is assumed to contain the compressed data produced by XceedCompressedStream
' This can be any Stream object from any source (file, memory, network, whatever) as
' long as data can be read from it. No 'seek' or 'write' capabilities are needed.
'
' In the special case where 'compressedData' is the same Stream object that has been
' used as the destination stream for compression, the stream's position will need to be
' changed to the start of the compressed data. Otherwise, no compressed data will be
' found since the stream will be positioned at the end of the compressed data.
' With XceedCompressedStream, the compression method of the compressed data is
' contained as part of the compressed stream. This is convenient as we don't need
' to remember the value.
' Optional: If the data was encrypted, supply the password
Dim password As String = String.Empty
password = "password"
' Create a XceedCompressedStream that wraps around our compressed source data
Using xceedCompressedStream As New XceedCompressedStream(compressedData, password)
' The XceedCompressedStream automatically closes the destination
' stream. So there is no need to declare the 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 XceedCompressedStream,
' set its Transient property to true.
' Optional: Prevent XceedCompressedStream from closing 'destinationStream' automatically
'xceedCompressedStream.Transient = true;
Dim uncompressedData() As Byte = Nothing
' Setup a destination stream. This can be any Stream object for any destination as
' long as data can be written to it. No 'seek' or 'read' capabilities are needed.
Using destinationStream As New MemoryStream()
Dim bytesRead As Integer
' Setup a 32K buffer
Dim buffer(32 * 1024 - 1) As Byte
' Read from the source stream until there is no more data, this will decompress the data
bytesRead = xceedCompressedStream.Read(buffer, 0, buffer.Length)
Do While bytesRead > 0
' Compress the data by writing into the compressed stream
' Compressed data will be written into its InnerStream, in our case, 'destinationStream'
destinationStream.Write(buffer, 0, bytesRead)
bytesRead = xceedCompressedStream.Read(buffer, 0, buffer.Length)
Loop
' Optional: The MemoryStream's compressed data can be copied to a byte array, you can use
' MemoryStream.ToArray(). The method works even when the memory stream has been closed.
uncompressedData = destinationStream.ToArray()
End Using
End Using
End Sub
End Class
End Namespace