VB.NET壓縮ZIP文件實際方式解析
作者:佚名
我們可以通過今天為大家介紹的這段代碼示例來對VB.NET壓縮ZIP文件這一應用技巧進行一個詳細的解讀,并對VB.NET有一個新的認識。
VB.NET這樣一款面向對象的編程語言,應用范圍非常廣泛,可以幫助開發人員打造一個穩定安全的開發框架。而且其對文件的處理方面也是非常強大的。這里就為大家介紹一下有關VB.NET壓縮ZIP文件的相關方法。#t#
VB.NET壓縮ZIP文件代碼示例:
- Public Function Decompress()
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 RetrieveBytes
FromStream()Function Retrieve
BytesFromStream( _ - 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
VB.NET壓縮ZIP文件的相關代碼編寫方式就為大家介紹到這里。
責任編輯:曹凱
來源:
博客園