GZipStream GZipStream GZipStream Constructors

Definition

Overloads

GZipStream(Stream, CompressionLevel) GZipStream(Stream, CompressionLevel) GZipStream(Stream, CompressionLevel)

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

GZipStream(Stream, CompressionMode) GZipStream(Stream, CompressionMode) GZipStream(Stream, CompressionMode)

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

GZipStream(Stream, CompressionLevel, Boolean) GZipStream(Stream, CompressionLevel, Boolean) GZipStream(Stream, CompressionLevel, Boolean)

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

GZipStream(Stream, CompressionMode, Boolean) GZipStream(Stream, CompressionMode, Boolean) GZipStream(Stream, CompressionMode, Boolean)

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

GZipStream(Stream, CompressionLevel) GZipStream(Stream, CompressionLevel) GZipStream(Stream, CompressionLevel)

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

public:
 GZipStream(System::IO::Stream ^ stream, System::IO::Compression::CompressionLevel compressionLevel);
public GZipStream (System.IO.Stream stream, System.IO.Compression.CompressionLevel compressionLevel);
new System.IO.Compression.GZipStream : System.IO.Stream * System.IO.Compression.CompressionLevel -> System.IO.Compression.GZipStream

Parameters

stream
Stream Stream Stream

The stream to compress.

compressionLevel
CompressionLevel CompressionLevel CompressionLevel

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

Exceptions

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

Examples

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

using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            string directoryPath = @"c:\users\public\reports";
            DirectoryInfo directorySelected = new DirectoryInfo(directoryPath);

            foreach (FileInfo fileToCompress in directorySelected.EnumerateFiles())
            {
                Compress(fileToCompress);
            }
        }

        public static void Compress(FileInfo fileToCompress)
        {
            using (FileStream originalFileStream = fileToCompress.OpenRead())
            {
                if ((File.GetAttributes(fileToCompress.FullName) & FileAttributes.Hidden) != FileAttributes.Hidden & fileToCompress.Extension != ".gz")
                {
                    using (FileStream compressedFileStream = File.Create(fileToCompress.FullName + ".gz"))
                    {
                        using (GZipStream compressionStream = new GZipStream(compressedFileStream, CompressionLevel.Fastest))
                        {
                            originalFileStream.CopyTo(compressionStream);
                        }
                    }
                }
            }
        }
    }
}
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)
                        originalFileStream.CopyTo(compressionStream)
                    End Using
                End Using
            End If
        End Using
    End Sub

End Module

Remarks

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.

GZipStream(Stream, CompressionMode) GZipStream(Stream, CompressionMode) GZipStream(Stream, CompressionMode)

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

public:
 GZipStream(System::IO::Stream ^ stream, System::IO::Compression::CompressionMode mode);
public GZipStream (System.IO.Stream stream, System.IO.Compression.CompressionMode mode);
new System.IO.Compression.GZipStream : System.IO.Stream * System.IO.Compression.CompressionMode -> System.IO.Compression.GZipStream

Parameters

stream
Stream Stream Stream

The stream the compressed or decompressed data is written to.

mode
CompressionMode CompressionMode CompressionMode

One of the enumeration values that indicates whether to compress or decompress the stream.

Exceptions

mode is not a valid CompressionMode enumeration value.

-or-

CompressionMode is Compress and CanWrite is false.

-or-

CompressionMode is Decompress and CanRead is false.

Examples

The following example initializes a new instance of the GZipStream class with mode set to Compress. This example is part of a larger example provided for the GZipStream class.

using System;
using System.IO;
using System.IO.Compression;

public class Program
{
    private static string directoryPath = @".\temp";
    public static void Main()
    {
        DirectoryInfo directorySelected = new DirectoryInfo(directoryPath);
        Compress(directorySelected);

        foreach (FileInfo fileToDecompress in directorySelected.GetFiles("*.gz"))
        {
            Decompress(fileToDecompress);
        }
    }

    public static void Compress(DirectoryInfo directorySelected)
    {
        foreach (FileInfo fileToCompress in directorySelected.GetFiles())
        {
            using (FileStream originalFileStream = fileToCompress.OpenRead())
            {
                if ((File.GetAttributes(fileToCompress.FullName) & 
                   FileAttributes.Hidden) != FileAttributes.Hidden & fileToCompress.Extension != ".gz")
                {
                    using (FileStream compressedFileStream = File.Create(fileToCompress.FullName + ".gz"))
                    {
                        using (GZipStream compressionStream = new GZipStream(compressedFileStream, 
                           CompressionMode.Compress))
                        {
                            originalFileStream.CopyTo(compressionStream);

                        }
                    }
                    FileInfo info = new FileInfo(directoryPath + Path.DirectorySeparatorChar + fileToCompress.Name + ".gz");
                    Console.WriteLine($"Compressed {fileToCompress.Name} from {fileToCompress.Length.ToString()} to {info.Length.ToString()} bytes.");
                }

            }
        }
    }

    public static void Decompress(FileInfo fileToDecompress)
    {
        using (FileStream originalFileStream = fileToDecompress.OpenRead())
        {
            string currentFileName = fileToDecompress.FullName;
            string newFileName = currentFileName.Remove(currentFileName.Length - fileToDecompress.Extension.Length);

            using (FileStream decompressedFileStream = File.Create(newFileName))
            {
                using (GZipStream decompressionStream = new GZipStream(originalFileStream, CompressionMode.Decompress))
                {
                    decompressionStream.CopyTo(decompressedFileStream);
                    Console.WriteLine($"Decompressed: {fileToDecompress.Name}");
                }
            }
        }
    }
}
Imports System.IO
Imports System.IO.Compression

Module Module1

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

        For Each fileToDecompress As FileInfo In directorySelected.GetFiles("*.gz")
            Decompress(fileToDecompress)
        Next
    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)

                            originalFileStream.CopyTo(compressionStream)
                        End Using
                    End Using
                    Dim info As New FileInfo(directoryPath & Path.DirectorySeparatorChar & fileToCompress.Name & ".gz")
                    Console.WriteLine($"Compressed {fileToCompress.Name} from {fileToCompress.Length.ToString()} to {info.Length.ToString()} bytes.")

                End If
            End Using
        Next
    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)
                    decompressionStream.CopyTo(decompressedFileStream)
                    Console.WriteLine($"Decompressed: {fileToDecompress.Name}")
                End Using
            End Using
        End Using
    End Sub
End Module

Remarks

By default, GZipStream owns the underlying stream, so closing the stream parameter also closes the underlying stream. Note that the state of the underlying stream can affect the usability of the stream. Also, no explicit checks are performed, so no additional exceptions are thrown when the new instance is created.

If an instance of the GZipStream class is created with the mode parameter equal to Compress and no further action occurs, the stream will appear as a valid, empty compressed file.

By default, the compression level is set to Optimal when the compression mode is Compress.

GZipStream(Stream, CompressionLevel, Boolean) GZipStream(Stream, CompressionLevel, Boolean) GZipStream(Stream, CompressionLevel, Boolean)

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

public:
 GZipStream(System::IO::Stream ^ stream, System::IO::Compression::CompressionLevel compressionLevel, bool leaveOpen);
public GZipStream (System.IO.Stream stream, System.IO.Compression.CompressionLevel compressionLevel, bool leaveOpen);
new System.IO.Compression.GZipStream : System.IO.Stream * System.IO.Compression.CompressionLevel * bool -> System.IO.Compression.GZipStream

Parameters

stream
Stream Stream Stream

The stream to write the compressed data to.

compressionLevel
CompressionLevel CompressionLevel CompressionLevel

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

leaveOpen
Boolean Boolean Boolean

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

Exceptions

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

Examples

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

using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            string directoryPath = @"c:\users\public\reports";
            DirectoryInfo directorySelected = new DirectoryInfo(directoryPath);

            foreach (FileInfo fileToCompress in directorySelected.EnumerateFiles())
            {
                Compress(fileToCompress);
            }
        }

        public static void Compress(FileInfo fileToCompress)
        {
            using (FileStream originalFileStream = fileToCompress.OpenRead())
            {
                if ((File.GetAttributes(fileToCompress.FullName) & FileAttributes.Hidden) != FileAttributes.Hidden & fileToCompress.Extension != ".gz")
                {
                    using (FileStream compressedFileStream = File.Create(fileToCompress.FullName + ".gz"))
                    {
                        using (GZipStream compressionStream = new GZipStream(compressedFileStream, CompressionLevel.Fastest, true))
                        {
                            originalFileStream.CopyTo(compressionStream);
                        }
                        Console.WriteLine(string.Format("file compressed to {0} bytes", compressedFileStream.Length));
                    }
                }
            }
        }
    }
}
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

Remarks

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.

GZipStream(Stream, CompressionMode, Boolean) GZipStream(Stream, CompressionMode, Boolean) GZipStream(Stream, CompressionMode, Boolean)

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

public:
 GZipStream(System::IO::Stream ^ stream, System::IO::Compression::CompressionMode mode, bool leaveOpen);
public GZipStream (System.IO.Stream stream, System.IO.Compression.CompressionMode mode, bool leaveOpen);
new System.IO.Compression.GZipStream : System.IO.Stream * System.IO.Compression.CompressionMode * bool -> System.IO.Compression.GZipStream

Parameters

stream
Stream Stream Stream

The stream to compress.

mode
CompressionMode CompressionMode CompressionMode

One of the enumeration values that indicates whether to compress or decompress the stream.

leaveOpen
Boolean Boolean Boolean

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

Exceptions

Remarks

By default, GZipStream owns the underlying stream, so closing the stream parameter also closes the underlying stream. Note that the state of the underlying stream can affect the usability of the stream. Also, no explicit checks are performed, so no additional exceptions are thrown when the new instance is created.

If an instance of the GZipStream class is created with the mode parameter equal to Compress and no further action occurs, the stream will appear as a valid, empty compressed file.

By default, the compression level is set to Optimal when the compression mode is Compress.

Applies to