Tutorials Forums
     Tutorials Videos
        Sign Up Now For FREE
Welcome, Guest
Username Password: Remember me

Decompress file with GZipStream class
(1 viewing) (1) Guest
VB.NET HELP, VB.NET TUTORIALS, VB.NET CODE, VB.NET PROGRAMMING, VB.NET

Visual Basic.NET is the most recent generation of Visual Basic. Developers will be pleased to note that its new features include inheritance, method overloading, structured exception handling, and more. These capabilities make it easier than ever to create .NET applications, including Windows applications, web services, and web applications. The articles in this section give you all the tips you need to work wit this useful language.
  • Page:
  • 1

TOPIC: Decompress file with GZipStream class

Decompress file with GZipStream class 09 Dec 2009 15:34 #73

Function for decompressing a compressed file using the GZipStream class

Instructions: Pass the function the file (with path), the destination file and destination directory. Need a reference to the System.IO Namespace

 
''' <summary>
'
''Function to decompress a file using the GZipStream Class
''' </summary>
'
'' <param name="inputFileName">File that we want to decompress</param>
''' <param name="destFileName">Name we want the decompressed file to be</param>
'
'' <param name="destDirectory">Directory to save the file to</param>
''' <returns>True/False</returns>
'
'' <remarks></remarks>
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
  • CE
  • OFFLINE
  • Administrator
  • Posts: 197
  • Karma: 78
CodersEngine
  • Page:
  • 1
Time to create page: 0.46 seconds