DeflateStream 類別

定義

提供方法和屬性,以透過 Deflate 演算法來壓縮和解壓縮資料流。

public ref class DeflateStream : System::IO::Stream
public class DeflateStream : System.IO.Stream
type DeflateStream = class
    inherit Stream
Public Class DeflateStream
Inherits Stream
繼承
DeflateStream
繼承

範例

下列範例示範如何使用 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

備註

這個類別代表Deflate演算法,這是不失真檔案壓縮和解壓縮的業界標準演算法。 從 .NET Framework 4.5 開始,類別會DeflateStream使用 zlib 連結庫。 因此,它提供較佳的壓縮演算法,而且在大部分情況下,壓縮檔案比舊版 .NET Framework 小。

這個類別原本不會提供將檔案新增至 zip 封存或從 zip 封存中擷取檔案的功能。 若要使用 zip 封存,請使用 ZipArchiveZipArchiveEntry 類別。

類別 DeflateStream 會使用與類別所使用的 GZipStream gzip 資料格式相同的壓縮演算法。

GZipStream 中的DeflateStream壓縮功能會公開為數據流。 數據是以位元組位元組為基礎讀取,因此無法執行多個傳遞,以判斷壓縮整個檔案或大型數據區塊的最佳方法。 DeflateStreamGZipStream 類別最適合用於未壓縮的數據源。 如果源數據已經壓縮,使用這些類別可能會實際增加數據流的大小。

建構函式

DeflateStream(Stream, CompressionLevel)

使用指定的資料流和壓縮層級,初始化 DeflateStream 類別的新執行個體。

DeflateStream(Stream, CompressionLevel, Boolean)

使用指定的資料流和壓縮層級,初始化 DeflateStream 類別的新執行個體,並選擇性地保持開啟資料流。

DeflateStream(Stream, CompressionMode)

使用指定的資料流和壓縮模式,初始化 DeflateStream 類別的新執行個體。

DeflateStream(Stream, CompressionMode, Boolean)

使用指定的資料流和壓縮模式,初始化 DeflateStream 類別的新執行個體,並選擇性地保持開啟資料流。

屬性

BaseStream

取得基礎資料流的參考。

CanRead

取得值,指出在解壓縮檔案時,資料流是否支援讀取。

CanSeek

取得值,指出資料流是否支援搜尋。

CanTimeout

取得值,該值判斷目前的資料流是否可以逾時。

(繼承來源 Stream)
CanWrite

取得值,指出資料流是否支援寫入。

Length

這個屬性不受支援,而且一律會擲回 NotSupportedException

Position

這個屬性不受支援,而且一律會擲回 NotSupportedException

ReadTimeout

取得或設定值 (以毫秒為單位),該值決定資料流在逾時前將嘗試讀取多長時間。

(繼承來源 Stream)
WriteTimeout

取得或設定毫秒值,該值決定在逾時前資料流將嘗試寫入多長時間。

(繼承來源 Stream)

方法

BeginRead(Byte[], Int32, Int32, AsyncCallback, Object)

開始非同步的讀取作業。 (請考慮使用 ReadAsync(Byte[], Int32, Int32) 方法替代。)

BeginRead(Byte[], Int32, Int32, AsyncCallback, Object)

開始非同步的讀取作業。 (請考慮用 ReadAsync(Byte[], Int32, Int32) 替代。)

(繼承來源 Stream)
BeginWrite(Byte[], Int32, Int32, AsyncCallback, Object)

開始非同步的寫入作業。 (請考慮使用 WriteAsync(Byte[], Int32, Int32) 方法替代。)

BeginWrite(Byte[], Int32, Int32, AsyncCallback, Object)

開始非同步的寫入作業。 (請考慮用 WriteAsync(Byte[], Int32, Int32) 替代。)

(繼承來源 Stream)
Close()

關閉目前資料流和釋放與目前資料流相關聯的任何資源 (例如通訊端和檔案控制代碼)。 請確定正確地處置資料流,而非呼叫這個方法。

(繼承來源 Stream)
CopyTo(Stream)

從目前資料流讀取位元組,並將其寫入另一個資料流中。 這兩個數據流位置都是由複製的位元元組數目進階。

(繼承來源 Stream)
CopyTo(Stream, Int32)

使用指定的緩衝區大小,從目前 Deflate 資料流讀取所有位元組,並將其寫入另一個資料流中。

CopyTo(Stream, Int32)

使用指定的緩衝區大小,從目前資料流讀取所有位元組,並將其寫入另一個資料流中。 這兩個數據流位置都是由複製的位元元組數目進階。

(繼承來源 Stream)
CopyToAsync(Stream)

以非同步的方式從目前資料流讀取所有位元組,並將其寫入另一個資料流中。 這兩個數據流位置都是由複製的位元元組數目進階。

(繼承來源 Stream)
CopyToAsync(Stream, CancellationToken)

使用指定的取消權杖,以非同步的方式從目前資料流讀取位元組,並將其寫入另一個資料流。 這兩個數據流位置都是由複製的位元元組數目進階。

(繼承來源 Stream)
CopyToAsync(Stream, Int32)

使用指定的緩衝區大小,以非同步的方式從目前資料流讀取所有位元組,並將其寫入另一個資料流中。 這兩個數據流位置都是由複製的位元元組數目進階。

(繼承來源 Stream)
CopyToAsync(Stream, Int32, CancellationToken)

使用指定的緩衝區大小,以非同步方式從目前 Deflate 資料流讀取所有位元組,並將其寫入另一個資料流中。

CopyToAsync(Stream, Int32, CancellationToken)

使用指定的緩衝區大小和取消語彙基元,以非同步的方式從目前資料流讀取位元組,並將其寫入另一個資料流。 這兩個數據流位置都是由複製的位元元組數目進階。

(繼承來源 Stream)
CreateObjRef(Type)

建立包含所有相關資訊的物件,這些資訊是產生用來與遠端物件通訊的所需 Proxy。

(繼承來源 MarshalByRefObject)
CreateWaitHandle()
已淘汰.
已淘汰.
已淘汰.

配置 WaitHandle 物件。

(繼承來源 Stream)
Dispose()

釋放 Stream 所使用的所有資源。

(繼承來源 Stream)
Dispose(Boolean)

釋放 DeflateStream 所使用的 Unmanaged 資源,並選擇性地釋放 Managed 資源。

DisposeAsync()

以非同步方式釋放 DeflateStream 使用的不受控資源。

DisposeAsync()

以非同步方式釋放 Stream 使用的不受控資源。

(繼承來源 Stream)
EndRead(IAsyncResult)

等候暫止的非同步讀取完成。 (請考慮使用 ReadAsync(Byte[], Int32, Int32) 方法替代。)

EndRead(IAsyncResult)

等候暫止的非同步讀取完成。 (請考慮用 ReadAsync(Byte[], Int32, Int32) 替代。)

(繼承來源 Stream)
EndWrite(IAsyncResult)

結束非同步的寫入作業。 (請考慮使用 WriteAsync(Byte[], Int32, Int32) 方法替代。)

EndWrite(IAsyncResult)

結束非同步的寫入作業。 (請考慮用 WriteAsync(Byte[], Int32, Int32) 替代。)

(繼承來源 Stream)
Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
Flush()

這個方法目前的實作沒有任何功能。

FlushAsync()

以非同步的方式清除這個資料流的所有緩衝區,並造成所有緩衝資料都寫入基礎裝置。

(繼承來源 Stream)
FlushAsync(CancellationToken)

以非同步方式清除這個 Deflate 資料流的所有緩衝區,造成所有緩衝資料都寫入基礎裝置,且監視取消要求。

FlushAsync(CancellationToken)

以非同步的方式清除這個資料流的所有緩衝區,造成所有緩衝資料都寫入基礎裝置,並且監視取消要求。

(繼承來源 Stream)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetLifetimeService()
已淘汰.

擷取控制這個執行個體存留期 (Lifetime) 原則的目前存留期服務物件。

(繼承來源 MarshalByRefObject)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
InitializeLifetimeService()
已淘汰.

取得存留期服務物件,以控制這個執行個體的存留期原則。

(繼承來源 MarshalByRefObject)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
MemberwiseClone(Boolean)

建立目前 MarshalByRefObject 物件的淺層複本。

(繼承來源 MarshalByRefObject)
ObjectInvariant()
已淘汰.

提供 Contract 的支援。

(繼承來源 Stream)
Read(Byte[], Int32, Int32)

將大量解壓縮的位元組讀入指定的位元組陣列。

Read(Span<Byte>)

將位元組序列從目前的 Deflate 資料流程讀取到位元組範圍,並將 Deflate 資料流內位置前移到讀取的位元組數目。

Read(Span<Byte>)

當在衍生類別中覆寫時,自目前資料流讀取一連串的位元組,並依所讀取的位元組數目進階資料流中的位置。

(繼承來源 Stream)
ReadAsync(Byte[], Int32, Int32)

以非同步的方式從目前的資料流讀取位元組序列,並依讀取的位元組數將資料流中的位置往前移。

(繼承來源 Stream)
ReadAsync(Byte[], Int32, Int32, CancellationToken)

以非同步方式從目前的 Deflate 資料流讀取位元組序列、將其寫入至位元組陣列、依讀取的位元組數將 Deflate 資料流內位置往前移,並監視取消要求。

ReadAsync(Byte[], Int32, Int32, CancellationToken)

以非同步的方式從目前資料流讀取一連串的位元組、依所讀取的位元組數目進階資料流中的位置,以及監視取消要求。

(繼承來源 Stream)
ReadAsync(Memory<Byte>, CancellationToken)

以非同步方式從目前的 Deflate 資料流讀取位元組序列、將其寫入至位元組記憶體範圍、依讀取的位元組數將 Deflate 資料流內位置往前移,並監視取消要求。

ReadAsync(Memory<Byte>, CancellationToken)

以非同步的方式從目前資料流讀取一連串的位元組、依所讀取的位元組數目進階資料流中的位置,以及監視取消要求。

(繼承來源 Stream)
ReadAtLeast(Span<Byte>, Int32, Boolean)

從目前的數據流讀取至少一個字節數目,並依讀取的位元元組數目將數據流中的位置往前移。

(繼承來源 Stream)
ReadAtLeastAsync(Memory<Byte>, Int32, Boolean, CancellationToken)

以異步方式從目前數據流讀取至少一個字節數目、依讀取的位元組數目將數據流中的位置往前移,並監視取消要求。

(繼承來源 Stream)
ReadByte()

從 Deflate 資料流讀取一個位元組,並將資料流內位置推進一個位元組;如果在 Deflate 資料流末端,則傳回 -1。

ReadByte()

從資料流讀取一個位元組,並將資料流的位置推進一個位元組;如果在資料流末端,則傳回 -1。

(繼承來源 Stream)
ReadExactly(Byte[], Int32, Int32)

count從目前的數據流讀取位元組數目,並將數據流中的位置往前移。

(繼承來源 Stream)
ReadExactly(Span<Byte>)

從目前的數據流讀取位元組,並將該位置往前移,直到 buffer 填滿為止。

(繼承來源 Stream)
ReadExactlyAsync(Byte[], Int32, Int32, CancellationToken)

以異步方式從目前數據流讀取 count 位元組數目、將數據流內的位置往前移,並監視取消要求。

(繼承來源 Stream)
ReadExactlyAsync(Memory<Byte>, CancellationToken)

以異步方式從目前的數據流讀取位元組、將數據流內的位置往前移,直到 buffer 填滿為止,並監視取消要求。

(繼承來源 Stream)
Seek(Int64, SeekOrigin)

這個作業不受支援,而且一律會擲回 NotSupportedException

SetLength(Int64)

這個作業不受支援,而且一律會擲回 NotSupportedException

ToString()

傳回代表目前物件的字串。

(繼承來源 Object)
Write(Byte[], Int32, Int32)

從指定的位元組陣列將壓縮的位元組寫入基礎資料流。

Write(ReadOnlySpan<Byte>)

將位元組序列寫入目前的 Deflate 資料流,並依寫入的位元組數將 Deflate 資料流內目前的位置往前移。

Write(ReadOnlySpan<Byte>)

在衍生類別中覆寫時,將一連串的位元組寫入目前的資料流,並且由這個資料流中目前的位置前移寫入的位元組數目。

(繼承來源 Stream)
WriteAsync(Byte[], Int32, Int32)

以非同步的方式將位元組序列寫入至目前的資料流,並依寫入的位元組數將資料流中目前的位置往前移。

(繼承來源 Stream)
WriteAsync(Byte[], Int32, Int32, CancellationToken)

以非同步方式從指定位元組陣列將壓縮的位元組寫入基礎 Deflate 資料流。

WriteAsync(Byte[], Int32, Int32, CancellationToken)

以非同步的方式將一連串的位元組寫入目前的資料流,由這個資料流中目前的位置前移寫入的位元組數目,並且監視取消要求。

(繼承來源 Stream)
WriteAsync(ReadOnlyMemory<Byte>, CancellationToken)

從指定的唯讀記憶體區域,以非同步方式將壓縮的位元組寫入基礎 Deflate 資料流。

WriteAsync(ReadOnlyMemory<Byte>, CancellationToken)

以非同步的方式將一連串的位元組寫入目前的資料流,由這個資料流中目前的位置前移寫入的位元組數目,並且監視取消要求。

(繼承來源 Stream)
WriteByte(Byte)

將位元組寫入目前的Deflate數據流,並將這個Deflate數據流內的目前位置往前移一個。

WriteByte(Byte)

寫入一個位元組至資料流的目前位置,並將資料流位置推進一個位元組。

(繼承來源 Stream)

擴充方法

AsInputStream(Stream)

將適用於 Windows 市集應用程式的 .NET 中的受控資料流轉換成 Windows 執行階段中的輸入資料流。

AsOutputStream(Stream)

將適用於 Windows 市集應用程式的 .NET 中的受控資料流轉換成 Windows 執行階段中的輸出資料流。

AsRandomAccessStream(Stream)

將指定的資料流轉換為隨機存取資料流。

ConfigureAwait(IAsyncDisposable, Boolean)

設定如何執行從非同步可處置項目傳回的工作 await。

適用於

另請參閱