Click here to Skip to main content
15,893,381 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
VB
Public Function DecompressFile(ByRef inputFileName As String, ByRef destFileName As String, ByRef destDirectory As String) As Boolean
    Try
        'Create a MemoryStream from the file bytes
        Dim stream As New MemoryStream(File.ReadAllBytes(inputFileName))

        'Create a new GZipStream from the MemoryStream
        Dim gZip As New GZipStream(stream, CompressionMode.Decompress)

        'Byte array to hold bytes
        Dim buffer(3) As Byte

        'Read the stream
        stream.Position = stream.Length - 5
        stream.Read(buffer, 0, 4)

        'Calculate the size of the decompressed bytes
        Dim size As Integer = BitConverter.ToInt32(buffer, 0)

        'Start at the beginning of the stream
        stream.Position = 0

        Dim decompressed(size - 1) As Byte

        'Read decompressed bytes into byte array
        gZip.Read(decompressed, 0, size)

        'Close & clean up
        gZip.Dispose()
        stream.Dispose()

        'Write the final file
        File.WriteAllBytes(destDirectory & "\" & destFileName, decompressed)

        Return True
    Catch ex As Exception
        MessageBox.Show(ex.ToString())
        Return False
    End Try
End Function
Posted
Updated 17-Nov-11 22:30pm
v2
Comments
CPallini 18-Nov-11 4:29am    
Please elaborate.
Mehdi Gholam 18-Nov-11 4:31am    
EDIT -> fixed formatting

1 solution

The reason your byte array has a fixed size is simply because you define it with a constant. It is true that dynamically expanding (preserving the data) isn't all that easy to do but in your case you know the size needed at the point you are declaring the array.

Have a look at the link. In the code example a buffer is declared and initialized when the necessary size is known.
http://victormaceda.wordpress.com/2007/07/18/compress-and-decompress-file-class-utility-in-vb-net/[^]

Good luck!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900