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


2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36


37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

转载于:https://www.cnblogs.com/desmend/archive/2008/07/29/1298622.html