VB.NET壓縮ZIP文件實(shí)際方式解析
作者:佚名
我們可以通過(guò)今天為大家介紹的這段代碼示例來(lái)對(duì)VB.NET壓縮ZIP文件這一應(yīng)用技巧進(jìn)行一個(gè)詳細(xì)的解讀,并對(duì)VB.NET有一個(gè)新的認(rèn)識(shí)。
VB.NET這樣一款面向?qū)ο蟮木幊陶Z(yǔ)言,應(yīng)用范圍非常廣泛,可以幫助開(kāi)發(fā)人員打造一個(gè)穩(wěn)定安全的開(kāi)發(fā)框架。而且其對(duì)文件的處理方面也是非常強(qiáng)大的。這里就為大家介紹一下有關(guān)VB.NET壓縮ZIP文件的相關(guān)方法。#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
- '---復(fù)制數(shù)據(jù)(壓縮的)到ms---
- Dim ms As New MemoryStream(data)
- Dim zipStream As Stream = Nothing
- '---開(kāi)始秒表---
- sw.Start()
- '---使用存儲(chǔ)在ms中的數(shù)據(jù)解壓---
- If algo = "Gzip" Then
- zipStream = New GZipStream(ms,
CompressionMode.Decompress) - ElseIf algo = "Deflate" Then
- zipStream = New DeflateStream(ms,
CompressionMode.Decompress, True) - End If
- '---用來(lái)存儲(chǔ)解壓的數(shù)據(jù)---
- Dim dc_data() As Byte
- '---解壓的數(shù)據(jù)存儲(chǔ)于zipStream中;
- '把它們提取到一個(gè)字節(jié)數(shù)組中---
- 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() - '---從一個(gè)流對(duì)象中檢索字節(jié)---
- Dim data() As Byte
- Dim totalCount As Integer = 0
- Try
- While True
- '---逐漸地增加數(shù)據(jù)字節(jié)數(shù)組-的大小--
- 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
- '---確保字節(jié)數(shù)組正確包含提取的字節(jié)數(shù)---
- ReDim Preserve data(totalCount - 1)
- Return data
- Catch ex As Exception
- MsgBox(ex.ToString)
- Return Nothing
- End Try
- End Function
VB.NET壓縮ZIP文件的相關(guān)代碼編寫方式就為大家介紹到這里。
責(zé)任編輯:曹凱
來(lái)源:
博客園