To test the above mentioned functions, create a windows application
in VB 2005 and add two text box controls on the form. Name these as txtFilePath and txtDestainationFolder.
Add two labels for these text boxes and change their Text property as "File
to Compress/Decompress" and "Destination Folder."
Add two command buttons and name these as btnCompress and btnDecompress and
change its Text property to "Compress file" and "Decompress
Zipped file" respectively.
Add two more command buttons with name "btnBrowseFile"
and " btnBrowseFolder." Specify Text property as "Browse"
for both.
Listing 6 – Compression and Decompression windows
form snap shot
Add a class files CComp.vb in the
application and implement the compression function CompressByte
as given in Listing1 above. Add one more class file CDcomp.vb and implement the
decompression function DecompressByte as given in Listing 3.
Implement click event of btnCompress.
Listing 7 –Click event implementation of button btnCompress
Private Sub btnCompress_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles btnCompress.Click
Dim byteSource() As Byte
Dim byteCompressed() As Byte
Dim oCCompression As New CComp
' Validate paths
If IO.File.Exists(txtFilePath.Text) = False Then
MsgBox("Please specify the valid file path to compress", MsgBoxStyle.OkOnly)
txtFilePath.Focus()
Exit Sub
Else
If IO.Directory.Exists(txtDestainationFolder.Text) = False Then
MsgBox("Please specify the valid path of destination folder", MsgBoxStyle.OkOnly)
txtDestainationFolder.Focus()
Exit Sub
End If
End If
Try
Dim sFileName As String = txtFilePath.Text.Substring(txtFilePath.Text.LastIndexOf("\") + 1)
byteSource = System.IO.File.ReadAllBytes(txtFilePath.Text)
byteCompressed = oCCompression.CompressByte(byteSource)
System.IO.File.WriteAllBytes(txtDestainationFolder.Text & "\" & sFileName & ".zip", byteCompressed)
MsgBox("File compressed successfully and placed in destination folder", _
MsgBoxStyle.OkOnly, "Compression")
Catch ex As Exception
MsgBox("Compression failed. Reason: " & ex.ToString())
End Try
End Sub
Description
txtFilePath.Text.Substring(txtFilePath.Text.LastIndexOf("\")
+ 1) returns the file name which we use to assign name to the
compressed file later. As I have mentioned before, filename or file extension
do not have much importance in these compression. We can assign name as we
desire.
·
byteSource = System.IO.File.ReadAllBytes(txtFilePath.Text)
gets the byte array of the file.
·
byteCompressed =
oCCompression.CompressByte(byteSource) gets the compressed byte array by
using CompressByte() function.
·
System.IO.File.WriteAllBytes(txtDestainationFolder.Text
& "\" & sFileName & ".zip", byteCompressed) writes
compressed byte array into <filename>.zip file. As I discussed before we
can use any extension for the compressed file. But saving in .zip extension
makes it to be decompressed by using WinZip software also.
Implement click event of btnDecompress:
Listing 8 –Click event implementation of button btnDecompress
Private Sub btnDecompress_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles btnDecompress.Click
Dim oCDecompression As New CDecomp
Dim byteDecompressed() As Byte
' Validate paths
If IO.File.Exists(txtFilePath.Text) = False Then
MsgBox("Please specify the valid file path to decompress", MsgBoxStyle.OkOnly)
txtFilePath.Focus()
Exit Sub
Else
If IO.Directory.Exists(txtDestainationFolder.Text) = False Then
MsgBox("Please specify the valid path of destination folder", MsgBoxStyle.OkOnly)
txtDestainationFolder.Focus()
Exit Sub
End If
End If
Try
Dim sFileName As String = txtFilePath.Text.Substring(txtFilePath.Text.LastIndexOf("\") + 1)
sFileName = sFileName.Remove(sFileName.LastIndexOf("."))
byteDecompressed = oCDecompression.DecompressByte(System.IO.File.ReadAllBytes(txtFilePath.Text))
System.IO.File.WriteAllBytes(txtDestainationFolder.Text & "\" & sFileName, byteDecompressed)
MsgBox("File decompressed successfully and placed in destination folder", _
MsgBoxStyle.OkOnly, "Decompression")
Catch ex As Exception
MsgBox("Decompression failed. Reason: File used to decompress may not have gzip compression format")
End Try
End Sub
Description
Dim sFileName As String =
txtFilePath.Text.Substring(txtFilePath.Text.LastIndexOf("\") + 1) reads
the filename to write into the decompressed byte.
·
sFileName =
sFileName.Remove(sFileName.LastIndexOf(".")) removes the .zip
extension.
·
byteDecompressed = oCDecompression.DecompressByte(System.IO.File.ReadAllBytes(
txtDestainationFolder.Text & "\" & sFileName &
".zip")) gets the decompressed byte array by using DecompressByte() function. This function takes the path of
compressed file.
·
System.IO.File.WriteAllBytes(txtDestainationFolder.Text
& "\" & sFileName, byteDecompressed) writes the
decompressed byte array into file.
Build the application and run it. If run successfully then on
the form windows specify the file to be compressed and folder location in the
text box by using browse buttons given for that. Test the compression and
decompression functionality of the application. You should get the output file
in the destination folder which you set on the form. Verify the difference in
file sizes of actual and compressed files. I will suggest to you to read the
"Limitations of GZipStream and DeflateStream in Framework 2.0"
section in this article before going to test application with different types
and sizes of files to find ratio of compression.
In the above windows application example I have used byte
array to byte array compression. In real life situations these functions are also
useful when you need to apply compression with memory to memory data transfer
over network where reading or writing files is not necessarily required. You
might need to apply compression on xml format dataset collected from database
at one end and to apply decompression at other end to get back the actual xml
dataset. Here you can use functions CompressByte and DecompressByte defined above.
We can also implement byte array to memory stream
compression by replacing CompressByte() with function
CompressData() as defined in Listing 2 and/or
replacing function DecompressByte() with DecompressData() as defined in
Listing 4.