VB.NET压缩ZIP文件实际方式解析

开发 后端
我们可以通过今天为大家介绍的这段代码示例来对VB.NET压缩ZIP文件这一应用技巧进行一个详细的解读,并对VB.NET有一个新的认识。

VB.NET这样一款面向对象的编程语言,应用范围非常广泛,可以帮助开发人员打造一个稳定安全的开发框架。而且其对文件的处理方面也是非常强大的。这里就为大家介绍一下有关VB.NET压缩ZIP文件的相关方法。#t#

VB.NET压缩ZIP文件代码示例:

  1. Public Function Decompress()
    Function Decompress
    (ByVal algo As String, ByVal 
    data() As Byte) As Byte()  
  2. Try  
  3. Dim sw As New Stopwatch  
  4. '---复制数据(压缩的)到ms---  
  5. Dim ms As New MemoryStream(data)  
  6. Dim zipStream As Stream = Nothing 
  7. '---开始秒表---  
  8. sw.Start()  
  9. '---使用存储在ms中的数据解压---  
  10. If algo = "Gzip" Then  
  11. zipStream = New GZipStream(ms, 
    CompressionMode.Decompress)  
  12. ElseIf algo = "Deflate" Then  
  13. zipStream = New DeflateStream(ms, 
    CompressionMode.Decompress, True)  
  14. End If  
  15. '---用来存储解压的数据---  
  16. Dim dc_data() As Byte  
  17. '---解压的数据存储于zipStream中;   
  18. '把它们提取到一个字节数组中---  
  19. dc_data = RetrieveBytesFromStream
    (zipStream, data.Length)  
  20. '---停止秒表---  
  21. sw.Stop()  
  22. lblMessage.Text = "Decompression 
    completed. Time spent: "
     & sw.
    ElapsedMilliseconds & "ms" & _  
  23. ", Original size: " & dc_data.Length  
  24. Return dc_data  
  25. Catch ex As Exception  
  26. MsgBox(ex.ToString)  
  27. Return Nothing  
  28. End Try  
  29. End Function  
  30. Public Function RetrieveBytes
    FromStream()Function Retrieve
    BytesFromStream( _  
  31. ByVal stream As Stream, ByVal 
    bytesblock As Integer) As Byte()  
  32. '---从一个流对象中检索字节---  
  33. Dim data() As Byte  
  34. Dim totalCount As Integer = 0 
  35. Try  
  36. While True  
  37. '---逐渐地增加数据字节数组-的大小--  
  38. ReDim Preserve data(totalCount 
    + bytesblock)  
  39. Dim bytesRead As Integer = 
    stream.Read(data, totalCount, bytesblock)  
  40. If bytesRead = 0 Then  
  41. Exit While  
  42. End If  
  43. totalCount += bytesRead  
  44. End While  
  45. '---确保字节数组正确包含提取的字节数---  
  46. ReDim Preserve data(totalCount - 1)  
  47. Return data  
  48. Catch ex As Exception  
  49. MsgBox(ex.ToString)  
  50. Return Nothing  
  51. End Try  
  52. End Function 

VB.NET压缩ZIP文件的相关代码编写方式就为大家介绍到这里。

责任编辑:曹凯 来源: 博客园
相关推荐

2010-01-07 16:16:03

VB.NET变量作用域

2010-01-11 10:44:47

VB.NET多窗体

2010-01-07 17:57:22

VB.NET构造函数

2010-01-19 13:36:16

VB.NET可选参数

2010-01-11 17:40:36

VB.NET相框效果

2010-01-11 11:37:08

VB.NET操作CSV

2010-01-15 15:03:23

VB.NET对象变量声

2010-01-20 17:41:13

VB.NET标记语句

2009-10-21 09:19:40

VB.NET文件压缩 

2010-01-12 18:12:58

VB.NET事件

2009-11-02 11:02:58

VB.NET XML文

2010-01-18 15:43:35

VB.NET自定义属性

2009-10-09 15:59:41

VB.NET对象

2010-01-15 16:46:05

VB.NET集合存储

2009-11-02 14:48:45

VB.NET HOOK

2010-01-08 10:37:50

VB.NET数据库

2010-01-07 18:17:00

VB.NET连接SAP

2010-01-07 18:22:40

VB.NET声音播放

2009-10-21 09:10:52

VB.NET压缩

2010-01-14 17:41:57

VB.NET变量范围
点赞
收藏

51CTO技术栈公众号