The following example demonstrates how to decompress a GZip file and write the decompressed contents to a FileStream.
using System; using System.IO; using Xceed.Compression; using Xceed.Compression.Formats; namespace DocumentationExamples.GZip { class GZipDecompressExample { static void Example() { // Open a file that contains GZip data for reading FileStream source = new FileStream( "SomeGZipFile.dat.gz", FileMode.Open ); /* The GZip format has a header that contains meta data like a filename, modified date, etc * In this example, we will try to use it. We will use a flavor of the constructor that takes * a 'readHeader' parameter and pass 'true' so that the constructor reads the GZip * header immediately so we can use the values in it to build our output filename. */ // Wrap the source stream with a GZipCompressedStream using( GZipCompressedStream gzip = new GZipCompressedStream( source, CompressionLevel.Normal, true ) ) { /* Because we used the value 'true' in the 'readHeader' parameter, GZipCompressedStream has read * the meta data header. We will now use the values to build a filename for output */ string filename = gzip.Header.FileName; /* If the header does not contain a filename, you will need to provide one ourselves */ if( String.IsNullOrEmpty( filename ) ) { filename = "SomeGZipFile.dat"; } // Create the destination file using( FileStream destination = new FileStream( filename, FileMode.Create ) ) { // Create a 32k buffer byte[] buffer = new byte[ 32 * 1024 ]; int read = 0; // Read from the GZip compressed stream while there is data to read while( ( read = gzip.Read( buffer, 0, buffer.Length ) ) > 0 ) { // Write the uncompressed data to the destination destination.Write( buffer, 0, read ); } } } } } }
Imports Microsoft.VisualBasic Imports System Imports System.IO Imports Xceed.Compression Imports Xceed.Compression.Formats Namespace DocumentationExamples.GZip Friend Class GZipDecompressExample Private Shared Sub Example() ' Open a file that contains GZip data for reading Dim source As New FileStream("SomeGZipFile.dat.gz", FileMode.Open) ' The GZip format has a header that contains meta data like a filename, modified date, etc ' * In this example, we will try to use it. We will use a flavor of the constructor that takes ' * a 'readHeader' parameter and pass 'true' so that the constructor reads the GZip ' * header immediately so we can use the values in it to build our output filename. ' Wrap the source stream with a GZipCompressedStream Using gzip As New GZipCompressedStream(source, CompressionLevel.Normal, True) ' Because we used the value 'true' in the 'readHeader' parameter, GZipCompressedStream has read ' * the meta data header. We will now use the values to build a filename for output Dim filename As String = gzip.Header.FileName ' If the header does not contain a filename, you will need to provide one ourselves If String.IsNullOrEmpty(filename) Then filename = "SomeGZipFile.dat" End If ' Create the destination file Using destination As New FileStream(filename, FileMode.Create) ' Create a 32k buffer Dim buffer(32 * 1024 - 1) As Byte Dim read As Integer = 0 ' Read from the GZip compressed stream while there is data to read read = gzip.Read(buffer, 0, buffer.Length) Do While read > 0 ' Write the uncompressed data to the destination destination.Write(buffer, 0, read) read = gzip.Read(buffer, 0, buffer.Length) Loop End Using End Using End Sub End Class End Namespace