VB   发布时间:2022-04-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了VB.net 2005内GZipStream类别可以针对档案做压缩、解压缩大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
[日期: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
@H_675_22@Imports System.IO
Imports System.IO.Compression

@H_675_22@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

@H_675_22@ Dim sourceStream As FileStream = Nothing
Dim desTinationStream As FileStream = Nothing
Dim compressedStream As GZipStream = Nothing

@H_675_22@ 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)

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

@H_675_22@ 'Open the FileStream to write to
desTinationStream = New FileStream(desTinationFile,FileMode.openOrCreate,FileAccess.WritE)

@H_675_22@ 'Create a compression stream poinTing to the destiantion stream
compressedStream = New GZipStream(desTinationStream,CompressionMode.Compress,TruE)

@H_675_22@ '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

@H_675_22@ If compressedStream Isnot Nothing Then
compressedStream.Close()
End If

@H_675_22@ if DesTinationStream Isnot Nothing Then
desTinationStream.Close()
End If
End Try
End Sub

@H_675_22@ 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

@H_675_22@ '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

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

@H_675_22@ 'Create a compression stream poinTing to the destiantion stream
decompressedStream = New GZipStream(sourceStream,CompressionMode.Decompress,TruE)

@H_675_22@ '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)

@H_675_22@ Dim buffer(checkLength + 100) As Byte
Dim offset,@R_186_10586@l As Integer
'Read the compressed data into the buffer
While (true)
Dim bytesRead As Integer = decompressedStream.Read(buffer,offset,100)

@H_675_22@ If bytesRead = 0 Then Exit While

@H_675_22@ offset += bytesRead
@R_186_10586@l += bytesRead
End While

@H_675_22@ 'Now write everything to the desTination file
desTinationStream = New FileStream(desTinationFile,FileMode.create)
desTinationStream.Write(buffer,@R_186_10586@l)

@H_675_22@ 'and flush everyhTing to clean out the buffer
desTinationStream.Flush()

@H_675_22@ 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

@H_675_22@ if DecompressedStream Isnot Nothing Then
decompressedStream.Close()
End If

@H_675_22@ if DesTinationStream Isnot Nothing Then desTinationStream.Close() End If End Try End Sub End Class

大佬总结

以上是大佬教程为你收集整理的VB.net 2005内GZipStream类别可以针对档案做压缩、解压缩全部内容,希望文章能够帮你解决VB.net 2005内GZipStream类别可以针对档案做压缩、解压缩所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。