PDA

View Full Version : سوال: توضيح كد unzip



morva14
چهارشنبه 09 بهمن 1387, 11:47 صبح
كسي ميتونه اين كد و كامل توضيح بد؟



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

' 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, 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

' 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 sourceStream IsNot Nothing Then
sourceStream.Close()
End If
If decompressedStream IsNot Nothing Then
decompressedStream.Close()
End If
If destinationStream IsNot Nothing Then
destinationStream.Close()
End If
End Try

End Sub
End Class
End Namespace