一个Zip压缩的类(VB.net)

本文介绍了一个使用VB.NET实现的文件压缩和解压缩工具类。该工具类支持通过GZipStream对单个文件进行压缩和解压缩操作。文章详细展示了如何读取源文件并将其压缩为目的地文件,以及如何将压缩文件还原为原始状态。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Imports System.IO
Imports System.IO.Compression

Public Class ZipUtil

Public Sub CompressFile(ByVal sourceFile As String, ByVal destinationFile As String)

' make sure the source file is there
If File.Exists(sourceFile) = False Then
Throw New FileNotFoundException
End If

' Create the streams and byte arrays needed
Dim buffer As Byte() = Nothing
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
buffer = New Byte(sourceStream.Length) {}
Dim checkCounter As Integer = sourceStream.Read(buffer, 0, buffer.Length)

' 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, 0, buffer.Length)

Catch ex As ApplicationException
MessageBox.Show(ex.Message, "An Error occured during compression", MessageBoxButtons.OK, MessageBoxIcon.Error)
Finally
' Make sure we allways close all streams
If Not (sourceStream Is Nothing) Then
sourceStream.Close()
End If
If Not (compressedStream Is Nothing) Then
compressedStream.Close()
End If
If Not (destinationStream Is 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 File.Exists(sourceFile) = False 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 As Byte() = Nothing

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
quartetBuffer = New Byte(4) {}
Dim position As Integer = CType(sourceStream.Length, Integer) - 4
sourceStream.Position = position
sourceStream.Read(quartetBuffer, 0, 4)
sourceStream.Position = 0
Dim checkLength As Integer = BitConverter.ToInt32(quartetBuffer, 0)

Dim buffer(checkLength + 100) As Byte
Dim offset As Integer = 0
Dim total As Integer = 0

' Read the compressed data into the buffer
While True
Dim bytesRead As Integer = decompressedStream.Read(buffer, offset, 100)
If bytesRead = 0 Then
Exit While
End If
offset += bytesRead
total += bytesRead
End While

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

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

Catch ex As ApplicationException
MessageBox.Show(ex.Message, "An Error occured during compression", MessageBoxButtons.OK, MessageBoxIcon.Error)
Finally
' Make sure we allways close all streams
If Not (sourceStream Is Nothing) Then
sourceStream.Close()
End If
If Not (decompressedStream Is Nothing) Then
decompressedStream.Close()
End If
If Not (destinationStream Is Nothing) Then
destinationStream.Close()
End If
End Try

End Sub

End Class
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值