GZipStream
using the specified CompressionMode
.
Visual Basic (Declaration) | |
---|---|
Public Function New( _ ByVal stream As Stream, _ ByVal mode As CompressionMode _ ) |
Visual Basic (Usage) | ![]() |
---|---|
Dim stream As Stream Dim mode As CompressionMode Dim instance As New GZipStream(stream, mode) |
C# | |
---|---|
public GZipStream( Stream stream, CompressionMode mode ) |
C++/CLI | |
---|---|
public: GZipStream( Stream^ stream, CompressionMode mode ) |
Parameters
- stream
- The stream which will be read or written.
- mode
- Indicates whether the GZipStream will compress or decompress.
This example shows how to use a GZipStream to compress data. This example shows how to use a GZipStream to uncompress a file.
C# | ![]() |
---|---|
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); } } } } |
Visual Basic | ![]() |
---|---|
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 |
C# | ![]() |
---|---|
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); } } } } } } |
Visual Basic | ![]() |
---|---|
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 |
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()
.
Target Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, Windows Vista, Windows Server 2008 family