DeflateStream 构造函数

定义

初始化 DeflateStream 类的新实例。Initializes a new instance of the DeflateStream class.

重载

DeflateStream(Stream, CompressionLevel)

用指定的流和压缩级别初始化 DeflateStream 类的新实例。Initializes a new instance of the DeflateStream class by using the specified stream and compression level.

DeflateStream(Stream, CompressionMode)

用指定的流和压缩模式初始化 DeflateStream 类的新实例。Initializes a new instance of the DeflateStream class by using the specified stream and compression mode.

DeflateStream(Stream, CompressionLevel, Boolean)

使用指定的流和压缩级别,初始化 DeflateStream 类的新实例,有选择性的打开流。Initializes a new instance of the DeflateStream class by using the specified stream and compression level, and optionally leaves the stream open.

DeflateStream(Stream, CompressionMode, Boolean)

使用指定的流和压缩模式,初始化 DeflateStream 类的新实例,有选择性的打开流。Initializes a new instance of the DeflateStream class by using the specified stream and compression mode, and optionally leaves the stream open.

注解

DeflateStream使用类压缩大于 4 GB 的文件将导致异常。Using the DeflateStream class to compress a file larger than 4 GB will cause an exception.

DeflateStream(Stream, CompressionLevel)

用指定的流和压缩级别初始化 DeflateStream 类的新实例。Initializes a new instance of the DeflateStream class by using the specified stream and compression level.

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

参数

stream
Stream

要压缩的流。The stream to compress.

compressionLevel
CompressionLevel

压缩流时,指示是否要强调速度或压缩效率的枚举值之一。One of the enumeration values that indicates whether to emphasize speed or compression efficiency when compressing the stream.

异常

streamnullstream is null.

流不支持写入操作,如压缩。The stream does not support write operations such as compression. (流对象上的 CanWrite 属性是 false。)(The CanWrite property on the stream object is false.)

示例

下面的示例演示如何在创建DeflateStream对象时设置压缩级别。The following example shows how to set the compression level when creating a DeflateStream 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 != ".cmp")
                {
                    using (FileStream compressedFileStream = File.Create(fileToCompress.FullName + ".cmp"))
                    {
                        using (DeflateStream compressionStream = new DeflateStream(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 <> ".cmp") Then
                Using compressedFileStream As FileStream = File.Create(fileToCompress.FullName + ".cmp")
                    Using compressionStream As DeflateStream = New DeflateStream(compressedFileStream, CompressionLevel.Fastest)
                        originalFileStream.CopyTo(compressionStream)
                    End Using
                End Using
            End If
        End Using
    End Sub

End Module

注解

当您想要指定压缩效率或速度对于DeflateStream类的实例是否更重要时,可以使用此构造函数。You use this constructor when you want to specify whether compression efficiency or speed is more important for an instance of the DeflateStream class.

此构造函数重载使用压缩模式CompressThis constructor overload uses the compression mode Compress. 若要将压缩模式设置为另一个值, DeflateStream(Stream, CompressionMode)DeflateStream(Stream, CompressionMode, Boolean)使用或重载。To set the compression mode to another value, use the DeflateStream(Stream, CompressionMode) or DeflateStream(Stream, CompressionMode, Boolean) overload.

DeflateStream(Stream, CompressionMode)

用指定的流和压缩模式初始化 DeflateStream 类的新实例。Initializes a new instance of the DeflateStream class by using the specified stream and compression mode.

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

参数

stream
Stream

要压缩或解压缩的流。The stream to compress or decompress.

mode
CompressionMode

指示是要压缩还是解压缩流的枚举值之一。One of the enumeration values that indicates whether to compress or decompress the stream.

异常

streamnullstream is null.

mode 不是有效的 CompressionMode 值。mode is not a valid CompressionMode value.

- 或 --or- CompressionModeCompress,而 CanWritefalseCompressionMode is Compress and CanWrite is false.

-or- CompressionModeDecompress,而 CanReadfalseCompressionMode is Decompress and CanRead is false.

示例

下面的示例演示如何使用DeflateStream类压缩和解压缩文件。The following example shows how to use the DeflateStream class to compress and decompress a file.

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

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

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

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

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

    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 (DeflateStream decompressionStream = new DeflateStream(originalFileStream, CompressionMode.Decompress))
                {
                    decompressionStream.CopyTo(decompressedFileStream);
                    Console.WriteLine("Decompressed: {0}", fileToDecompress.Name);
                }
            }
        }
    }
}
Imports System.IO
Imports System.IO.Compression

Public Class Program
    Shared directoryPath As String = "c:\temp"
    Public Shared Sub Main()

        Dim directorySelected As New DirectoryInfo(directoryPath)
        Compress(directorySelected)


        For Each fileToDecompress As FileInfo In directorySelected.GetFiles("*.cmp")
            Decompress(fileToDecompress)
        Next
    End Sub

    Public Shared Sub Compress(directorySelected As DirectoryInfo)

        For Each file__1 As FileInfo In directorySelected.GetFiles("*.xml")
            Using originalFileStream As FileStream = file__1.OpenRead()
                If (File.GetAttributes(file__1.FullName) And FileAttributes.Hidden) <> FileAttributes.Hidden And file__1.Extension <> ".cmp" Then
                    Using compressedFileStream As FileStream = File.Create(file__1.FullName & ".cmp")
                        Using compressionStream As Compression.DeflateStream = New DeflateStream(compressedFileStream, CompressionMode.Compress)
                            originalFileStream.CopyTo(compressionStream)
                        End Using
                    End Using

                    Dim info As New FileInfo(directoryPath & "\" & file__1.Name & ".cmp")
                    Console.WriteLine("Compressed {0} from {1} to {2} bytes.", file__1.Name, file__1.Length, info.Length)
                End If
            End Using
        Next
    End Sub

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

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

注解

默认情况下DeflateStream ,拥有基础流,因此关闭流也会关闭基础流。By default, DeflateStream owns the underlying stream, so closing the stream 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.

如果创建DeflateStream类的实例时使用的mode参数等于Compress,则会立即插入标头信息。If an instance of the DeflateStream class is created with the mode parameter equal to Compress, header information is inserted immediately. 如果没有进一步的操作,流将显示为有效的、空的压缩文件。If no further action occurs, the stream appears as a valid, empty, compressed file.

DeflateStream使用类压缩大于 4 GB 的文件会引发异常。Using the DeflateStream class to compress a file larger than 4 GB raises an exception.

默认情况下,压缩Optimal Compress模式为时设置为的压缩级别。By default, the compression level is set to Optimal when the compression mode is Compress.

DeflateStream(Stream, CompressionLevel, Boolean)

使用指定的流和压缩级别,初始化 DeflateStream 类的新实例,有选择性的打开流。Initializes a new instance of the DeflateStream class by using the specified stream and compression level, and optionally leaves the stream open.

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

参数

stream
Stream

要压缩的流。The stream to compress.

compressionLevel
CompressionLevel

压缩流时,指示是否要强调速度或压缩效率的枚举值之一。One of the enumeration values that indicates whether to emphasize speed or compression efficiency when compressing the stream.

leaveOpen
Boolean

如果在释放 DeflateStream 对象之后打开流对象,则为 true;否则为 falsetrue to leave the stream object open after disposing the DeflateStream object; otherwise, false.

异常

streamnullstream is null.

流不支持写入操作,如压缩。The stream does not support write operations such as compression. (流对象上的 CanWrite 属性是 false。)(The CanWrite property on the stream object is false.)

示例

下面的示例演示如何在创建DeflateStream对象时设置压缩级别,以及如何使流保持打开状态。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));
                    }
                }
            }
        }
    }
}
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 <> ".cmp") Then
                Using compressedFileStream As FileStream = File.Create(fileToCompress.FullName + ".cmp")
                    Using compressionStream As DeflateStream = New DeflateStream(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

注解

若要指定压缩效率或速度对于DeflateStream类的实例是否更重要,以及在DeflateStream释放对象后是否将流对象保持打开状态,请使用此构造函数。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.

此构造函数重载使用压缩模式CompressThis constructor overload uses the compression mode Compress. 若要将压缩模式设置为另一个值, DeflateStream(Stream, CompressionMode)DeflateStream(Stream, CompressionMode, Boolean)使用或重载。To set the compression mode to another value, use the DeflateStream(Stream, CompressionMode) or DeflateStream(Stream, CompressionMode, Boolean) overload.

DeflateStream(Stream, CompressionMode, Boolean)

使用指定的流和压缩模式,初始化 DeflateStream 类的新实例,有选择性的打开流。Initializes a new instance of the DeflateStream class by using the specified stream and compression mode, and optionally leaves the stream open.

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

参数

stream
Stream

要压缩或解压缩的流。The stream to compress or decompress.

mode
CompressionMode

指示是要压缩还是解压缩流的枚举值之一。One of the enumeration values that indicates whether to compress or decompress the stream.

leaveOpen
Boolean

如果在释放 DeflateStream 对象之后打开流,则为 true;否则为 falsetrue to leave the stream open after disposing the DeflateStream object; otherwise, false.

异常

streamnullstream is null.

mode 不是有效的 CompressionMode 值。mode is not a valid CompressionMode value.

-or- CompressionModeCompress,而 CanWritefalseCompressionMode is Compress and CanWrite is false.

-or- CompressionModeDecompress,而 CanReadfalseCompressionMode is Decompress and CanRead is false.

示例

下面的代码示例演示如何使用DeflateStream类压缩和解压缩文件。The following code example shows how to use the DeflateStream class to compress and decompress a file.

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

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

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

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

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

    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 (DeflateStream decompressionStream = new DeflateStream(originalFileStream, CompressionMode.Decompress))
                {
                    decompressionStream.CopyTo(decompressedFileStream);
                    Console.WriteLine("Decompressed: {0}", fileToDecompress.Name);
                }
            }
        }
    }
}
Imports System.IO
Imports System.IO.Compression

Public Class Program
    Shared directoryPath As String = "c:\temp"
    Public Shared Sub Main()

        Dim directorySelected As New DirectoryInfo(directoryPath)
        Compress(directorySelected)


        For Each fileToDecompress As FileInfo In directorySelected.GetFiles("*.cmp")
            Decompress(fileToDecompress)
        Next
    End Sub

    Public Shared Sub Compress(directorySelected As DirectoryInfo)

        For Each file__1 As FileInfo In directorySelected.GetFiles("*.xml")
            Using originalFileStream As FileStream = file__1.OpenRead()
                If (File.GetAttributes(file__1.FullName) And FileAttributes.Hidden) <> FileAttributes.Hidden And file__1.Extension <> ".cmp" Then
                    Using compressedFileStream As FileStream = File.Create(file__1.FullName & ".cmp")
                        Using compressionStream As Compression.DeflateStream = New DeflateStream(compressedFileStream, CompressionMode.Compress)
                            originalFileStream.CopyTo(compressionStream)
                        End Using
                    End Using

                    Dim info As New FileInfo(directoryPath & "\" & file__1.Name & ".cmp")
                    Console.WriteLine("Compressed {0} from {1} to {2} bytes.", file__1.Name, file__1.Length, info.Length)
                End If
            End Using
        Next
    End Sub

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

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

注解

默认情况下DeflateStream ,拥有基础流,因此关闭流也会关闭基础流。By default, DeflateStream owns the underlying stream, so closing the stream 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.

如果创建DeflateStream类的实例时使用的mode参数等于Compress,则会立即插入标头信息。If an instance of the DeflateStream class is created with the mode parameter equal to Compress, header information is inserted immediately. 如果没有进一步的操作,流将显示为有效的、空的压缩文件。If no further action occurs, the stream appears as a valid, empty, compressed file.

DeflateStream使用类压缩大于 4 GB 的文件会引发异常。Using the DeflateStream class to compress a file larger than 4 GB raises an exception.

默认情况下,压缩Optimal Compress模式为时设置为的压缩级别。By default, the compression level is set to Optimal when the compression mode is Compress.

适用于