GZipStream / DeflateStream 압축 / 압축 풀기
6043 단어 Stream
. NET 은 두 가지 압축 자료 흐름 을 제공 합 니 다. 하 나 는 GZipStream 이 고 다른 하 나 는 Deflate Stream 입 니 다. 압축 자료 흐름 을 사용 할 때 몇 가지 주의해 야 합 니 다.
1. 이 두 알고리즘 은 최대 4G 의 자료 만 압축 할 수 있다.2. 파일 을 다른 사람 에 게 나 누 어 사용 하고 ZIP 으로 압축 을 풀 수 있 도록 하려 면 GZipStream 을 사용 하 십시오.
압축 파일 단계:
1. System. IO 및 System. IO. Compression 클래스 참조
2. 압축 할 파일 열기
/ / 원본 파일 열기
FileStream SourceFile = File.OpenRead(inFile);
3. 압축 된 파일 만 들 기
/ / 목적지 파일 만 들 기
FileStream DestnFile = File.Create(outFile);
4. 압축 자료 흐름 GZipStream 참조, 압축 매개 변수 참조
/ / 참조 압축 분류
GZipStream myGZip = new GZipStream(DestnFile, CompressionMode.Compress);
5. 압축 된 자료 흐름 까지 자료 쓰기
/ / 읽 기 압축 (원본 파일) 변 수 를 설정 합 니 다.
int ByteFile = SourceFile.ReadByte();
while (ByteFile != -1)
{
//myGZip 자료 흐름 쓰기 (압축)
myGZip.WriteByte((byte)ByteFile);
ByteFile = SourceFile.ReadByte();
}
6. 자원 방출
/ / 자원 방출
myGZip.Dispose();
DestnFile.Dispose();
SourceFile.Dispose();
압축 풀기 단계:
1. System. IO 및 System. IO. Compression 클래스 참조
2. 압축 을 풀 파일 열기
/ / 원본 파일 열기
FileStream SourceFile = File.OpenRead(inFile);
3. 압축 된 파일 만 들 기
/ / 목적지 파일 만 들 기
FileStream DestnFile = File.Create(outFile);
4. 압축 자료 흐름 GZipStream 참조, 압축 해제 파라미터 참조
/ / 압축 풀기 클래스 참조
GZipStream myGZip = new GZipStream(SourceFile, CompressionMode.Decompress);
5. 목적지 까지 자 료 를 기록 합 니 다.
/ / 압축 풀기 (원본 파일) 변 수 를 설정 합 니 다.
int ByteFile = myGZip.ReadByte();
while (ByteFile != -1)
{
//목적지 까지 자 료 를 쓰기 (압축 풀기)
DestnFile.WriteByte((byte)ByteFile);
ByteFile = myGZip.ReadByte();
}
6. 자원 방출
/ / 자원 방출
myGZip.Dispose();
DestnFile.Dispose();
SourceFile.Dispose();
Imports System.IO
Imports System.IO.Compression
'
''' <summary>
'''
''' </summary>
''' <param name="sourceFile"> </param>
''' <param name="destinationFile"> </param>
''' <remarks></remarks>
Public Function CompressFile(ByVal sourceFile As String, ByVal destinationFile As String) As Boolean
If Not File.Exists(sourceFile) Then Throw New FileNotFoundException
Dim sourceStream As FileStream = Nothing
Dim destinationStream As FileStream = Nothing
Dim compressedStream As GZipStream = Nothing
Try
sourceStream = New FileStream(sourceFile, FileMode.Open, FileAccess.Read, FileShare.Read)
Dim buffer(sourceStream.Length - 1) As Byte
Dim checkCounter As Integer = sourceStream.Read(buffer, 0, buffer.Length)
If checkCounter <> buffer.Length Then Throw New ApplicationException
destinationStream = New FileStream(destinationFile, FileMode.OpenOrCreate, FileAccess.Write)
compressedStream = New GZipStream(destinationStream, CompressionMode.Compress, True)
compressedStream.Write(buffer, 0, buffer.Length)
Catch ex As ApplicationException
Return False
Finally
If sourceStream IsNot Nothing Then sourceStream.Close()
If compressedStream IsNot Nothing Then compressedStream.Close()
If destinationStream IsNot Nothing Then destinationStream.Close()
End Try
Return True
End Function
''' <summary>
'''
''' </summary>
''' <param name="sourceFile"> </param>
''' <param name="destinationFile"> </param>
''' <remarks></remarks>
Public Function DecompressFile(ByVal sourceFile As String, ByVal destinationFile As String) As Boolean
If Not File.Exists(sourceFile) Then Throw New FileNotFoundException
Dim sourceStream As FileStream = Nothing
Dim destinationStream As FileStream = Nothing
Dim decompressedStream As GZipStream = Nothing
Dim quartetBuffer(4) As Byte
Try
sourceStream = New FileStream(sourceFile, FileMode.Open)
decompressedStream = New GZipStream(sourceStream, CompressionMode.Decompress, True)
Dim position As Integer = sourceStream.Length - 4
sourceStream.Position = position
sourceStream.Read(quartetBuffer, 0, 4)
sourceStream.Position = 0
Dim checkLength As Integer = BitConverter.ToInt32(quartetBuffer, 0)
Dim buffer(checkLength + 100) As Byte
Dim offset, total As Integer
While (True)
Dim bytesRead As Integer = decompressedStream.Read(buffer, offset, 100)
If bytesRead = 0 Then Exit While
offset += bytesRead
total += bytesRead
End While
destinationStream = New FileStream(destinationFile, FileMode.Create)
destinationStream.Write(buffer, 0, total)
destinationStream.Flush()
Catch ex As ApplicationException
Return False
Finally
If sourceStream IsNot Nothing Then sourceStream.Close()
If decompressedStream IsNot Nothing Then decompressedStream.Close()
If destinationStream IsNot Nothing Then destinationStream.Close()
End Try
Return True
End Function
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
socket 통신 코드 연습--매일 조금씩 진보/*服务器*/ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <sys/socket.h> #include ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.