DeflateStream Konstruktory
Definice
Důležité
Některé informace platí pro předběžně vydaný produkt, který se může zásadně změnit, než ho výrobce nebo autor vydá. Microsoft neposkytuje žádné záruky, výslovné ani předpokládané, týkající se zde uváděných informací.
Inicializuje novou instanci DeflateStream třídy.
Přetížení
DeflateStream(Stream, CompressionLevel) |
Inicializuje novou instanci třídy DeflateStream pomocí zadané úrovně datového proudu a komprese. |
DeflateStream(Stream, CompressionMode) |
Inicializuje novou instanci třídy DeflateStream pomocí zadaného streamu a režimu komprese. |
DeflateStream(Stream, CompressionLevel, Boolean) |
Inicializuje novou instanci třídy DeflateStream pomocí zadané úrovně datového proudu a komprese a volitelně ponechá stream otevřený. |
DeflateStream(Stream, CompressionMode, Boolean) |
Inicializuje novou instanci DeflateStream třídy pomocí zadaného streamu a režimu komprese a volitelně ponechá stream otevřený. |
DeflateStream(Stream, ZLibCompressionOptions, Boolean) |
Inicializuje novou instanci třídy DeflateStream pomocí zadaného datového proudu, možností komprese a volitelně ponechá stream otevřený. |
Poznámky
Použití třídy DeflateStream ke komprimaci souboru většího než 4 GB způsobí výjimku.
DeflateStream(Stream, CompressionLevel)
- Zdroj:
- DeflateStream.cs
- Zdroj:
- DeflateStream.cs
- Zdroj:
- DeflateStream.cs
Inicializuje novou instanci třídy DeflateStream pomocí zadané úrovně datového proudu a komprese.
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)
Parametry
- stream
- Stream
Datový proud, do kterého se zapisují komprimovaná data.
- compressionLevel
- CompressionLevel
Jedna z hodnot výčtu, která označuje, zda je třeba zdůraznit rychlost nebo efektivitu komprese při komprimaci dat do datového proudu.
Výjimky
stream
je null
.
Stream nepodporuje operace zápisu, jako je komprese. (Vlastnost CanWrite objektu streamu je false
.)
Poznámky
Tento konstruktor použijete, pokud chcete určit, zda je efektivita komprese nebo rychlost důležitější pro instanci DeflateStream třídy.
Toto přetížení konstruktoru používá režim komprese Compress. Chcete-li nastavit režim komprese na jinou hodnotu, použijte DeflateStream(Stream, CompressionMode) nebo DeflateStream(Stream, CompressionMode, Boolean) přetížení.
Příklady
Následující příklad ukazuje, jak nastavit úroveň komprese při vytváření DeflateStream objektu a jak ponechat datový proud otevřený.
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
Platí pro
DeflateStream(Stream, CompressionMode)
- Zdroj:
- DeflateStream.cs
- Zdroj:
- DeflateStream.cs
- Zdroj:
- DeflateStream.cs
Inicializuje novou instanci třídy DeflateStream pomocí zadaného streamu a režimu komprese.
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)
Parametry
- stream
- Stream
Stream, do kterého se komprimovaná data zapisují nebo ze kterých se data dekomprimují, se čtou.
- mode
- CompressionMode
Jedna z hodnot výčtu, která označuje, zda se mají komprimovat data do datového proudu nebo dekomprimovat data z datového proudu.
Výjimky
stream
je null
.
mode
není platná hodnota CompressionMode.
-nebo-
CompressionMode je Compress a CanWrite je false
.
-nebo-
CompressionMode je Decompress a CanRead je false
.
Poznámky
Ve výchozím nastavení DeflateStream vlastníkem podkladového datového proudu, takže zavření datového proudu také zavře podkladový stream. Všimněte si, že stav podkladového streamu může ovlivnit použitelnost datového proudu. Navíc se neprovedou žádné explicitní kontroly, takže při vytvoření nové instance se nevyvolají žádné další výjimky.
Pokud je instance třídy DeflateStream vytvořena s parametrem mode
rovna Compress
, informace hlavičky se vloží okamžitě. Pokud nedojde k žádné další akci, datový proud se zobrazí jako platný, prázdný komprimovaný soubor.
Pomocí třídy DeflateStream komprimuje soubor větší než 4 GB výjimku.
Ve výchozím nastavení je úroveň komprese nastavena na Optimal, pokud je režim komprese Compress.
Příklady
Následující příklad ukazuje, jak pomocí třídy DeflateStream komprimovat a dekomprimovat soubor.
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
Platí pro
DeflateStream(Stream, CompressionLevel, Boolean)
- Zdroj:
- DeflateStream.cs
- Zdroj:
- DeflateStream.cs
- Zdroj:
- DeflateStream.cs
Inicializuje novou instanci třídy DeflateStream pomocí zadané úrovně datového proudu a komprese a volitelně ponechá stream otevřený.
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)
Parametry
- stream
- Stream
Datový proud, do kterého se zapisují komprimovaná data.
- compressionLevel
- CompressionLevel
Jedna z hodnot výčtu, která označuje, zda je třeba zdůraznit rychlost nebo efektivitu komprese při komprimaci dat do datového proudu.
- leaveOpen
- Boolean
true
nechat objekt streamu otevřený po vystavení objektu DeflateStream; jinak false
.
Výjimky
stream
je null
.
Stream nepodporuje operace zápisu, jako je komprese. (Vlastnost CanWrite objektu streamu je false
.)
Poznámky
Tento konstruktor použijete, pokud chcete určit, zda je efektivita komprese nebo rychlost důležitější pro instanci třídy DeflateStream a zda ponechat objekt stream otevřený po zveřejnění DeflateStream objektu.
Toto přetížení konstruktoru používá režim komprese Compress. Chcete-li nastavit režim komprese na jinou hodnotu, použijte DeflateStream(Stream, CompressionMode) nebo DeflateStream(Stream, CompressionMode, Boolean) přetížení.
Příklady
Následující příklad ukazuje, jak nastavit úroveň komprese při vytváření DeflateStream objektu a jak ponechat datový proud otevřený.
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
Platí pro
DeflateStream(Stream, CompressionMode, Boolean)
- Zdroj:
- DeflateStream.cs
- Zdroj:
- DeflateStream.cs
- Zdroj:
- DeflateStream.cs
Inicializuje novou instanci DeflateStream třídy pomocí zadaného streamu a režimu komprese a volitelně ponechá stream otevřený.
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)
Parametry
- stream
- Stream
Stream, do kterého se komprimovaná data zapisují nebo ze kterých se data dekomprimují, se čtou.
- mode
- CompressionMode
Jedna z hodnot výčtu, která označuje, zda se mají komprimovat data do datového proudu nebo dekomprimovat data z datového proudu.
- leaveOpen
- Boolean
true
nechat datový proud otevřený po zrušení objektu DeflateStream; jinak false
.
Výjimky
stream
je null
.
mode
není platná hodnota CompressionMode.
-nebo-
CompressionMode je Compress a CanWrite je false
.
-nebo-
CompressionMode je Decompress a CanRead je false
.
Poznámky
Ve výchozím nastavení DeflateStream vlastníkem podkladového datového proudu, takže zavření datového proudu také zavře podkladový stream. Všimněte si, že stav podkladového streamu může ovlivnit použitelnost datového proudu. Navíc se neprovedou žádné explicitní kontroly, takže při vytvoření nové instance se nevyvolají žádné další výjimky.
Pokud je instance třídy DeflateStream vytvořena s parametrem mode
rovna Compress
, informace hlavičky se vloží okamžitě. Pokud nedojde k žádné další akci, datový proud se zobrazí jako platný, prázdný komprimovaný soubor.
Pomocí třídy DeflateStream komprimuje soubor větší než 4 GB výjimku.
Ve výchozím nastavení je úroveň komprese nastavena na Optimal, pokud je režim komprese Compress.
Příklady
Následující příklad ukazuje, jak pomocí třídy DeflateStream komprimovat a dekomprimovat soubor.
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
Platí pro
DeflateStream(Stream, ZLibCompressionOptions, Boolean)
Inicializuje novou instanci třídy DeflateStream pomocí zadaného datového proudu, možností komprese a volitelně ponechá stream otevřený.
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)
Parametry
- stream
- Stream
Datový proud, do kterého se zapisují komprimovaná data.
- compressionOptions
- ZLibCompressionOptions
Možnosti pro vyladění streamu komprese.
- leaveOpen
- Boolean
true
nechat objekt streamu otevřený po vystavení objektu DeflateStream; jinak false
Výjimky
stream
nebo compressionOptions
je null
.