GZipStream 构造函数

定义

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

重载

GZipStream(Stream, CompressionLevel)

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

GZipStream(Stream, CompressionMode)

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

GZipStream(Stream, CompressionLevel, Boolean)

使用指定的流和压缩级别,初始化 GZipStream 类的新实例,有选择性的打开流。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 类的新实例,有选择性的打开流。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 类的新实例。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

参数

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

示例

下面的示例演示如何在创建GZipStream对象时设置压缩级别。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

注解

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

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

GZipStream(Stream, CompressionMode)

用指定的流和压缩模式初始化 GZipStream 类的新实例。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

参数

stream
Stream

压缩或解压缩后的数据被写入的流。The stream the compressed or decompressed data is written to.

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

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

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

示例

下面的示例通过GZipStream mode将设置为来Compress初始化类的新实例。The following example initializes a new instance of the GZipStream class with mode set to Compress. 此示例是为GZipStream类提供的更大示例的一部分。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

注解

默认情况下GZipStream ,拥有基础流,因此stream关闭参数也会关闭基础流。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.

如果创建GZipStream类的实例时使用的mode参数等于Compress ,并且不执行其他操作,则流将显示为有效的空压缩文件。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.

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

GZipStream(Stream, CompressionLevel, Boolean)

使用指定的流和压缩级别,初始化 GZipStream 类的新实例,有选择性的打开流。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

参数

stream
Stream

要将压缩数据写入到其中的流。The stream to write the compressed data to.

compressionLevel
CompressionLevel

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

leaveOpen
Boolean

如果在释放 GZipStream 对象之后打开流对象,则为 true;否则为 falsetrue to leave the stream object open after disposing the GZipStream 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.)

示例

下面的示例演示如何在创建GZipStream对象时设置压缩级别,以及如何使流保持打开状态。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

注解

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

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

GZipStream(Stream, CompressionMode, Boolean)

使用指定的流和压缩模式,初始化 GZipStream 类的新实例,有选择性的打开流。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

参数

stream
Stream

要压缩的流。The stream to compress.

mode
CompressionMode

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

leaveOpen
Boolean

如果在释放 GZipStream 对象之后打开流,则为 true;否则为 falsetrue to leave the stream open after disposing the GZipStream 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.

注解

默认情况下GZipStream ,拥有基础流,因此stream关闭参数也会关闭基础流。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.

如果创建GZipStream类的实例时使用的mode参数等于Compress ,并且不执行其他操作,则流将显示为有效的空压缩文件。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.

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

适用于