DeflateStream 构造函数

定义

初始化 DeflateStream 类的新实例。

重载

DeflateStream(Stream, CompressionLevel)

使用指定的流和压缩级别初始化 DeflateStream 类的新实例。

DeflateStream(Stream, CompressionMode)

使用指定的流和压缩模式初始化 DeflateStream 类的新实例。

DeflateStream(Stream, CompressionLevel, Boolean)

使用指定的流和压缩级别初始化 DeflateStream 类的新实例,并选择性地将流保持打开状态。

DeflateStream(Stream, CompressionMode, Boolean)

使用指定的流和压缩模式初始化 DeflateStream 类的新实例,并选择性地使流保持打开状态。

DeflateStream(Stream, ZLibCompressionOptions, Boolean)

使用指定的流、压缩选项以及选择性地将流打开来初始化 DeflateStream 类的新实例。

注解

使用 DeflateStream 类压缩大于 4 GB 的文件将导致异常。

DeflateStream(Stream, CompressionLevel)

Source:
DeflateStream.cs
Source:
DeflateStream.cs
Source:
DeflateStream.cs

使用指定的流和压缩级别初始化 DeflateStream 类的新实例。

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
Public Sub New (stream As Stream, compressionLevel As CompressionLevel)

参数

stream
Stream

将压缩数据写入到的流。

compressionLevel
CompressionLevel

枚举值之一,指示在将数据压缩到流时是强调速度还是压缩效率。

例外

stream null

流不支持写入操作,例如压缩。 (流对象上的 CanWrite 属性为 false.)

注解

如果要指定压缩效率还是速度对于 DeflateStream 类的实例更重要,请使用此构造函数。

此构造函数重载使用压缩模式 Compress。 若要将压缩模式设置为另一个值,请使用 DeflateStream(Stream, CompressionMode)DeflateStream(Stream, CompressionMode, Boolean) 重载。

例子

以下示例演示如何在创建 DeflateStream 对象时设置压缩级别,以及如何使流保持打开状态。

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

public static class FileCompressionLevelExample
{
    private const string Message = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";
    private const string OriginalFileName = "original.txt";
    private const string CompressedFileName = "compressed.dfl";

    public static void Run()
    {
        CreateFileToCompress();
        CompressFile();
        PrintResults();
        DeleteFiles();

        /*
         Output:
            The original file 'original.txt' is 445 bytes.
            The compressed file 'compressed.dfl' is 259 bytes.
         */
    }

    private static void CreateFileToCompress() => File.WriteAllText(OriginalFileName, Message);

    private static void CompressFile()
    {
        using FileStream originalFileStream = File.Open(OriginalFileName, FileMode.Open);
        using FileStream compressedFileStream = File.Create(CompressedFileName);
        using var compressor = new DeflateStream(compressedFileStream, CompressionLevel.Fastest);
        originalFileStream.CopyTo(compressor);
    }

    private static void PrintResults()
    {
        long originalSize = new FileInfo(OriginalFileName).Length;
        long compressedSize = new FileInfo(CompressedFileName).Length;

        Console.WriteLine($"The original file '{OriginalFileName}' is {originalSize} bytes.");
        Console.WriteLine($"The compressed file '{CompressedFileName}' is {compressedSize} bytes.");
    }

    private static void DeleteFiles()
    {
        File.Delete(OriginalFileName);
        File.Delete(CompressedFileName);
    }
}
Imports System.IO
Imports System.IO.Compression

Module FileCompressionLevelExample
    Private Const Message As String = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
    Private Const OriginalFileName As String = "original.txt"
    Private Const CompressedFileName As String = "compressed.dfl"

    Sub Main()
        CreateFileToCompress()
        CompressFile()
        PrintResults()
        DeleteFiles()

        'Output:
        '   The original file 'original.txt' weighs 445 bytes.
        '   The compressed file 'compressed.dfl' weighs 259 bytes.
    End Sub

    Private Sub CreateFileToCompress()
        File.WriteAllText(OriginalFileName, Message)
    End Sub

    Private Sub CompressFile()
        Using originalFileStream As FileStream = File.Open(OriginalFileName, FileMode.Open)
            Using compressedFileStream As FileStream = File.Create(CompressedFileName)
                Using compressor = New DeflateStream(compressedFileStream, CompressionLevel.Fastest)
                    originalFileStream.CopyTo(compressor)
                End Using
            End Using
        End Using
    End Sub

    Private Sub PrintResults()
        Dim originalSize As Long = New FileInfo(OriginalFileName).Length
        Dim compressedSize As Long = New FileInfo(CompressedFileName).Length

        Console.WriteLine($"The original file '{OriginalFileName}' weighs {originalSize} bytes.")
        Console.WriteLine($"The compressed file '{CompressedFileName}' weighs {compressedSize} bytes.")
    End Sub

    Private Sub DeleteFiles()
        File.Delete(OriginalFileName)
        File.Delete(CompressedFileName)
    End Sub
End Module

适用于

DeflateStream(Stream, CompressionMode)

Source:
DeflateStream.cs
Source:
DeflateStream.cs
Source:
DeflateStream.cs

使用指定的流和压缩模式初始化 DeflateStream 类的新实例。

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
Public Sub New (stream As Stream, mode As CompressionMode)

参数

stream
Stream

写入压缩数据的流或从中读取要解压缩的数据的流。

mode
CompressionMode

枚举值之一,指示是将数据压缩到流还是从流解压缩数据。

例外

stream null

mode 不是有效的 CompressionMode 值。

-或-

CompressionMode CompressCanWritefalse

-或-

CompressionMode DecompressCanReadfalse

注解

默认情况下,DeflateStream 拥有基础流,因此关闭流也会关闭基础流。 请注意,基础流的状态可能会影响流的可用性。 此外,不会执行显式检查,因此创建新实例时不会引发其他异常。

如果使用等于 Compressmode 参数创建 DeflateStream 类的实例,则立即插入标头信息。 如果没有进一步操作,流将显示为有效的空压缩文件。

使用 DeflateStream 类压缩大于 4 GB 的文件会引发异常。

默认情况下,压缩级别设置为在压缩模式 CompressOptimal

例子

以下示例演示如何使用 DeflateStream 类压缩和解压缩文件。

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

public static class FileCompressionModeExample
{
    private const string Message = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";
    private const string OriginalFileName = "original.txt";
    private const string CompressedFileName = "compressed.dfl";
    private const string DecompressedFileName = "decompressed.txt";

    public static void Run()
    {
        CreateFileToCompress();
        CompressFile();
        DecompressFile();
        PrintResults();
        DeleteFiles();

        /*
         Output:

            The original file 'original.txt' is 445 bytes. Contents: "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."

            The compressed file 'compressed.dfl' is 265 bytes.

            The decompressed file 'decompressed.txt' is 445 bytes. Contents: "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
         */
    }

    private static void CreateFileToCompress() => File.WriteAllText(OriginalFileName, Message);

    private static void CompressFile()
    {
        using FileStream originalFileStream = File.Open(OriginalFileName, FileMode.Open);
        using FileStream compressedFileStream = File.Create(CompressedFileName);
        using var compressor = new DeflateStream(compressedFileStream, CompressionMode.Compress);
        originalFileStream.CopyTo(compressor);
    }

    private static void DecompressFile()
    {
        using FileStream compressedFileStream = File.Open(CompressedFileName, FileMode.Open);
        using FileStream outputFileStream = File.Create(DecompressedFileName);
        using var decompressor = new DeflateStream(compressedFileStream, CompressionMode.Decompress);
        decompressor.CopyTo(outputFileStream);
    }

    private static void PrintResults()
    {
        long originalSize = new FileInfo(OriginalFileName).Length;
        long compressedSize = new FileInfo(CompressedFileName).Length;
        long decompressedSize = new FileInfo(DecompressedFileName).Length;

        Console.WriteLine($"The original file '{OriginalFileName}' is {originalSize} bytes. Contents: \"{File.ReadAllText(OriginalFileName)}\"");
        Console.WriteLine($"The compressed file '{CompressedFileName}' is {compressedSize} bytes.");
        Console.WriteLine($"The decompressed file '{DecompressedFileName}' is {decompressedSize} bytes. Contents: \"{File.ReadAllText(DecompressedFileName)}\"");
    }

    private static void DeleteFiles()
    {
        File.Delete(OriginalFileName);
        File.Delete(CompressedFileName);
        File.Delete(DecompressedFileName);
    }
}
Imports System.IO
Imports System.IO.Compression

Module FileCompressionModeExample
    Private Const Message As String = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
    Private Const OriginalFileName As String = "original.txt"
    Private Const CompressedFileName As String = "compressed.dfl"
    Private Const DecompressedFileName As String = "decompressed.txt"

    Sub Main()
        CreateFileToCompress()
        CompressFile()
        DecompressFile()
        PrintResults()
        DeleteFiles()

        'Output:

        '   The original file 'original.txt' weighs 445 bytes. Contents: "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."

        '   The compressed file 'compressed.dfl' weighs 265 bytes.

        '   The decompressed file 'decompressed.txt' weighs 445 bytes. Contents: "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."

    End Sub

    Private Sub CreateFileToCompress()
        File.WriteAllText(OriginalFileName, Message)
    End Sub

    Private Sub CompressFile()
        Using originalFileStream As FileStream = File.Open(OriginalFileName, FileMode.Open)
            Using compressedFileStream As FileStream = File.Create(CompressedFileName)
                Using compressor = New DeflateStream(compressedFileStream, CompressionMode.Compress)
                    originalFileStream.CopyTo(compressor)
                End Using
            End Using
        End Using
    End Sub

    Private Sub DecompressFile()
        Using compressedFileStream As FileStream = File.Open(CompressedFileName, FileMode.Open)
            Using outputFileStream As FileStream = File.Create(DecompressedFileName)
                Using decompressor = New DeflateStream(compressedFileStream, CompressionMode.Decompress)
                    decompressor.CopyTo(outputFileStream)
                End Using
            End Using
        End Using
    End Sub

    Private Sub PrintResults()
        Dim originalSize As Long = New FileInfo(OriginalFileName).Length
        Dim compressedSize As Long = New FileInfo(CompressedFileName).Length
        Dim decompressedSize As Long = New FileInfo(DecompressedFileName).Length

        Console.WriteLine($"The original file '{OriginalFileName}' weighs {originalSize} bytes. Contents: ""{File.ReadAllText(OriginalFileName)}""")
        Console.WriteLine($"The compressed file '{CompressedFileName}' weighs {compressedSize} bytes.")
        Console.WriteLine($"The decompressed file '{DecompressedFileName}' weighs {decompressedSize} bytes. Contents: ""{File.ReadAllText(DecompressedFileName)}""")
    End Sub

    Private Sub DeleteFiles()
        File.Delete(OriginalFileName)
        File.Delete(CompressedFileName)
        File.Delete(DecompressedFileName)
    End Sub
End Module

适用于

DeflateStream(Stream, CompressionLevel, Boolean)

Source:
DeflateStream.cs
Source:
DeflateStream.cs
Source:
DeflateStream.cs

使用指定的流和压缩级别初始化 DeflateStream 类的新实例,并选择性地将流保持打开状态。

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
Public Sub New (stream As Stream, compressionLevel As CompressionLevel, leaveOpen As Boolean)

参数

stream
Stream

将压缩数据写入到的流。

compressionLevel
CompressionLevel

枚举值之一,指示在将数据压缩到流时是强调速度还是压缩效率。

leaveOpen
Boolean

释放 DeflateStream 对象后,true 将流对象保持打开状态;否则,false

例外

stream null

流不支持写入操作,例如压缩。 (流对象上的 CanWrite 属性为 false.)

注解

如果想要指定压缩效率还是速度对于 DeflateStream 类的实例更重要,以及释放 DeflateStream 对象后是否保持流对象打开,请使用此构造函数。

此构造函数重载使用压缩模式 Compress。 若要将压缩模式设置为另一个值,请使用 DeflateStream(Stream, CompressionMode)DeflateStream(Stream, CompressionMode, Boolean) 重载。

例子

以下示例演示如何在创建 DeflateStream 对象时设置压缩级别,以及如何使流保持打开状态。

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

public static class FileCompressionLevelExample
{
    private const string Message = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";
    private const string OriginalFileName = "original.txt";
    private const string CompressedFileName = "compressed.dfl";

    public static void Run()
    {
        CreateFileToCompress();
        CompressFile();
        PrintResults();
        DeleteFiles();

        /*
         Output:
            The original file 'original.txt' is 445 bytes.
            The compressed file 'compressed.dfl' is 259 bytes.
         */
    }

    private static void CreateFileToCompress() => File.WriteAllText(OriginalFileName, Message);

    private static void CompressFile()
    {
        using FileStream originalFileStream = File.Open(OriginalFileName, FileMode.Open);
        using FileStream compressedFileStream = File.Create(CompressedFileName);
        using var compressor = new DeflateStream(compressedFileStream, CompressionLevel.Fastest);
        originalFileStream.CopyTo(compressor);
    }

    private static void PrintResults()
    {
        long originalSize = new FileInfo(OriginalFileName).Length;
        long compressedSize = new FileInfo(CompressedFileName).Length;

        Console.WriteLine($"The original file '{OriginalFileName}' is {originalSize} bytes.");
        Console.WriteLine($"The compressed file '{CompressedFileName}' is {compressedSize} bytes.");
    }

    private static void DeleteFiles()
    {
        File.Delete(OriginalFileName);
        File.Delete(CompressedFileName);
    }
}
Imports System.IO
Imports System.IO.Compression

Module FileCompressionLevelExample
    Private Const Message As String = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
    Private Const OriginalFileName As String = "original.txt"
    Private Const CompressedFileName As String = "compressed.dfl"

    Sub Main()
        CreateFileToCompress()
        CompressFile()
        PrintResults()
        DeleteFiles()

        'Output:
        '   The original file 'original.txt' weighs 445 bytes.
        '   The compressed file 'compressed.dfl' weighs 259 bytes.
    End Sub

    Private Sub CreateFileToCompress()
        File.WriteAllText(OriginalFileName, Message)
    End Sub

    Private Sub CompressFile()
        Using originalFileStream As FileStream = File.Open(OriginalFileName, FileMode.Open)
            Using compressedFileStream As FileStream = File.Create(CompressedFileName)
                Using compressor = New DeflateStream(compressedFileStream, CompressionLevel.Fastest)
                    originalFileStream.CopyTo(compressor)
                End Using
            End Using
        End Using
    End Sub

    Private Sub PrintResults()
        Dim originalSize As Long = New FileInfo(OriginalFileName).Length
        Dim compressedSize As Long = New FileInfo(CompressedFileName).Length

        Console.WriteLine($"The original file '{OriginalFileName}' weighs {originalSize} bytes.")
        Console.WriteLine($"The compressed file '{CompressedFileName}' weighs {compressedSize} bytes.")
    End Sub

    Private Sub DeleteFiles()
        File.Delete(OriginalFileName)
        File.Delete(CompressedFileName)
    End Sub
End Module

适用于

DeflateStream(Stream, CompressionMode, Boolean)

Source:
DeflateStream.cs
Source:
DeflateStream.cs
Source:
DeflateStream.cs

使用指定的流和压缩模式初始化 DeflateStream 类的新实例,并选择性地使流保持打开状态。

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
Public Sub New (stream As Stream, mode As CompressionMode, leaveOpen As Boolean)

参数

stream
Stream

写入压缩数据的流或从中读取要解压缩的数据的流。

mode
CompressionMode

枚举值之一,指示是将数据压缩到流还是从流解压缩数据。

leaveOpen
Boolean

true 释放 DeflateStream 对象后使流保持打开状态;否则,false

例外

stream null

mode 不是有效的 CompressionMode 值。

-或-

CompressionMode CompressCanWritefalse

-或-

CompressionMode DecompressCanReadfalse

注解

默认情况下,DeflateStream 拥有基础流,因此关闭流也会关闭基础流。 请注意,基础流的状态可能会影响流的可用性。 此外,不会执行显式检查,因此创建新实例时不会引发其他异常。

如果使用等于 Compressmode 参数创建 DeflateStream 类的实例,则立即插入标头信息。 如果没有进一步操作,流将显示为有效的空压缩文件。

使用 DeflateStream 类压缩大于 4 GB 的文件会引发异常。

默认情况下,压缩级别设置为在压缩模式 CompressOptimal

例子

以下示例演示如何使用 DeflateStream 类压缩和解压缩文件。

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

public static class FileCompressionModeExample
{
    private const string Message = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";
    private const string OriginalFileName = "original.txt";
    private const string CompressedFileName = "compressed.dfl";
    private const string DecompressedFileName = "decompressed.txt";

    public static void Run()
    {
        CreateFileToCompress();
        CompressFile();
        DecompressFile();
        PrintResults();
        DeleteFiles();

        /*
         Output:

            The original file 'original.txt' is 445 bytes. Contents: "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."

            The compressed file 'compressed.dfl' is 265 bytes.

            The decompressed file 'decompressed.txt' is 445 bytes. Contents: "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
         */
    }

    private static void CreateFileToCompress() => File.WriteAllText(OriginalFileName, Message);

    private static void CompressFile()
    {
        using FileStream originalFileStream = File.Open(OriginalFileName, FileMode.Open);
        using FileStream compressedFileStream = File.Create(CompressedFileName);
        using var compressor = new DeflateStream(compressedFileStream, CompressionMode.Compress);
        originalFileStream.CopyTo(compressor);
    }

    private static void DecompressFile()
    {
        using FileStream compressedFileStream = File.Open(CompressedFileName, FileMode.Open);
        using FileStream outputFileStream = File.Create(DecompressedFileName);
        using var decompressor = new DeflateStream(compressedFileStream, CompressionMode.Decompress);
        decompressor.CopyTo(outputFileStream);
    }

    private static void PrintResults()
    {
        long originalSize = new FileInfo(OriginalFileName).Length;
        long compressedSize = new FileInfo(CompressedFileName).Length;
        long decompressedSize = new FileInfo(DecompressedFileName).Length;

        Console.WriteLine($"The original file '{OriginalFileName}' is {originalSize} bytes. Contents: \"{File.ReadAllText(OriginalFileName)}\"");
        Console.WriteLine($"The compressed file '{CompressedFileName}' is {compressedSize} bytes.");
        Console.WriteLine($"The decompressed file '{DecompressedFileName}' is {decompressedSize} bytes. Contents: \"{File.ReadAllText(DecompressedFileName)}\"");
    }

    private static void DeleteFiles()
    {
        File.Delete(OriginalFileName);
        File.Delete(CompressedFileName);
        File.Delete(DecompressedFileName);
    }
}
Imports System.IO
Imports System.IO.Compression

Module FileCompressionModeExample
    Private Const Message As String = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
    Private Const OriginalFileName As String = "original.txt"
    Private Const CompressedFileName As String = "compressed.dfl"
    Private Const DecompressedFileName As String = "decompressed.txt"

    Sub Main()
        CreateFileToCompress()
        CompressFile()
        DecompressFile()
        PrintResults()
        DeleteFiles()

        'Output:

        '   The original file 'original.txt' weighs 445 bytes. Contents: "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."

        '   The compressed file 'compressed.dfl' weighs 265 bytes.

        '   The decompressed file 'decompressed.txt' weighs 445 bytes. Contents: "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."

    End Sub

    Private Sub CreateFileToCompress()
        File.WriteAllText(OriginalFileName, Message)
    End Sub

    Private Sub CompressFile()
        Using originalFileStream As FileStream = File.Open(OriginalFileName, FileMode.Open)
            Using compressedFileStream As FileStream = File.Create(CompressedFileName)
                Using compressor = New DeflateStream(compressedFileStream, CompressionMode.Compress)
                    originalFileStream.CopyTo(compressor)
                End Using
            End Using
        End Using
    End Sub

    Private Sub DecompressFile()
        Using compressedFileStream As FileStream = File.Open(CompressedFileName, FileMode.Open)
            Using outputFileStream As FileStream = File.Create(DecompressedFileName)
                Using decompressor = New DeflateStream(compressedFileStream, CompressionMode.Decompress)
                    decompressor.CopyTo(outputFileStream)
                End Using
            End Using
        End Using
    End Sub

    Private Sub PrintResults()
        Dim originalSize As Long = New FileInfo(OriginalFileName).Length
        Dim compressedSize As Long = New FileInfo(CompressedFileName).Length
        Dim decompressedSize As Long = New FileInfo(DecompressedFileName).Length

        Console.WriteLine($"The original file '{OriginalFileName}' weighs {originalSize} bytes. Contents: ""{File.ReadAllText(OriginalFileName)}""")
        Console.WriteLine($"The compressed file '{CompressedFileName}' weighs {compressedSize} bytes.")
        Console.WriteLine($"The decompressed file '{DecompressedFileName}' weighs {decompressedSize} bytes. Contents: ""{File.ReadAllText(DecompressedFileName)}""")
    End Sub

    Private Sub DeleteFiles()
        File.Delete(OriginalFileName)
        File.Delete(CompressedFileName)
        File.Delete(DecompressedFileName)
    End Sub
End Module

适用于

DeflateStream(Stream, ZLibCompressionOptions, Boolean)

使用指定的流、压缩选项以及选择性地将流打开来初始化 DeflateStream 类的新实例。

public DeflateStream (System.IO.Stream stream, System.IO.Compression.ZLibCompressionOptions compressionOptions, bool leaveOpen = false);
new System.IO.Compression.DeflateStream : System.IO.Stream * System.IO.Compression.ZLibCompressionOptions * bool -> System.IO.Compression.DeflateStream
Public Sub New (stream As Stream, compressionOptions As ZLibCompressionOptions, Optional leaveOpen As Boolean = false)

参数

stream
Stream

将压缩数据写入到的流。

compressionOptions
ZLibCompressionOptions

用于微调压缩流的选项。

leaveOpen
Boolean

释放 DeflateStream 对象后,true 将流对象保持打开状态;否则,false

例外

streamcompressionOptionsnull

适用于