This topic demonstrates how to decompress a GZip compressed byte array using the static Decompress method of the XceedCompressedStream class.
For the purposes of this example we used the Decompress method of XceedCompressedStream class; however, we could have also used the Decompress method of the GZipCompressedStream or the ZLibCompressedStream classes.
Basic steps
To decompress a compressed byte array, the following steps must be taken:
Retrieve a byte array containing the compressed data. For the purposes of this example, our compressed data is the data compressed using the Compress example.
Decompress 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 Decompress method of the GZipCompressedStream class.
using System.IO;
using System.Text;
using Xceed.Compression;
using Xceed.Compression.Formats;
namespace FileSystemDocumentationExamples.CompressionComponent
{
publicclass DecompressArrayXceedCompressedStreamExample
{
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( byte[] compressedData )
{
/* 'compressedData' is assumed to contain the compressed data created by XceedCompressedStream.Compress() */// Optional: If the data was encrypted, supply the password
string password = String.Empty;
password = "password";
// Optional: Data can be selected for decompression from the source array at a specific offset and for a specific size
int offset = 0;
int count = compressedData.Length;
// Decompress the data
byte[] uncompressedData = XceedCompressedStream.Decompress( compressedData, offset, count, password );
/* In our compress example, we generated the source data from a string. At this point,
we can interpret the uncompressed data as a string. */string sourceData = Encoding.UTF8.GetString( uncompressedData );
}
}
}
Imports System.IO
Imports System.Text
Imports Xceed.Compression
Imports Xceed.Compression.Formats
Namespace FileSystemDocumentationExamples.CompressionComponent
PublicClass DecompressArrayXceedCompressedStreamExample
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(ByVal compressedData() AsByte)
' 'compressedData' is assumed to contain the compressed data created by XceedCompressedStream.Compress()
' Optional: If the data was encrypted, supply the password
Dim password AsString = String.Empty
password = "password"' Optional: Data can be selected for decompression from the source array at a specific offset and for a specific size
Dim offset AsInteger = 0
Dim count AsInteger = compressedData.Length
' Decompress the data
Dim uncompressedData() AsByte = XceedCompressedStream.Decompress(compressedData, offset, count, password)
' In our compress example, we generated the source data from a string. At this point,
' we can interpret the uncompressed data as a string.
Dim sourceData AsString = Encoding.UTF8.GetString(uncompressedData)
End SubEnd ClassEnd Namespace
Things you should consider
The main questions you should ask yourself when decompressing data read from a stream are:
Do you want to decompress byte arrays compressed using the Xceed or GZip formatted compressed streams? Use the static Decompress method of the GZipCompressedStream or ZLibCompressedStream classes instead of the XceedCompressedStream's.
Do you want to decompress a byte array that was compressed without a compression format (raw data)? Use the Decompress method of the QuickCompression class.
Do you want to decompress data as it is read from a stream? Use the Read method of the desired compression format class.
Do you need to convert the resulting decompressed byte array to a string? Use the GetString method of the .NET Framework's System.Text.Encoding class.