`
izuoyan
  • 浏览: 8959560 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

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

阅读更多
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
分享到:
评论

相关推荐

    VB.NET编写的压缩与解压函数

    用VB.NET编写的压缩与解压函数,此函数可对保存成二进制数据的WORD文档内容进行很大的压缩,使系统读取大容量数据的速度大大地加快。

    VB.NET使用ZipPackage实现ZIP文件压缩解压缩示例

    VB.NET使用ZipPackage实现ZIP文件压缩解压缩示例

    VB.NET操作SQL数据库.zip

    VB.NET操作SQL数据库.zip

    .net采用SharpZipLib压缩文件夹及文件(类及库文件)

    详细的描述了采用SharpZipLib压缩文件夹及文件,并提供了可用的Dll文件

    dotnet数据压缩组件包

    dotnet 数据解压缩 压缩 c# vb.net 组件包 dotnet 数据解压缩 压缩 c# vb.net 组件包 dotnet 数据解压缩 压缩 c# vb.net 组件包 dotnet 数据解压缩 压缩 c# vb.net 组件包

    .net中的Zip类

    这一个用于zip压缩.net类库,有1.0,1.1及2.0等各个版本。并且有vb.net的例子。是新手制作压缩工作的好软件。 如果你的软件中需要压缩备份,引用这个类库可以轻松实现。

    VB版仿WinRar解压缩源代码(修正版)

    这是一个仿winrar的解压缩程序的源码,使用了Unrar.dll来实现解压缩功能,目前改dll的版本是3.8,还没有实现压缩功能。使用前,请把压缩包中的unrar.dll 拷贝到系统目录(windows/system32)目录下方可使用。 这个代码下载...

    packetvb vb网络抓包

    2.解压缩packetvb-1.0.0.3.bin.zip,把其中的vbpcap.dll拷贝到c:\windows\system32目录下 3.“开始”-->“运行”,输入“regsvr32 c:\windows\system32\vbpcap.dll”,注册vbpcap.dll 4.打开VB,“工程”-->“引用”-...

    【.Net 】Zip操作库

    - 一个Windows窗体应用程序,创建的归档内容的隐私AES加密ZIP压缩文件。 - 在PowerShell的脚本或VBScript行政执行备份和归档。 - WCF服务,它接收一个zip文件作为附件,并动态的zip解压缩到一个流分析 - 一个老派...

    Tcp通讯.zip压缩格式

    完整的VB.NET TCP多客户端通信,可以参考。

    A Nibble of a Byte:有限数据集的位操作和唯一压缩-开源

    .zip包含一个独立的vb.net类(NibbleAndByte),如果需要,可以在其他vb.net应用程序中使用。 演示应用程序的几乎所有功能都源自NibbleAndByte类。 此类可以添加到VB.Net或C#应用程序中,这些应用程序需要使用一组...

    详解C#压缩、解压文件夹/文件(带密码)

    DotNetZip是一个开源的免费类库,主要提供了快速操作zip文件的工具集,VB、C#任何.Net语言都可以通过它创建、解压缩zip文件。我使用该类库最主要的目的还是因为它可以创建带密码保护的压缩文件。 只有设置了zip....

    CL.IO.Zip:CL.IO.Zip(v1.1.0)是一个用C#编写的.NET平台的Zip库。 它提供流程委托

    #### CL.IO.Zip 是一个基于SharpZipLib的一个压缩和解压的类库,提供给用户在.net环境下使用(VB.NET,C#..等等)当前最新版本为:V1.1.0 百度网盘下载地址:  做任何操作之前,请使用单例模式,获取ZipHandler对象...

    用VB6.0自制压缩与解压缩程序.rar

    不是我的东西,详细介绍看这里,http://blog.csdn.net/douhapy/ archive/2004/10/11/132114.aspx. 不过,搞半天都显示出错,于是加了点东西,终于可以了 原作者没有联系上,如果侵权,请联系我,我马上删除

    压缩修复数据库.zip

    采用vb.net 编程,对损坏的ACEESS 2007数据库进行修复

    VB编程资源大全(源码 控制)

    提供了一个鼠标控制的类,包括移动、限制隐藏等功能(4KB) 43,cpu.zip 监视 CPU 使用情况的例子(4KB) 44,cdfind.zip 找出系统光驱的盘符(3KB) 45,up.zip 数字大写转换程序(4KB) 46,ktext.zip...

    VB编程资源大全(控件 图形1)

    gtimage.v1.4.zip 一个扩展的picturebox控件(215KB) 10,browser.zip 支持bmp, jpeg, gif, ico和wmf格式的图片浏览控件(free), 主页地址http://www.planete.net/~lucash/activex/index.htm(181KB) 11,...

    sonar9.8-scanner4.7-jdk11.zip

    社区版本不支持一个项目多分支的形式,只能按照特性分支的名称来生成相对应的扫描项目(会产生很多Sonarqube项目)。 如何计算费用? Sonarqube是按照扫描的行数进行计费的,以年为单位进行订阅。关于行数如何解读?...

    C#中进行解压缩ZIP UNZIP源码,推荐!!!

    可以在C#, VB等任何.NET 语言中 非常好用!

    PowerTCP FTP Tool – 适用于FTP服务器上传和下载的控件

    目前该工具包括.NET wrapper DLL、9个新的VB.NET样例和.NET文件。 概述 只需调用一个方法就可以实现在客户端和任何FTP服务器之间拷贝文件。简单的属性提供了精细的控制功能,而且文件数据甚至可以放入内存/或从...

Global site tag (gtag.js) - Google Analytics