VB icon

Compression / decompression : intrinsic to VB.Net 2005

Email
Submitted on: 1/3/2015 5:12:00 PM
By: George E. (from psc cd)  
Level: Beginner
User Rating: By 6 Users
Compatibility: VB.NET
Views: 3027
 
     Zip Rar Tga etc intrinsic compression and decompression, extremely fast, 1.09mb -> 17kb in less than a second, not my code, as it is plainly just a part of the .net framework
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
//**************************************
// Name: Compression / decompression : intrinsic to VB.Net 2005
// Description:Zip Rar Tga etc intrinsic compression and decompression, extremely fast, 1.09mb -> 17kb in less than a second, not my code, as it is plainly just a part of the .net framework
// By: George E. (from psc cd)
//**************************************

Imports System.IO
Imports System.IO.Compression
Public Class ZipUtil
 Public Sub CompressFile(ByVal sourceFile As String, ByVal destinationFile As String)
' make sure the source file is there
If File.Exists(sourceFile) = False Then
 Throw New FileNotFoundException
End If
' Create the streams and byte arrays needed
Dim buffer As Byte() = Nothing
Dim sourceStream As FileStream = Nothing
Dim destinationStream As FileStream = Nothing
Dim compressedStream As GZipStream = Nothing
Try
 ' Read the bytes from the source file into a byte array
 sourceStream = New FileStream(sourceFile, FileMode.Open, FileAccess.Read, FileShare.Read)
 ' Read the source stream values into the buffer
 buffer = New Byte(CInt(sourceStream.Length)) {}
 Dim checkCounter As Integer = sourceStream.Read(buffer, 0, buffer.Length)
 ' Open the FileStream to write to
 destinationStream = New FileStream(destinationFile, FileMode.OpenOrCreate, FileAccess.Write)
 ' Create a compression stream pointing to the destiantion stream
 compressedStream = New GZipStream(destinationStream, CompressionMode.Compress, True)
 'Now write the compressed data to the destination file
 compressedStream.Write(buffer, 0, buffer.Length)
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 Not (sourceStream Is Nothing) Then
sourceStream.Close()
 End If
 If Not (compressedStream Is Nothing) Then
compressedStream.Close()
 End If
 If Not (destinationStream Is Nothing) Then
destinationStream.Close()
 End If
End Try
 End Sub
 Public Sub DecompressFile(ByVal sourceFile As String, ByVal destinationFile As String)
' make sure the source file is there
If File.Exists(sourceFile) = False 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 Not (sourceStream Is Nothing) Then
sourceStream.Close()
 End If
 If Not (decompressedStream Is Nothing) Then
decompressedStream.Close()
 End If
 If Not (destinationStream Is Nothing) Then
destinationStream.Close()
 End If
End Try
 End Sub
End Class


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.