CompressionMode 열거형
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
기본 스트림에서 데이터를 압축하거나 압축 해제할지 여부를 지정합니다.
public enum class CompressionMode
public enum CompressionMode
type CompressionMode =
Public Enum CompressionMode
- 상속
필드
Compress | 1 | 데이터를 기본 스트림으로 압축합니다. |
Decompress | 0 | 기본 스트림에서 데이터를 압축 해제합니다. |
예제
다음 코드 예제에서는 클래스와 CompressionMode 함께 열거형을 GZipStream 사용하여 파일을 압축 및 압축 해제합니다.
using System;
using System.IO;
using System.IO.Compression;
public 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.gz";
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.gz' is 283 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 GZipStream(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 GZipStream(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
Imports System.IO
Imports System.IO.Compression
Public Class 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.gz"
Private Const DecompressedFileName As String = "decompressed.txt"
Public Shared 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.gz' weighs 283 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 Shared Sub CreateFileToCompress()
File.WriteAllText(OriginalFileName, Message)
End Sub
Private Shared Sub CompressFile()
Using originalFileStream As FileStream = File.Open(OriginalFileName, FileMode.Open)
Using compressedFileStream As FileStream = File.Create(CompressedFileName)
Using compressor = New GZipStream(compressedFileStream, CompressionMode.Compress)
originalFileStream.CopyTo(compressor)
End Using
End Using
End Using
End Sub
Private Shared Sub DecompressFile()
Using compressedFileStream As FileStream = File.Open(CompressedFileName, FileMode.Open)
Using outputFileStream As FileStream = File.Create(DecompressedFileName)
Using decompressor = New GZipStream(compressedFileStream, CompressionMode.Decompress)
decompressor.CopyTo(outputFileStream)
End Using
End Using
End Using
End Sub
Private Shared 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 Shared Sub DeleteFiles()
File.Delete(OriginalFileName)
File.Delete(CompressedFileName)
File.Delete(DecompressedFileName)
End Sub
End Class
설명
이 열거형은 및 DeflateStream 클래스와 함께 GZipStream 사용됩니다.
적용 대상
GitHub에서 Microsoft와 공동 작업
이 콘텐츠의 원본은 GitHub에서 찾을 수 있으며, 여기서 문제와 끌어오기 요청을 만들고 검토할 수도 있습니다. 자세한 내용은 참여자 가이드를 참조하세요.
.NET