加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

VB.net 2005内GZipStream类别可以针对档案做压缩、解压缩

发布时间:2020-12-16 23:34:09 所属栏目:大数据 来源:网络整理
导读:[日期:2007-11-08] 来源:互联网整理 作者:未知 [字体:大 中 小] 新闻简介: Imports System.IO Imports System.IO.Compression Public Class ClsZip Public Sub CompressFile(ByVal sourceFile As String,ByVal destinationFile As String) '压缩档案 If
[日期:2007-11-08] 来源:互联网整理 作者:未知 [字体:大 中 小]
新闻简介:Imports System.IO
Imports System.IO.Compression
Public Class ClsZip
Public Sub CompressFile(ByVal sourceFile As String,ByVal destinationFile As String)
'压缩档案
If Not File.Exists(sourceFile) Then
关 键 词: vb.net 压缩 gzip GZipStream 解压缩 VB.NET 2005

Imports System.IO
Imports System.IO.Compression

Public Class ClsZip
Public Sub CompressFile(ByVal sourceFile As String,ByVal destinationFile As String)
'压缩档案
If Not File.Exists(sourceFile) Then
Throw New FileNotFoundException
End If

Dim sourceStream As FileStream = Nothing
Dim destinationStream As FileStream = Nothing
Dim compressedStream As GZipStream = Nothing

Try
'Read the bytes from the source file into a byte array
sourceStream = New FileStream(sourceFile,FileMode.Open,FileAccess.Read,FileShare.Read)
'Read the source stream values into the buffer
Dim buffer(sourceStream.Length - 1) As Byte
Dim checkCounter As Integer = sourceStream.Read(buffer,buffer.Length)

If checkCounter <> buffer.Length Then
Throw New ApplicationException
End If

'Open the FileStream to write to
destinationStream = New FileStream(destinationFile,FileMode.OpenOrCreate,FileAccess.Write)

'Create a compression stream pointing to the destiantion stream
compressedStream = New GZipStream(destinationStream,CompressionMode.Compress,True)

'Now write the compressed data to the destination file
compressedStream.Write(buffer,buffer.Length)
Catch ex As ApplicationException
MessageBox.Show(ex.Message)
Finally
'Make sure we allways close all streams
If sourceStream IsNot Nothing Then
sourceStream.Close()
End If

If compressedStream IsNot Nothing Then
compressedStream.Close()
End If

If destinationStream IsNot Nothing Then
destinationStream.Close()
End If
End Try
End Sub

Public Sub DecompressFile(ByVal sourceFile As String,ByVal destinationFile As String)
'解压缩档案
'make sure the source file is there
If Not File.Exists(sourceFile) Then
Throw New FileNotFoundException
End If

'Create the streams and byte arrays needed
Dim sourceStream As FileStream = Nothing
Dim destinationStream As FileStream = Nothing
Dim decompressedStream As GZipStream = Nothing
Dim quartetBuffer(4) As Byte

Try
'Read in the compressed source stream
sourceStream = New FileStream(sourceFile,FileMode.Open)

'Create a compression stream pointing to the destiantion stream
decompressedStream = New GZipStream(sourceStream,CompressionMode.Decompress,True)

'Read the footer to determine the length of the destiantion file
Dim position As Integer = sourceStream.Length - 4
sourceStream.Position = position
sourceStream.Read(quartetBuffer,4)
sourceStream.Position = 0
Dim checkLength As Integer = BitConverter.ToInt32(quartetBuffer,0)

Dim buffer(checkLength + 100) As Byte
Dim offset,total As Integer
'Read the compressed data into the buffer
While (True)
Dim bytesRead As Integer = decompressedStream.Read(buffer,offset,100)

If bytesRead = 0 Then Exit While

offset += bytesRead
total += bytesRead
End While

'Now write everything to the destination file
destinationStream = New FileStream(destinationFile,FileMode.Create)
destinationStream.Write(buffer,total)

'and flush everyhting to clean out the buffer
destinationStream.Flush()

Catch ex As ApplicationException
MessageBox.Show(ex.Message)
Finally
'Make sure we allways close all streams
If sourceStream IsNot Nothing Then
sourceStream.Close()
End If

If decompressedStream IsNot Nothing Then
decompressedStream.Close()
End If

If destinationStream IsNot Nothing Then destinationStream.Close() End If End Try End Sub End Class

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读