CompressionMode Enumeration


Specifies whether to compress or decompress the underlying stream.

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

Public Enumeration CompressionMode

Member nameDescription

Compresses the underlying stream.


Decompresses the underlying stream.

This enumeration is used with the GZipStream and DeflateStream classes.

The following code example uses the CompressionMode enumeration with the GZipStream class to compress and decompress a file.

Imports System.IO
Imports System.IO.Compression
Module Module1

    Private directoryPath As String = "c:\temp"
    Public Sub Main()
        Dim directorySelected As New DirectoryInfo(directoryPath)

        For Each fileToDecompress As FileInfo In directorySelected.GetFiles("*.gz")
    End Sub

    Public Sub Compress(directorySelected As DirectoryInfo)
        For Each fileToCompress As FileInfo In directorySelected.GetFiles()
            Using originalFileStream As FileStream = fileToCompress.OpenRead()
                If (File.GetAttributes(fileToCompress.FullName) And FileAttributes.Hidden) <> FileAttributes.Hidden And fileToCompress.Extension <> ".gz" Then
                    Using compressedFileStream As FileStream = File.Create(fileToCompress.FullName & ".gz")
                        Using compressionStream As New GZipStream(compressedFileStream, CompressionMode.Compress)

                        End Using
                    End Using
                    Dim info As New FileInfo(directoryPath & "\" & fileToCompress.Name & ".gz")
                    Console.WriteLine("Compressed {0} from {1} to {2} bytes.", fileToCompress.Name,
                                      fileToCompress.Length.ToString(), info.Length.ToString())

                End If
            End Using
    End Sub

    Private Sub Decompress(ByVal fileToDecompress As FileInfo)
        Using originalFileStream As FileStream = fileToDecompress.OpenRead()
            Dim currentFileName As String = fileToDecompress.FullName
            Dim newFileName = currentFileName.Remove(currentFileName.Length - fileToDecompress.Extension.Length)

            Using decompressedFileStream As FileStream = File.Create(newFileName)
                Using decompressionStream As GZipStream = New GZipStream(originalFileStream, CompressionMode.Decompress)
                    Console.WriteLine("Decompressed: {0}", fileToDecompress.Name)
                End Using
            End Using
        End Using
    End Sub
End Module

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