處理壓縮事件
現(xiàn)在,既然定義好了主要的壓縮和解壓例程,那么接下來你就可以為各種按鈕進行編碼了。相應于Compress按鈕的事件處理器如下:
Private Sub BTnCompress_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnCompress.Click
'---用來存儲壓縮的數(shù)據(jù)---
Dim compressedData() As Byte
'---壓縮數(shù)據(jù)---
If rbGZipStream.Checked Then
compressedData = Compress("Gzip",System.Text.Encoding.ASCII.GetBytes(txtBefore.Text))
Else
compressedData = Compress("Deflate",System.Text.Encoding.ASCII.GetBytes(txtBefore.Text))
End If
'---把壓縮的數(shù)據(jù)復制到一個字符串中---
Dim i As Integer
Dim s As New System.Text.StringBuilder()
For i = 0 To compressedData.Length - 1
If i <> compressedData.Length - 1 Then
s.Append(compressedData(i) & " ")
Else
s.Append(compressedData(i))
End If
Next
'---顯示壓縮的數(shù)據(jù)為一個字符串---
txtAfter.Text = s.ToString
End Sub
在txtBefore控件中的數(shù)據(jù)被轉換成一個字節(jié)數(shù)組,然后被壓縮。然后,該壓縮的數(shù)據(jù)被轉換成字符串以便于在txtAfter中顯示。
相應于Decompress按鈕的事件處理器如下:
Private Sub btnDecompress_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnDecompress.Click
'---把壓縮的字符串格式化成一個字節(jié)數(shù)組---
Dim eachbyte() As String = txtAfter.Text.Split(" ")
Dim data(eachbyte.Length - 1) As Byte
For i As Integer = 0 To eachbyte.Length - 1
data(i) = Convert.ToByte(eachbyte(i))
Next
'---解壓數(shù)據(jù)并且顯示解壓的數(shù)據(jù)---
If rbGZipStream.Checked Then
txtBefore.Text = System.Text.Encoding.ASCII.GetString(Decompress("Gzip", data))
Else
txtBefore.Text = System.Text.Encoding.ASCII.GetString(Decompress("Deflate", data))
End If
End Sub
它把顯示在控件txtAfter中的數(shù)據(jù)轉換成一個字節(jié)數(shù)組,然后發(fā)送它以便進行解壓。解壓縮的數(shù)據(jù)被顯示回txtBefore控件中。
相應于"Select file to compress"按鈕的事件處理器代碼如下:
Private Sub btnSelectFile_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnSelectFile.Click
'---讓用戶選擇一個要壓縮的文件--
Dim openFileDialog1 As New OpenFileDialog()
'openFileDialog1.InitialDirectory = "c:/"
openFileDialog1.Filter = "All files (*.*)*.*"
openFileDialog1.RestoreDirectory = True
If openFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
'---把文件的內(nèi)容讀入字節(jié)數(shù)組---
Dim fileContents As Byte()
fileContents = My.Computer.FileSystem.ReadAllBytes(openFileDialog1.FileName)
'---創(chuàng)建gzip文件---
Dim filename As String = openFileDialog1.FileName & ".gzip"
If File.Exists(filename) Then File.Delete(filename)
Dim fs As FileStream = New FileStream(filename, FileMode.CreateNew, File
access.Write)
'---壓縮文件的內(nèi)容---
Dim compressed_Data As Byte()
If rbGZipStream.Checked Then
compressed_Data = Compress("Gzip", fileContents)
Else
compressed_Data = Compress("Deflate", fileContents)
End If
If compressed_Data IsNot Nothing Then
'---把壓縮的內(nèi)容寫進壓縮的文件中---
fs.Write(compressed_Data, 0, compressed_Data.Length)
fs.Close()
End If
End If
End Sub
它讀取由用戶選擇的文件的內(nèi)容,壓縮它,并且創(chuàng)建一個包含壓縮的數(shù)據(jù)的新文件(具有一樣的文件名,但是加上了一個.gzip擴展名)。
相應于"Select file to Decompress"按鈕的事件處理器代碼如下:
Private Sub btnDecompressFile_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnDecompressFile.Click
'---讓用戶選擇一個要解壓的文件---
Dim openFileDialog1 As New OpenFileDialog()
' openFileDialog1.InitialDirectory = "c:/"
openFileDialog1.Filter = "All GZIP files (*.gzip)*.gzip"
openFileDialog1.RestoreDirectory = True
If openFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
'---把壓縮的文件的內(nèi)容讀入到字節(jié)數(shù)組---
Dim fileContents As Byte()
fileContents = My.Computer.FileSystem.ReadAllBytes(openFileDialog1.FileName)
'---解壓文件的內(nèi)容---
Dim uncompressed_Data As Byte()
If rbGZipStream.Checked Then
uncompressed_Data = Decompress("Gzip", fileContents)
Else
uncompressed_Data = Decompress("Deflat", fileContents)
End If
'---創(chuàng)建解壓的文件---
Dim filename As String = openFileDialog1.FileName.Substring(0, openFileDialog1.FileName.Length - 5)
If File.Exists(filename) Then File.Delete(filename)
Dim fs As FileStream = New FileStream(filename,F(xiàn)ileMode.CreateNew, FileAccess.Write)
If uncompressed_Data IsNot Nothing Then
'---把解壓內(nèi)容寫入到文件中---
fs.Write(uncompressed_Data, 0, uncompressed_Data.Length)
fs.Close()
End If
End If
End Sub
它讀取用戶選擇的文件的內(nèi)容,解壓之,并且創(chuàng)建一個包含解壓的數(shù)據(jù)的新文件(通過去掉它的.gzip擴展名)。進入討論組討論。
測試應用程序
按F5測試應用程序(見圖2)。

圖2.測試應用程序:選擇使用的壓縮算法,然后你可以壓縮一個文本串或一個文件內(nèi)容。
你應該注重下列事實:
· 壓縮小數(shù)量的文本實際上將會導致一種較大的壓縮文本。
· 不同的文本將產(chǎn)生不同的壓縮比,盡管字符數(shù)是固定的。
· 文本文件壓縮效果最好;它們能夠帶來最好的壓縮比。
· 其它二進制的文件,例如.exe,jpg,通常壓縮效果并不很好并且可能會導致大于百分比之100的壓縮比,這是沒有價值的。
需要注重的是,.NET中的GZIP和Deflate算法的實現(xiàn)要比市場上的其它第三方GZIP工具具有較低的效率。盡管你能夠使用.NET類把10MB的數(shù)據(jù)壓縮到4MB,但是你發(fā)現(xiàn)使用一種第三方工具可能會達到一種更小的壓縮大小。另外,這個壓縮類無法操作大于4GB的數(shù)據(jù)。然而,在.NET中的實現(xiàn)將答應你解壓使用市場中的其它GZIP工具壓縮的所有的文件。
小結
在本文中,你已經(jīng)看到了如何在.NET 2.0中使用壓縮類。盡管這種實現(xiàn)還不如市場上的那些非MS方案有效,但是它的確為你提供了一種輕易(免費)的方式來在你的.NET應用程序中加入壓縮功能。進入討論組討論。