站内搜索:     
站点首页破釜学院编程开发.NETASP.NET → 用.NET 2.0压缩/解压功能处理大型数据
正在加载相关信息.....
Web 站内搜索
用.NET 2.0压缩/解压功能处理大型数据
】【打印】【加入收藏】【关闭收藏到新浪ViVi】【收藏到365KEY】 浏览字号:
日期:2006-07-17 人气: 出处:天极网

处理压缩事件

现在,既然定义好了主要的压缩和解压例程,那么接下来你就可以为各种按钮进行编码了。相应于Compress按钮的事件处理器如下:

Private Sub btnCompress_Click(ByVal sender As System.Object, _

ByVal e As System.EventArgs) Handles btnCompress.Click

'---用来存储压缩的数据---

Dim compressedData() As Byte

'---压缩数据---

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

'---把压缩的数据复制到一个字符串中---

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

'---显示压缩的数据为一个字符串---

txtAfter.Text = s.ToString

End Sub

在txtBefore控件中的数据被转换成一个字节数组,然后被压缩。然后,该压缩的数据被转换成字符串以便于在txtAfter中显示。

相应于Decompress按钮的事件处理器如下:

Private Sub btnDecompress_Click(ByVal sender As System.Object, _

ByVal e As System.EventArgs) Handles btnDecompress.Click

'---把压缩的字符串格式化成一个字节数组---

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

'---解压数据并且显示解压的数据---

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中的数据转换成一个字节数组,然后发送它以便进行解压。解压缩的数据被显示回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

'---把文件的内容读入字节数组---

Dim fileContents As Byte()

fileContents = My.Computer.FileSystem.ReadAllBytes(openFileDialog1.FileName)

'---创建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, FileAccess.Write)

'---压缩文件的内容---

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

'---把压缩的内容写进压缩的文件中---

fs.Write(compressed_Data, 0, compressed_Data.Length)

fs.Close()

End If

End If

End Sub

它读取由用户选择的文件的内容,压缩它,并且创建一个包含压缩的数据的新文件(具有一样的文件名,但是加上了一个.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

'---把压缩的文件的内容读入到字节数组---

Dim fileContents As Byte()

fileContents = My.Computer.FileSystem.ReadAllBytes(openFileDialog1.FileName)

'---解压文件的内容---

Dim uncompressed_Data As Byte()

If rbGZipStream.Checked Then

uncompressed_Data = Decompress("Gzip", fileContents)

Else

uncompressed_Data = Decompress("Deflat", fileContents)

End If

'---创建解压的文件---

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,FileMode.CreateNew, FileAccess.Write)

If uncompressed_Data IsNot Nothing Then

'---把解压内容写入到文件中---

fs.Write(uncompressed_Data, 0, uncompressed_Data.Length)

fs.Close()

End If

End If

End Sub

它读取用户选择的文件的内容,解压之,并且创建一个包含解压的数据的新文件(通过去掉它的.gzip扩展名)。

>>>> 进入论坛交流 <<<<

相关文章:
暂时没有相关文章