Ionic Zip Library v1.9.1.6
GZipStream Constructor (stream, mode, level)
Reference ► Ionic.Zlib ► GZipStream ► GZipStream(Stream, CompressionMode, CompressionLevel)
Create a GZipStream using the specified CompressionMode and
the specified CompressionLevel.
Declaration Syntax
C# | Visual Basic | Visual C++ |
public GZipStream( Stream stream, CompressionMode mode, CompressionLevel level )
Public Sub New ( _ stream As Stream, _ mode As CompressionMode, _ level As CompressionLevel _ )
public: GZipStream( Stream^ stream, CompressionMode mode, CompressionLevel level )
Parameters
- stream (Stream)
- The stream to be read or written while deflating or inflating.
- mode (CompressionMode)
- Indicates whether the GZipStream will compress or decompress.
- level (CompressionLevel)
- A tuning knob to trade speed for effectiveness.
Remarks
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().
Examples
This example shows how to use a GZipStream to compress a file into a .gz file.
CopyC#
using (System.IO.Stream input = System.IO.File.OpenRead(fileToCompress)) { using (var raw = System.IO.File.Create(fileToCompress + ".gz")) { using (Stream compressor = new GZipStream(raw, CompressionMode.Compress, CompressionLevel.BestCompression)) { byte[] buffer = new byte[WORKING_BUFFER_SIZE]; int n; while ((n= input.Read(buffer, 0, buffer.Length)) != 0) { compressor.Write(buffer, 0, n); } } } }
CopyVB.NET
Using input As Stream = File.OpenRead(fileToCompress) Using raw As FileStream = File.Create(fileToCompress & ".gz") Using compressor As Stream = New GZipStream(raw, CompressionMode.Compress, CompressionLevel.BestCompression) 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