DeflateStream Constructor (Stream, CompressionLevel, Boolean)

.NET Framework 4.6 and 4.5

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

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

public DeflateStream(
	Stream stream,
	CompressionLevel compressionLevel,
	bool leaveOpen
)

Parameters

stream
Type: System.IO.Stream

The stream to compress.

compressionLevel
Type: System.IO.Compression.CompressionLevel

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

leaveOpen
Type: System.Boolean

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

ExceptionCondition
ArgumentNullException

stream is null.

ArgumentException

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 DeflateStream class, and whether to leave the stream object open after disposing the DeflateStream object.

This constructor overload uses the compression mode Compress. To set the compression mode to another value, use the DeflateStream(Stream, CompressionMode) or DeflateStream(Stream, CompressionMode, Boolean) overload.

The following example shows how to set the compression level when creating a DeflateStream 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 != ".cmp")
                {
                    using (FileStream compressedFileStream = File.Create(fileToCompress.FullName + ".cmp"))
                    {
                        using (DeflateStream compressionStream = new DeflateStream(compressedFileStream, CompressionLevel.Fastest, true))
                        {
                            originalFileStream.CopyTo(compressionStream);
                        }
                        Console.WriteLine(string.Format("file compressed to {0} bytes", compressedFileStream.Length));
                    }
                }
            }
        }
    }
}

.NET Framework

Supported in: 4.6, 4.5

.NET for Windows Phone apps

Supported in: Windows Phone 8.1
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2015 Microsoft