vba 压缩文件
使用方便 (Ease of use)
There are many uses for zip folders. Previously, saving disk space was the primary purpose, but today that often isn't very important; more the option for collecting a selection of files in one file may be more pertinent.
zip文件夹有很多用途。 以前,节省磁盘空间是主要目的,但如今,这通常并不是很重要。 此外,在一个文件中收集选择的文件的选项可能更相关。
However, for whatever reason or purpose you may have for using a zip folder (or a file as it physically is) that is controlled by VBA code in, say, Microsoft Excel or Microsoft Access, it should be easy to manage.
但是,无论出于何种原因或目的,使用在Microsoft Excel或Microsoft Access中由VBA代码控制的zip文件夹(或实际上是文件的文件),都应该易于管理。
Several external third-party tools and libraries are offered, and each may have certain advantages, but for the general usage - reading and writing zip folders as Windows Explorer does - you only need the two functions presented here: Zip and Unzip - nothing more, nothing less.
提供了几种外部第三方工具和库,每种工具都有某些优点,但是对于一般用途-像Windows资源管理器一样读写zip文件夹-您仅需要此处提供的两个功能: Zip和Unzip-仅此而已,不外乎。
Note: Cabinet files/folders can be managed in similar ways. If you are interested in this, please see:
Handle cabinet files and folders with VBA the Windows Explorer way
注意: 内阁文件/文件夹可以用类似的方式进行管理。 如果您对此感兴趣,请参阅:
使用Windows资源管理器方式使用VBA处理文件柜文件和文件夹
(
)
压缩和解压缩 (Zip and Unzip)
These two functions are included in the code and does just that in a way that - by default - matches the result as if you did it manually onscreen using Windows Explorer.
这两个函数包含在代码中,并且以与默认情况下匹配结果的方式(就像您使用Windows资源管理器在屏幕上手动完成)那样进行操作。
They can do a little more, controlled by the parameters as explained below, and will also work with custom file extensions, should you have the need. Thus, a zip folder may be named, say, myzipfolder.baz . This will prevent the file from being recognized by Windows Explorer as a zip folder.
它们可以通过如下所述的参数控制来做更多的工作,并且如果需要,还可以使用自定义文件扩展名。 因此,一个zip文件夹可以命名为myzipfolder.baz 。 这将防止Windows资源管理器将该文件识别为zip文件夹。
For large and/or many files, the well-known progress bar box will be displayed.
对于大文件和/或许多文件,将显示众所周知的进度栏框。
压缩 (Zip)
Result = Zip(Path)
This zips Path (a file or a folder) as if you, in Windows Explorer, right-click the file/folder and select Send to zip compressed folder. Result holds 0 (zero) if success, or an error code.
就像在Windows资源管理器中一样,右键单击文件/文件夹,然后选择发送到zip压缩文件夹,这样就可以压缩 Path (文件或文件夹)的路径 。 如果成功,则结果为0(零),否则为错误代码。
For many practical purposes, you also wish to specify where the zip folder will be created and/or what name it should be given. Thus:
出于许多实际目的,您还希望指定将在何处创建zip文件夹和/或应为其指定名称。 从而:
Destination = "D:\Backups\Data_20171224.zip"
Result = Zip(Path, Destination)
Should you wish so, the extension can be different from zip, for example:
如果需要,扩展名可以与zip不同,例如:
Destination = "D:\Backups\Data_20171224.zap"
Also you are probably aware, that if you, in Windows Explorer, tries to zip a file/folder once more, Windows Explorer will generate a "versioned" zip file, like:
您可能还知道,如果您在Windows资源管理器中尝试再次压缩文件/文件夹,则Windows资源管理器将生成一个“版本化”的zip文件,例如:
D:\Backups\Data_20171224.zip
D:\Backups\Data_20171224 (2).zip
D:\Backups\Data_20171224 (3).zip
and so on - and so will Zip do.
等等-Zip也会这样做。
If you don't want this, a third parameter is available, Overwrite. Thus, if the destination exists:
如果您不希望这样做,则可以使用第三个参数“覆盖”。 因此,如果目的地存在:
Result = Zip(Path, , True) ' Delete and recreate the zip folder.
Result = Zip(Path, , False) ' Create a versioned zip folder.
(
)
解压缩 (UnZip)
Result = UnZip(Path)
This unzips Path (a zip folder) as if you, in Windows Explorer, right-click the zip folder and select Unzip all.... Result holds 0 (zero) if success, or an error code.
就像在Windows资源管理器中右键单击zip文件夹并选择全部解压缩...一样,这会将Path (一个zip文件夹) 解压缩 。 如果成功,则结果为0(零),否则为错误代码。
Often, you wish to specify where the zip folder will be unzipped and/or what name the folder should be given. If so, specify the Destination parameter:
通常情况下,你想指定的zip文件夹将被解压缩和/或什么名字的文件夹应该放在首位。 如果是这样,请指定Destination参数:
<