IdeaBlade DevForce 2010 Help Reference
GZipStream Constructor(Stream,CompressionMode,CompressionLevel,Boolean)
See Also  Example Send Feedback
IdeaBlade.Core Assembly > Ionic.Zlib Namespace > GZipStream Class > GZipStream Constructor : GZipStream Constructor(Stream,CompressionMode,CompressionLevel,Boolean)



stream
The stream which will be read or written.
mode
Indicates whether the GZipStream will compress or decompress.
level
A tuning knob to trade speed for effectiveness.
leaveOpen
true if the application would like the stream to remain open after inflation/deflation.
Create a GZipStream using the specified CompressionMode and the specified CompressionLevel, and explicitly specify whether the stream should be left open after Deflation or Inflation.

Syntax

Visual Basic (Declaration) 
Public Function New( _
   ByVal stream As Stream, _
   ByVal mode As CompressionMode, _
   ByVal level As CompressionLevel, _
   ByVal leaveOpen As Boolean _
)
Visual Basic (Usage)Copy Code
Dim stream As Stream
Dim mode As CompressionMode
Dim level As CompressionLevel
Dim leaveOpen As Boolean
 
Dim instance As New GZipStream(stream, mode, level, leaveOpen)
C# 
public GZipStream( 
   Stream stream,
   CompressionMode mode,
   CompressionLevel level,
   bool leaveOpen
)
C++/CLI 
public:
GZipStream( 
   Stream^ stream,
   CompressionMode mode,
   CompressionLevel level,
   bool leaveOpen
)

Parameters

stream
The stream which will be read or written.
mode
Indicates whether the GZipStream will compress or decompress.
level
A tuning knob to trade speed for effectiveness.
leaveOpen
true if the application would like the stream to remain open after inflation/deflation.

Example

This example shows how to use a GZipStream to compress data.
C#Copy Code
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, CompressionLevel.BestCompression, true))
        {
            byte[] buffer = new byte[WORKING_BUFFER_SIZE];
            int n;
            while ((n= input.Read(buffer, 0, buffer.Length)) != 0)
            {
                compressor.Write(buffer, 0, n);
            }
        }
    }
}
Visual BasicCopy Code
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, CompressionLevel.BestCompression, True)
        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

Remarks

This constructor allows the application to request that the captive stream remain open after the deflation or inflation occurs. By default, after Close() is called on the stream, the captive stream is also closed. In some cases this is not desired, for example if the stream is a memory stream that will be re-read after compressed data has been written to it. Specify true for the leaveOpen parameter to leave the stream open.

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().

Requirements

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

See Also

© 2013 All Rights Reserved.