Public Sub CompressFile(ByVal strSourceFile() As String, ByVal strDestinationFile As String)
'// Make sure all files exist before processing
For intLoop As Integer = 0 To strSourceFile$.Length - 1
If (Not File.Exists(strSourceFile$(intLoop))) Then
Throw New FileNotFoundException
End If
Next
'// Declare method variables
Dim bytBuffer As Byte() = Nothing
Dim fsSourceStream() As FileStream = Nothing
Dim fsDestinationStream As FileStream = Nothing
Dim gzipStream As GZipStream = Nothing
Try
'// Recreate source stream array
ReDim fsSourceStream(strSourceFile$.Length - 1)
'// Create destination and gzip streams
fsDestinationStream = New FileStream(strDestinationFile$, FileMode.OpenOrCreate, FileAccess.Write)
gzipStream = New GZipStream(fsDestinationStream, CompressionMode.Compress, True)
'// Loop through sources and write buffer
For intLoop As Integer = 0 To strSourceFile$.Length - 1
fsSourceStream(intLoop) = New FileStream(strSourceFile$(intLoop), FileMode.Open, FileAccess.Read, FileShare.Read)
bytBuffer = New Byte(CInt(fsSourceStream(intLoop).Length)) {}
Dim intCheckCounter As Integer = fsSourceStream(intLoop).Read(bytBuffer, 0, bytBuffer.Length)
gzipStream.Write(bytBuffer, 0, bytBuffer.Length)
Next
Catch ex As Exception
'// An exception has occured
MessageBox.Show("An exception has happened during compression." & vbCrLf & ex.Message, "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error)
Finally
'// Close all streams
If (Not fsSourceStream Is Nothing) Then
For intLoop As Integer = 0 To fsSourceStream.Length
fsSourceStream(intLoop).Close()
Next
End If
If (Not gzipStream Is Nothing) Then
gzipStream.Close()
End If
If (Not fsDestinationStream Is Nothing) Then
fsDestinationStream.Close()
End If
End Try
End Sub