DeflateStream 建構函式
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
初始化 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)
使用指定的數據流和壓縮層級,初始化 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)
使用指定的數據流和壓縮模式,初始化 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 是 Compress,CanWrite 是 false
。
-或-
CompressionMode 是 Decompress,CanRead 是 false
。
備註
根據預設,DeflateStream 擁有基礎數據流,因此關閉數據流也會關閉基礎數據流。 請注意,基礎數據流的狀態可能會影響數據流的可用性。 此外,不會執行任何明確的檢查,因此在建立新實例時不會擲回其他例外狀況。
如果使用等於 Compress
的 mode
參數建立 DeflateStream 類別的實例,則會立即插入標頭資訊。 如果沒有進一步的動作發生,數據流會顯示為有效的空白壓縮檔案。
使用 DeflateStream 類別來壓縮大於 4 GB 的檔案會引發例外狀況。
根據預設,當壓縮模式 Compress時,壓縮層級會設定為 Optimal。
例子
下列範例示範如何使用 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)
使用指定的數據流和壓縮層級,初始化 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
true
處置 DeflateStream 對象之後,讓數據流物件保持開啟;否則,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)
使用指定的數據流和壓縮模式,初始化 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 是 Compress,CanWrite 是 false
。
-或-
CompressionMode 是 Decompress,CanRead 是 false
。
備註
根據預設,DeflateStream 擁有基礎數據流,因此關閉數據流也會關閉基礎數據流。 請注意,基礎數據流的狀態可能會影響數據流的可用性。 此外,不會執行任何明確的檢查,因此在建立新實例時不會擲回其他例外狀況。
如果使用等於 Compress
的 mode
參數建立 DeflateStream 類別的實例,則會立即插入標頭資訊。 如果沒有進一步的動作發生,數據流會顯示為有效的空白壓縮檔案。
使用 DeflateStream 類別來壓縮大於 4 GB 的檔案會引發例外狀況。
根據預設,當壓縮模式 Compress時,壓縮層級會設定為 Optimal。
例子
下列範例示範如何使用 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
true
處置 DeflateStream 對象之後,讓數據流物件保持開啟;否則,false
例外狀況
stream
或 compressionOptions
null
。