This topic demonstrates how to create a GZip file (. gz). Although Xceed's streaming compression component supports GZip files, it does not support tar files.
To create a GZip file, the following steps must be taken:
Create the source stream from which the data will be read. For the purposes of this example, our source stream will be a FileStream.
Create the destination FileStream to which the GZip compressed data will be written. We will give the destination file a . gz extension to stay compatible with popular GZip softwares.
Read the uncompressed data from the source stream.
Write the data to the compressed stream object. Writing to the object compresses the data and, in turn, writes the compressed data to the destination stream.
Example
The following example demonstrates how to create a GZip file from a source FileStream.
using Xceed.Compression;
using Xceed.Compression.Formats;
namespace FileSystemDocumentationExamples.CompressionComponent
{
publicclass CompressGZipCompressedStreamExample
{
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()
{
/* Setup a destination data 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. */
FileStream destinationStream = new FileStream( @"D:\Data.gz", FileMode.Create, FileAccess.Write, FileShare.None );
/* Setup a source data stream. This can be any Stream object from any source as
long as data can be read from it. No 'seek' or 'write' capabilities are needed. */using( FileStream sourceStream = new FileStream( @"D:\Data.dat", FileMode.Open, FileAccess.Read, FileShare.Read ) )
{
/* Optional: the compression level can be specified. */
CompressionLevel compressionLevel = CompressionLevel.Normal;
// Create a GZip compressed stream that wraps around our destination stream
using( GZipCompressedStream gzipCompressedStream = new GZipCompressedStream( destinationStream, compressionLevel ) )
{
/* The GZipCompressedStream 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 GZipCompressedStream,
set its Transient property to true. */// Optional: Prevent XceedCompressedStream from closing 'destinationStream' automatically
//gzipCompressedStream.Transient = true;
int bytesRead;
// Setup a 32K buffer
byte[] buffer = newbyte[ 32 * 1024 ];
// Read from the source stream until there is no more data
while( ( bytesRead = sourceStream.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'
gzipCompressedStream.Write( buffer, 0, bytesRead );
}
}
}
}
}
}
Imports Xceed.Compression
Imports Xceed.Compression.Formats
Namespace FileSystemDocumentationExamples.CompressionComponent
PublicClass CompressGZipCompressedStreamExample
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()
' Setup a destination data 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.
Dim destinationStream AsNew FileStream("D:\Data.gz", FileMode.Create, FileAccess.Write, FileShare.None)
' Setup a source data stream. This can be any Stream object from any source as
' long as data can be read from it. No 'seek' or 'write' capabilities are needed.
Using sourceStream AsNew FileStream("D:\Data.dat", FileMode.Open, FileAccess.Read, FileShare.Read)
' Optional: the compression level can be specified.
Dim compressionLevel As CompressionLevel = CompressionLevel.Normal
' Create a GZip compressed stream that wraps around our destination stream
Using gzipCompressedStream AsNew GZipCompressedStream(destinationStream, compressionLevel)
' The GZipCompressedStream 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 GZipCompressedStream,
' set its Transient property to true.
' Optional: Prevent XceedCompressedStream from closing 'destinationStream' automatically
'gzipCompressedStream.Transient = true;
Dim bytesRead AsInteger' Setup a 32K buffer
Dim buffer(32 * 1024 - 1) AsByte' Read from the source stream until there is no more data
bytesRead = sourceStream.Read(buffer, 0, buffer.Length)
DoWhile bytesRead > 0
' Compress the data by writing into the compressed stream
' Compressed data will be written into its InnerStream, in our case, 'destinationStream'
gzipCompressedStream.Write(buffer, 0, bytesRead)
bytesRead = sourceStream.Read(buffer, 0, buffer.Length)
LoopEndUsingEndUsingEnd SubEnd ClassEnd Namespace