Export (0) Print
Expand All

GZipStream Constructor (Stream, CompressionLevel, Boolean)

.NET Framework 4.5

Initializes a new instance of the GZipStream class by using the specified stream and compression level, and optionally leaves the stream open.

Namespace:  System.IO.Compression
Assembly:  System (in System.dll)

'Declaration
Public Sub New ( _
	stream As Stream, _
	compressionLevel As CompressionLevel, _
	leaveOpen As Boolean _
)

Parameters

stream
Type: System.IO.Stream

The stream to compress.

compressionLevel
Type: System.IO.Compression.CompressionLevel

One of the enumeration values that indicates whether to emphasize speed or compression efficiency when compressing the stream.

leaveOpen
Type: System.Boolean

true to leave the stream object open after disposing the GZipStream object; otherwise, false.

ExceptionCondition
ArgumentNullException

stream is Nothing.

ArgumentException

The stream does not support write operations such as compression. (The CanWrite property on the stream object is false.)

You use this constructor when you want to specify whether compression efficiency or speed is more important for an instance of the GZipStream class, and whether to leave the stream object open after disposing the GZipStream object.

This constructor overload uses the compression mode Compress. To set the compression mode to another value, use the GZipStream(Stream, CompressionMode) or GZipStream(Stream, CompressionMode, Boolean) overload.

The following example shows how to set the compression level when creating a GZipStream object and how to leave the stream open.

Imports System.IO
Imports System.IO.Compression

Module Module1

    Sub Main()
        Dim directoryPath As String = "c:\users\public\reports" 
        Dim directorySelected As DirectoryInfo = New DirectoryInfo(directoryPath)

        For Each fileToCompress As FileInfo In directorySelected.EnumerateFiles()
            Compress(fileToCompress)
        Next 
    End Sub 

    Public Sub Compress(fileToCompress As FileInfo)
        Using originalFileStream As FileStream = fileToCompress.OpenRead()
            If ((File.GetAttributes(fileToCompress.FullName) & FileAttributes.Hidden) <> FileAttributes.Hidden And fileToCompress.Extension <> ".gz") Then 
                Using compressedFileStream As FileStream = File.Create(fileToCompress.FullName + ".gz")
                    Using compressionStream As GZipStream = New GZipStream(compressedFileStream, CompressionLevel.Fastest, True)
                        originalFileStream.CopyTo(compressionStream)
                    End Using
                    Console.WriteLine(String.Format("file compressed to {0} bytes", compressedFileStream.Length))
                End Using 
            End If 
        End Using 
    End Sub 

End Module

.NET Framework

Supported in: 4.5

.NET for Windows Store apps

Supported in: Windows 8

.NET for Windows Phone apps

Supported in: Windows Phone 8.1

Windows Phone 8.1, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft