GZipStream Constructor (Stream, CompressionLevel)

.NET Framework (current version)

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

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

Public Sub New (
	stream As Stream,
	compressionLevel As CompressionLevel


Type: System.IO.Stream

The stream to write the compressed data to.

Type: System.IO.Compression.CompressionLevel

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

Exception Condition

stream is null.


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.

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.

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()
    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)
                    End Using
                End Using
            End If
        End Using
    End Sub

End Module

Universal Windows Platform
Available since 8
.NET Framework
Available since 4.5
Portable Class Library
Supported in: portable .NET platforms
Windows Phone
Available since 8.1
Return to top