Skip to content

cZipArchive

一个纯 VB6 单类库,用于 ZIP 压缩包管理。

开源来源:https://github.com/wqweto/ZipArchive
作者:wqweto@gmail.com
许可证:MIT

使用方法

只需将 cZipArchive.cls 添加到你的项目中,然后像这样使用:

简单压缩

vb
With New cZipArchive
    .AddFile App.Path & "\your_file"
    .CompressArchive App.Path & "\test.zip"
End With

压缩所有文件和子文件夹

vb
With New cZipArchive
    .AddFromFolder "C:\Path\To\*.*", Recursive:=True
    .CompressArchive App.Path & "\archive.zip"
End With

解压压缩包中的所有文件

vb
With New cZipArchive
    .OpenArchive App.Path & "\test.zip"
    .Extract "C:\Path\To\extract_folder"
End With

Extract 方法可以按文件掩码(如 Filter:="*.doc")、文件索引(如 Filter:=15)或布尔数组(需要解压的索引设为 True)进行过滤。

提取单个文件到指定文件名

OutputTarget 可以包含目标 new_filename,用于从压缩包中提取特定文件时重命名。

vb
With New cZipArchive
    .OpenArchive App.Path & "\test.zip"
    .Extract "C:\Path\To\extract_folder\new_filename", Filter:="your_file"
End With

获取压缩包条目未压缩大小

通过 FileInfo 属性,以条目文件名作为第一个参数,并使用 zipIdxSize

vb
With New cZipArchive
    .OpenArchive App.Path & "\test.zip"
    Debug.Print .FileInfo("report.pdf", zipIdxSize)
End With

列出 ZIP 压缩包中的文件

通过 FileInfo 属性,以数字索引作为第一个参数:

vb
Dim lIdx As Long
With New cZipArchive
    .OpenArchive App.Path & "\test.zip"
    For lIdx = 0 To .FileCount - 1
        Debug.Print "FileName=" & .FileInfo(lIdx, zipIdxFileName) & ", Size=" & .FileInfo(lIdx, zipIdxSize)
    Next
End With

FileInfo 第二个参数的可用值:

名称
0zipIdxFileName
1zipIdxAttributes
2zipIdxCrc32
3zipIdxSize
4zipIdxCompressedSize
5zipIdxComment
6zipIdxLastModified
7zipIdxMethod
8zipIdxOffset
9zipIdxFlags

加密支持

确保在项目属性的“生成”选项卡中设置条件编译参数 ZIP_CRYPTO = 1,以从源码编译加密支持。默认情况下不编译加密支持,以减小最终可执行文件的体积。

vb
With New cZipArchive
    .OpenArchive App.Path & "\test.zip"
    .Extract App.Path & "\test", Password:="123456"
End With

AddFile 方法上使用 Password 参数,并配合 EncrStrength 参数来设置创建压缩包时使用的加密方式。

EncrStrength模式
0ZipCrypto(默认)
1AES-128
2AES-192
3AES-256(推荐)

注意:默认的 ZipCrypto 加密较弱,但它是唯一与 Windows 资源管理器内置 zip 文件夹支持兼容的选项。

内存操作

/test/basic/Form1.frm 中的示例工具函数 ReadBinaryFile 返回文件内容的字节数组。

vb
Dim baZip() As Byte
With New cZipArchive
    .AddFile ReadBinaryFile("sample.pdf"), "report.pdf"
    .CompressArchive baZip
End With
WriteBinaryFile "test.zip", baZip

Extract 方法也支持字节数组作为目标。

vb
Dim baOutput() As Byte
With New cZipArchive
    .OpenArchive ReadBinaryFile("test.zip")
    .Extract baOutput, Filter:=0    '--- 仅压缩包中的第一个文件
End With

待办事项(尚未支持)

  • Deflate64 (解)压缩器
  • VBA7 (x64) 支持

VB6及其LOGO版权为微软公司所有