DeflateStream 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
提供使用 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 类压缩和解压缩文件。
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 存档,请使用 ZipArchive 和 ZipArchiveEntry 类。
DeflateStream 类使用与 GZipStream 类使用的 gzip 数据格式相同的压缩算法。
DeflateStream 和 GZipStream 中的压缩功能以流的形式公开。 数据按字节读取,因此无法执行多个传递来确定压缩整个文件或大型数据块的最佳方法。 DeflateStream 和 GZipStream 类最适合用于未压缩的数据源。 如果源数据已压缩,则使用这些类实际上可能会增加流的大小。
构造函数
DeflateStream(Stream, CompressionLevel) |
使用指定的流和压缩级别初始化 DeflateStream 类的新实例。 |
DeflateStream(Stream, CompressionLevel, Boolean) |
使用指定的流和压缩级别初始化 DeflateStream 类的新实例,并选择性地将流保持打开状态。 |
DeflateStream(Stream, CompressionMode) |
使用指定的流和压缩模式初始化 DeflateStream 类的新实例。 |
DeflateStream(Stream, CompressionMode, Boolean) |
使用指定的流和压缩模式初始化 DeflateStream 类的新实例,并选择性地使流保持打开状态。 |
DeflateStream(Stream, ZLibCompressionOptions, Boolean) |
提供使用 Deflate 算法压缩和解压缩流的方法和属性。 |
属性
BaseStream |
获取对基础流的引用。 |
CanRead |
获取一个值,该值指示流在解压缩文件时是否支持读取。 |
CanSeek |
获取一个值,该值指示流是否支持查找。 |
CanTimeout |
获取一个值,该值确定当前流是否可以超时。 (继承自 Stream) |
CanWrite |
获取一个值,该值指示流是否支持写入。 |
Length |
此属性不受支持,并且始终引发 NotSupportedException。 |
Position |
此属性不受支持,并且始终引发 NotSupportedException。 |
ReadTimeout |
获取或设置一个值(以毫秒为单位),该值确定流在超时前尝试读取的时间。 (继承自 Stream) |
WriteTimeout |
获取或设置一个值(以毫秒为单位),该值确定流在超时之前尝试写入的时间。 (继承自 Stream) |
方法
扩展方法
CopyToAsync(Stream, PipeWriter, CancellationToken) |
使用取消令牌从 Stream 异步读取字节并将其写入指定的 PipeWriter。 |
AsInputStream(Stream) |
将适用于 Windows 应用商店应用的 .NET 中的托管流转换为 Windows 运行时中的输入流。 |
AsOutputStream(Stream) |
将适用于 Windows 应用商店应用的 .NET 中的托管流转换为 Windows 运行时中的输出流。 |
AsRandomAccessStream(Stream) |
将指定的流转换为随机访问流。 |
ConfigureAwait(IAsyncDisposable, Boolean) |
配置如何执行从异步可释放项返回的任务的 await。 |
适用于
另请参阅
- DeflateStream、GZipStream 和 CryptoStream 中的部分读取和零字节读取