扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
图1.填充表单:使用所有显示的控件填充默认的Form1。 |
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) |
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者