CompressFile and DecompressFile

  ''' <summary>
    ''' 压缩
    ''' </summary>
    ''' <param name="sourceFile"></param>
    ''' <param name="destinationFile"></param>
    ''' <remarks></remarks>
    Public Sub CompressFile(ByVal sourceFile As StringByVal destinationFile As String)

        
' make sure the source file is there
        If File.Exists(sourceFile) = False Then
            
Throw New FileNotFoundException
        
End If

        
' Create the streams and byte arrays needed
        Dim buffer As Byte() = Nothing
        
Dim sourceStream As FileStream = Nothing
        
Dim destinationStream As FileStream = Nothing
        
Dim compressedStream As GZipStream = Nothing

        
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
            buffer = New Byte(sourceStream.Length) {}
            
Dim checkCounter As Integer = sourceStream.Read(buffer, 0, buffer.Length)

            
' Open the FileStream to write to
            destinationStream = New FileStream(destinationFile, FileMode.OpenOrCreate, FileAccess.Write)

            
' Create a compression stream pointing to the destiantion stream
            compressedStream = New GZipStream(destinationStream, CompressionMode.Compress, True)

            
'Now write the compressed data to the destination file
            compressedStream.Write(buffer, 0, buffer.Length)

        
Catch ex As ApplicationException
            MessageBox.Show(ex.Message, 
"An Error occured during compression", MessageBoxButtons.OK, MessageBoxIcon.Error)
        
Finally
            
' Make sure we allways close all streams
            If Not (sourceStream Is NothingThen
                sourceStream.Close()
            
End If
            
If Not (compressedStream Is NothingThen
                compressedStream.Close()
            
End If
            
If Not (destinationStream Is NothingThen
                destinationStream.Close()
            
End If
            
'If IO.File.Exists("base_tmp.do") = True Then
            '    IO.File.Delete("base_tmp.do")
            'End If
        End Try
    
End Sub


    
''' <summary>
    ''' 解压
    ''' </summary>
    ''' <param name="sourceFile"></param>
    ''' <param name="destinationFile"></param>
    ''' <remarks></remarks>
    Public Sub DecompressFile(ByVal sourceFile As StringByVal destinationFile As String)

        
' make sure the source file is there
        If File.Exists(sourceFile) = False Then
            
Throw New FileNotFoundException
        
End If

        
' Create the streams and byte arrays needed
        Dim sourceStream As FileStream = Nothing
        
Dim destinationStream As FileStream = Nothing
        
Dim decompressedStream As GZipStream = Nothing
        
Dim quartetBuffer As Byte() = Nothing

        
Try
            
' Read in the compressed source stream
            sourceStream = New FileStream(sourceFile, FileMode.Open)

            
' Create a compression stream pointing to the destiantion stream
            decompressedStream = New GZipStream(sourceStream, CompressionMode.Decompress, True)

            
' Read the footer to determine the length of the destiantion file
            quartetBuffer = New Byte(4) {}
            
Dim position As Integer = CType(sourceStream.Length, Integer- 4
            sourceStream.Position 
= position
            sourceStream.Read(quartetBuffer, 
04)
            sourceStream.Position 
= 0
            
Dim checkLength As Integer = BitConverter.ToInt32(quartetBuffer, 0)

            
Dim buffer(checkLength + 100As Byte
            
Dim offset As Integer = 0
            
Dim total As Integer = 0

            
' Read the compressed data into the buffer
            While True
                
Dim bytesRead As Integer = decompressedStream.Read(buffer, offset, 100)
                
If bytesRead = 0 Then
                    
Exit While
                
End If
                offset 
+= bytesRead
                total 
+= bytesRead
            
End While

            
' Now write everything to the destination file
            destinationStream = New FileStream(destinationFile, FileMode.Create)
            destinationStream.Write(buffer, 
0, total)

            
' and flush everyhting to clean out the buffer
            destinationStream.Flush()

        
Catch ex As ApplicationException
            MessageBox.Show(ex.Message, 
"An Error occured during compression", MessageBoxButtons.OK, MessageBoxIcon.Error)
        
Finally
            
' Make sure we allways close all streams
            If Not (sourceStream Is NothingThen
                sourceStream.Close()
            
End If
            
If Not (decompressedStream Is NothingThen
                decompressedStream.Close()
            
End If
            
If Not (destinationStream Is NothingThen
                destinationStream.Close()
            
End If
        
End Try

    
End Sub

exp:
CompressFile("test.mdb","test.rar")
DecompressFile("test.rar","test.mdb")
posted @ 2007-07-10 00:58  yongwnet  阅读(414)  评论(0编辑  收藏  举报