Ionic Zip Library v1.9.1.6
GZipStream Constructor (stream, mode)
Reference ► Ionic.Zlib ► GZipStream ► GZipStream(Stream, CompressionMode)
Create a GZipStream using the specified CompressionMode.

C# | Visual Basic | Visual C++ |
public GZipStream( Stream stream, CompressionMode mode )
Public Sub New ( _ stream As Stream, _ mode As CompressionMode _ )
public: GZipStream( Stream^ stream, CompressionMode mode )

- stream (Stream)
- The stream which will be read or written.
- mode (CompressionMode)
- Indicates whether the GZipStream will compress or decompress.

When mode is CompressionMode.Compress, the GZipStream will use the default compression level.
As noted in the class documentation, the CompressionMode (Compress or Decompress) also establishes the "direction" of the stream. A GZipStream with CompressionMode.Compress works only through Write(). A GZipStream with CompressionMode.Decompress works only through Read().

This example shows how to use a GZipStream to compress data.
CopyC#
CopyVB.NET

using (System.IO.Stream input = System.IO.File.OpenRead(fileToCompress)) { using (var raw = System.IO.File.Create(outputFile)) { using (Stream compressor = new GZipStream(raw, CompressionMode.Compress)) { byte[] buffer = new byte[WORKING_BUFFER_SIZE]; int n; while ((n= input.Read(buffer, 0, buffer.Length)) != 0) { compressor.Write(buffer, 0, n); } } } }

Dim outputFile As String = (fileToCompress & ".compressed") Using input As Stream = File.OpenRead(fileToCompress) Using raw As FileStream = File.Create(outputFile) Using compressor As Stream = New GZipStream(raw, CompressionMode.Compress) Dim buffer As Byte() = New Byte(4096) {} Dim n As Integer = -1 Do While (n <> 0) If (n > 0) Then compressor.Write(buffer, 0, n) End If n = input.Read(buffer, 0, buffer.Length) Loop End Using End Using End Using

This example shows how to use a GZipStream to uncompress a file.
CopyC#
CopyVB.NET

private void GunZipFile(string filename) { if (!filename.EndsWith(".gz)) throw new ArgumentException("filename"); var DecompressedFile = filename.Substring(0,filename.Length-3); byte[] working = new byte[WORKING_BUFFER_SIZE]; int n= 1; using (System.IO.Stream input = System.IO.File.OpenRead(filename)) { using (Stream decompressor= new Ionic.Zlib.GZipStream(input, CompressionMode.Decompress, true)) { using (var output = System.IO.File.Create(DecompressedFile)) { while (n !=0) { n= decompressor.Read(working, 0, working.Length); if (n > 0) { output.Write(working, 0, n); } } } } } }

Private Sub GunZipFile(ByVal filename as String) If Not (filename.EndsWith(".gz)) Then Throw New ArgumentException("filename") End If Dim DecompressedFile as String = filename.Substring(0,filename.Length-3) Dim working(WORKING_BUFFER_SIZE) as Byte Dim n As Integer = 1 Using input As Stream = File.OpenRead(filename) Using decompressor As Stream = new Ionic.Zlib.GZipStream(input, CompressionMode.Decompress, True) Using output As Stream = File.Create(UncompressedFile) Do n= decompressor.Read(working, 0, working.Length) If n > 0 Then output.Write(working, 0, n) End IF Loop While (n > 0) End Using End Using End Using End Sub