How to decompress File
Following code explain how to decompress file
Following code explain how to decompress file
Public Sub DecompressGivenFile(ByVal srcFile As String, ByVal destFile As String)
Dim sourceStream As FileStream = Nothing
Dim destinationStream As FileStream = Nothing
Dim decompressedStream As GZipStream = Nothing
Dim quartetBuffer As Byte() = Nothing
Try
sourceStream = New FileStream(sourceFile, FileMode.Open)
decompressedStream = New GZipStream(sourceStream, CompressionMode.Decompress, True)
quartetBuffer = New Byte(4) {}
Dim position As Integer = CType(sourceStream.Length, Integer) - 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 As Integer = 0
Dim total As Integer = 0
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
destinationStream = New FileStream(destinationFile, FileMode.Create)
destinationStream.Write(buffer, 0, total)
destinationStream.Flush()
Catch ex As ApplicationException
MessageBox.Show(ex.Message, "An Error occured during compression", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
