Export (0) Print
Expand All

GZipStream Constructor (Stream, CompressionLevel)

.NET Framework 4.5

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 GZipStream(
	Stream stream,
	CompressionLevel 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.


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.

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())

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

.NET Framework

Supported in: 4.6, 4.5

Supported in: Windows Phone 8.1
© 2015 Microsoft