The following example demonstrates how to compress an array of bytes using the static Compress method of the QuickCompression class.
VB.NET
Copy Code
Imports Xceed.Compression
Dim data() As Byte = System.Text.Encoding.Default.GetBytes( "This is the data to compress compress compress" )
Dim compressedData() As Byte = QuickCompression.Compress( data )
C#
Copy Code
using Xceed.Compression;
byte[] data = System.Text.Encoding.Default.GetBytes( "This is the data to compress compress compress" );
byte[] compressedData = QuickCompression.Compress( data );
FileSystem Example
The following example demonstrates how to compress an array of bytes using the FileSystem object model.
VB.NET
Copy Code
Imports Xceed.Compression Imports System.IO
Dim data() As Byte = System.Text.Encoding.Default.GetBytes("This is the data to compress compress compress") Dim sourceStream As New MemoryStream(data)
' If you do not want the inner stream to be closed by the CompressedStream ' then set the CompressedStream's Transient property to true.
Dim destinationStream As New MemoryStream() Dim compStream As New CompressedStream(destinationStream)
'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 = sourceStream.Read(buffer, 0, buffer.Length)
If bytesRead > 0 Then compStream.Write(buffer, 0, bytesRead) End If Loop Until bytesRead = 0
' Close the source stream and the CompressedStream. ' ' Because the CompressedStream will automatically close the destination ' memory stream, there is no need to call Close once we are done with the stream. sourceStream.Close() compStream.Close()
' To get access to the MemoryStream's compressed data, you can use: Dim compressedData() As Byte = destinationStream.ToArray() ' ToArray() works even when the memory stream is closed.
C#
Copy Code
using Xceed.Compression; using System.IO;
byte[] data = System.Text.Encoding.Default.GetBytes( "This is the data to compress compress compress");
using( MemoryStream sourceStream = new MemoryStream( data ) ) { // Because the CompressedStream will automatically close the destination // 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. MemoryStream destinationStream = new MemoryStream();
using( CompressedStream compStream = new CompressedStream( destinationStream ) ) { // 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 = sourceStream.Read( buffer, 0, buffer.Length ) ) > 0 ) { compStream.Write( buffer, 0, bytesRead ); } }
// To get access to the MemoryStream's compressed data, you can use: byte[] compressedData = destinationStream.ToArray(); // ToArray() works even when the memory stream is closed. }