This topic demonstrates how to compress a byte array using the static Compress method of the XceedCompressedStream class.
For the purposes of this example we used the Compress method of XceedCompressedStream class; however, we could have also used the Compress method of the GZipCompressedStream or the ZLibCompressedStream classes.
Basic steps
To compress a byte array, the following steps must be taken:
Retrieve a byte array containing the data to compress. For the purposes of this example, we will convert a string to a byte array using the System.Text.Encoding.GetBytes() method.
Compress the data. The resulting compressed data will be returned as a byte array.
Example
The following example demonstrates how to compress an array of bytes using the static Compress method of the XceedCompressedStream class.
using System.IO;
using System.Text;
using Xceed.Compression;
using Xceed.Compression.Formats;
namespace FileSystemDocumentationExamples.CompressionComponent
{
publicclass CompressArrayXceedCompressedStreamExample
{
publicvoid 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>";
}
publicvoid Example()
{
// Generate a byte array from a string
byte[] data = Encoding.UTF8.GetBytes( "This is the data to compress compress compress" );
/* Optional: the compression method and level can be specified. */
CompressionMethod compressionMethod = CompressionMethod.Deflated;
CompressionLevel compressionLevel = CompressionLevel.Normal;
string password = String.Empty;
// Optional: the data will be encrypted if a non-empty password is specified
//password = "password";
// Optional: Data can be selected for compression at a specific offset and for a specific size
int offset = 0;
int count = data.Length;
// Compress the data
byte[] compressedData = XceedCompressedStream.Compress( data, offset, count, compressionMethod, compressionLevel, password );
}
}
}
Imports System.IO
Imports System.Text
Imports Xceed.Compression
Imports Xceed.Compression.Formats
Namespace FileSystemDocumentationExamples.CompressionComponent
PublicClass CompressArrayXceedCompressedStreamExample
PublicSub 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 SubPublicSub Example()
' Generate a byte array from a string
Dim data() AsByte = Encoding.UTF8.GetBytes("This is the data to compress compress compress")
' Optional: the compression method and level can be specified.
Dim compressionMethod As CompressionMethod = CompressionMethod.Deflated
Dim compressionLevel As CompressionLevel = CompressionLevel.Normal
Dim password AsString = String.Empty
' Optional: the data will be encrypted if a non-empty password is specified
'password = "password";
' Optional: Data can be selected for compression at a specific offset and for a specific size
Dim offset AsInteger = 0
Dim count AsInteger = data.Length
' Compress the data
Dim compressedData() AsByte = XceedCompressedStream.Compress(data, offset, count, compressionMethod, compressionLevel, password)
End SubEnd ClassEnd Namespace
Things you should consider
The main questions you should ask yourself when compressing data read from a stream are:
Do you want to compress the byte array using the Xceed or ZLib compression formats? Use the static Compress method of the GZipCompressedStream or ZLibCompressedStream classes instead of the XceedCompressedStream's.
Do you want to compress the byte array but without a compression format (header/footer)? Use the Compress method of the QuickCompression class.
Do you want to compress data as it is written to a stream? Use the Write method of the desired compression format class.
Do you want to compress a string? Convert it to a byte array before passing it to the Compress method using the GetBytes method of the .NET Framework's System.Text.Encoding class.
Do you need to convert the resulting compressed byte array to a string? Use the GetString method of the .NET Framework's System.Text.Encoding class.