GZipStream Constructor (Stream, CompressionMode)

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

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

public GZipStream(
	Stream stream,
	CompressionMode mode


Type: System.IO.Stream

The stream to compress or decompress.

Type: System.IO.Compression.CompressionMode

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


stream is null.


mode is not a valid CompressionMode enumeration value.


CompressionMode is Compress and CanWrite is false.


CompressionMode is Decompress and CanRead is false.

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.

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;

namespace zip
    public class Program
        private static string directoryPath = @"c:\temp";
        public static void Main()
            DirectoryInfo directorySelected = new DirectoryInfo(directoryPath);

            foreach (FileInfo fileToDecompress in directorySelected.GetFiles("*.gz"))

        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, 

                        FileInfo info = new FileInfo(directoryPath + "\\" + fileToCompress.Name + ".gz");
                        Console.WriteLine("Compressed {0} from {1} to {2} bytes.",
                        fileToCompress.Name, fileToCompress.Length.ToString(), info.Length.ToString());


        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))
                        Console.WriteLine("Decompressed: {0}", fileToDecompress.Name);

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

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

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
© 2014 Microsoft