by Gehan Fernando (47 Submissions)
Category: Files/File Controls/Input/Output
Compatability: VB.NET
Difficulty: Unknown Difficulty
Originally Published: Tue 15th April 2008
Date Added: Mon 8th February 2021
Rating: (1 Votes)
Compress And Decompress File Using GZipStream
API Declarations
.IO
.IO.Compression
.IO.Compression.GZipStream
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim sourceFile As FileStream = File.OpenRead("D:\Gehan.txt")
Dim destFile As FileStream = File.Create("D:\Gehan.gz")
Dim compStream As New GZipStream(destFile, CompressionMode.Compress)
Dim theByte As Integer = sourceFile.ReadByte()
While theByte <> -1
compStream.WriteByte(CType(theByte, Byte))
theByte = sourceFile.ReadByte()
End While
compStream.Flush() : compStream.Close() : compStream.Dispose()
destFile.Close() : destFile.Dispose()
sourceFile.Close() : sourceFile.Dispose()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim sourceFile As FileStream = File.OpenRead("D:\Gehan.gz")
Dim destFile As FileStream = File.Create("D:\Gehan.txt")
Dim compStream As New GZipStream(sourceFile, CompressionMode.Decompress, True)
Dim theByte As Integer = compStream.ReadByte()
While theByte <> -1
destFile.WriteByte(CType(theByte, Byte))
theByte = compStream.ReadByte()
End While
compStream.Flush() : compStream.Close() : compStream.Dispose()
destFile.Close() : destFile.Dispose()
sourceFile.Close() : sourceFile.Dispose()
End Sub
End Class