Imports System.IO Imports System.IO.Compression |
Public Function Compress(ByVal algo As String, ByVal data() As Byte) As Byte() Try Dim sw As New Stopwatch '---ms用于存储压缩的数据--- Dim ms As New MemoryStream() Dim zipStream As Stream = Nothing '---开始秒表计时--- sw.Start() If algo = "Gzip" Then zipStream = New GZipStream(ms, CompressionMode.Compress, True) ElseIf algo = "Deflate" Then zipStream = New DeflateStream(ms, CompressionMode.Compress, True) End If '---使用存储在数据中的信息进行压缩--- zipStream.Write(data, 0, data.Length) zipStream.Close() '---停止秒表--- sw.Stop() '---计算压缩比--- Dim ratio As Single = Math.Round((ms.Length / data.Length) * 100, 2) Dim msg As String = "Original size: " & data.Length & _ ", Compressed size: " & ms.Length & _ ", 压缩比: " & ratio & "%" & _ ", Time spent: " & sw.ElapsedMilliseconds & "ms" lblMessage.Text = msg ms.Position = 0 '---用来存储压缩了的数据(字节数组)--- Dim c_data(ms.Length - 1) As Byte '---把内存流的内容读取到字节数组--- ms.Read(c_data, 0, ms.Length) Return c_data Catch ex As Exception MsgBox(ex.ToString) Return Nothing End Try End Function |
Public Function Decompress(ByVal algo As String, ByVal data() As Byte) As Byte() Try Dim sw As New Stopwatch '---复制数据(压缩的)到ms--- Dim ms As New MemoryStream(data) Dim zipStream As Stream = Nothing '---开始秒表--- sw.Start() '---使用存储在ms中的数据解压--- If algo = "Gzip" Then zipStream = New GZipStream(ms, CompressionMode.Decompress) ElseIf algo = "Deflate" Then zipStream = New DeflateStream(ms, CompressionMode.Decompress, True) End If '---用来存储解压的数据--- Dim dc_data() As Byte '---解压的数据存储于zipStream中; '把它们提取到一个字节数组中--- dc_data = RetrieveBytesFromStream(zipStream, data.Length) '---停止秒表--- sw.Stop() lblMessage.Text = "Decompression completed. Time spent: " & _ sw.ElapsedMilliseconds & "ms" & _ ", Original size: " & dc_data.Length Return dc_data Catch ex As Exception MsgBox(ex.ToString) Return Nothing End Try End Function |
Public Function RetrieveBytesFromStream( _ ByVal stream As Stream, ByVal bytesblock As Integer) As Byte() '---从一个流对象中检索字节--- Dim data() As Byte Dim totalCount As Integer = 0 Try While True '---逐渐地增加数据字节数组-的大小-- ReDim Preserve data(totalCount + bytesblock) Dim bytesRead As Integer = stream.Read(data, totalCount, bytesblock) If bytesRead = 0 Then Exit While End If totalCount += bytesRead End While '---确保字节数组正确包含提取的字节数--- ReDim Preserve data(totalCount - 1) Return data Catch ex As Exception MsgBox(ex.ToString) Return Nothing End Try End Function |
dc_data = RetrieveBytesFromStream(zipStream, data.Length) |
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 |
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 |
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 |
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 |
![]() 图2.测试应用程序:选择使用的压缩算法,然后你可以压缩一个文本串或一个文件内容。 |
本文出自 “青峰” 博客,转载请与作者联系!