关 键 词:
vb.net
压缩
gzip
GZipStream
解压缩
VB.NET 2005
@H_675_22@Imports System.IO
Imports System.IO.Compression
@H_675_22@Public Class ClsZip
Public Sub CompressFile(ByVal sourceFile As String,ByVal desTinationFile As String)
'压缩档案
If Not File.Exists(sourceFilE) Then
Throw New FileNotFoundException
End If
@H_675_22@ Dim sourceStream As FileStream = Nothing
Dim desTinationStream As FileStream = Nothing
Dim compressedStream As GZipStream = Nothing
@H_675_22@ Try
'Read the bytes from the source filE into a byte array
sourceStream = New FileStream(sourceFile,FileMode.open,FileAccess.Read,FileShare.Read)
'Read the source stream values into the buffer
Dim buffer(sourceStream.Length - 1) As Byte
Dim checkCounter As Integer = sourceStream.Read(buffer,buffer.Length)
@H_675_22@ If checkCounter <> buffer.Length Then
Throw New ApplicationException
End If
@H_675_22@ 'Open the FileStream to write to
desTinationStream = New FileStream(desTinationFile,FileMode.openOrCreate,FileAccess.WritE)
@H_675_22@ 'Create a compression stream poinTing to the destiantion stream
compressedStream = New GZipStream(desTinationStream,CompressionMode.Compress,TruE)
@H_675_22@ 'Now write the compressed data to the desTination file
compressedStream.Write(buffer,buffer.Length)
Catch ex As ApplicationException
messageBox.Show(ex.messagE)
Finally
'Make sure we allways close all streams
If sourceStream Isnot Nothing Then
sourceStream.Close()
End If
@H_675_22@ If compressedStream Isnot Nothing Then
compressedStream.Close()
End If
@H_675_22@ if DesTinationStream Isnot Nothing Then
desTinationStream.Close()
End If
End Try
End Sub
@H_675_22@ Public Sub DecompressFile(ByVal sourceFile As String,ByVal desTinationFile As String)
'解压缩档案
'make sure the source file is there
If Not File.Exists(sourceFilE) Then
Throw New FileNotFoundException
End If
@H_675_22@ 'Create the streams and byte arrays needed
Dim sourceStream As FileStream = Nothing
Dim desTinationStream As FileStream = Nothing
Dim decompressedStream As GZipStream = Nothing
Dim quartetBuffer(4) As Byte
@H_675_22@ Try
'Read in the compressed source stream
sourceStream = New FileStream(sourceFile,FileMode.open)
@H_675_22@ 'Create a compression stream poinTing to the destiantion stream
decompressedStream = New GZipStream(sourceStream,CompressionMode.Decompress,TruE)
@H_675_22@ 'Read the footer to determine the length of the destiantion file
Dim position As Integer = sourceStream.Length - 4
sourceStream.Position = position
sourceStream.Read(quartetBuffer,4)
sourceStream.Position = 0
Dim checkLength As Integer = BitConverter.ToInt32(quartetBuffer,0)
@H_675_22@ Dim buffer(checkLength + 100) As Byte
Dim offset,@R_186_10586@l As Integer
'Read the compressed data into the buffer
While (true)
Dim bytesRead As Integer = decompressedStream.Read(buffer,offset,100)
@H_675_22@ If bytesRead = 0 Then Exit While
@H_675_22@ offset += bytesRead
@R_186_10586@l += bytesRead
End While
@H_675_22@ 'Now write everything to the desTination file
desTinationStream = New FileStream(desTinationFile,FileMode.create)
desTinationStream.Write(buffer,@R_186_10586@l)
@H_675_22@ 'and flush everyhTing to clean out the buffer
desTinationStream.Flush()
@H_675_22@ Catch ex As ApplicationException
messageBox.Show(ex.messagE)
Finally
'Make sure we allways close all streams
If sourceStream Isnot Nothing Then
sourceStream.Close()
End If
@H_675_22@ if DecompressedStream Isnot Nothing Then
decompressedStream.Close()
End If
@H_675_22@ if DesTinationStream Isnot Nothing Then desTinationStream.Close() End If End Try End Sub End Class