vbscript对文件或文件夹进行打包与解包
在自动化测试工作中,有时会用到对文件或文件夹进行打包或解包,经过一番研究,我们可以通过CreateObject非常轻松地实现所需要的功能。
首先,需要在操作系统中注册XZip.dll,以管理员身份:Regsvr32 "XZip.dll文件路径"
在http://www.xstandard.com/en/documentation/xzip/地址可以下载到该DLL,这里也有它的使用范例讲解,目前可以支持以下平台:
?For Windows 2000, XP, Vista, Windows 7: IE 5.0+, Firefox 1.0+, Safari 3.0+ or Opera 9.0+
?For OS X 10.3.9+: Firefox 1.0+ or Safari 1.3+
例如:将C:\test\xifeijian.jpg打包成C:\test.zip
Dim objZip
set objZip=createobject("XStandard.zip")
objZip.Pack "C:\test\xifeijian.jpg","C:\test.zip"
Set objZip=nothing
Examples
The examples below are for Active Server Pages. For Windows Scripting Host or Visual Basic, replaceServer.CreateObject
withCreateObject
and replaceResonse.Write
withMsgBox
.
How to archive (or zip) multiple files
<%
Dim objZip
Set objZip = Server.CreateObject("XStandard.Zip")
objZip.Pack "C:\Temp\golf.jpg", "C:\Temp\images.zip"
objZip.Pack "C:\Temp\racing.gif", "C:\Temp\images.zip"
Set objZip = Nothing
%>
How to archive (or zip) multiple files with different compression levels
<%
Dim objZip
Set objZip = Server.CreateObject("XStandard.Zip")
objZip.Pack "C:\Temp\reports.doc", "C:\Temp\archive.zip", , , 9
objZip.Pack "C:\Temp\boat.jpg", "C:\Temp\archive.zip", , , 1
Set objZip = Nothing
%>
How to archive (or zip) multiple files with default path
<%
Dim objZip
Set objZip = Server.CreateObject("XStandard.Zip")
objZip.Pack "C:\Temp\reports.doc", "C:\Temp\archive.zip", True
objZip.Pack "C:\Temp\boat.jpg", "C:\Temp\archive.zip", True
Set objZip = Nothing
%>
How to archive (or zip) multiple files with a custom path
<%
Dim objZip
Set objZip = Server.CreateObject("XStandard.Zip")
objZip.Pack "C:\Temp\reports.doc", "C:\Temp\archive.zip", True, "files/word"
objZip.Pack "C:\Temp\boat.jpg", "C:\Temp\archive.zip", True, "files/images"
Set objZip = Nothing
%>
How to archive (or zip) multiple files using wildcards
<%
Dim objZip
Set objZip = Server.CreateObject("XStandard.Zip")
objZip.Pack "C:\Temp\*.jpg", "C:\Temp\images.zip"
Set objZip = Nothing
%>
How to unzip files
<%
Dim objZip
Set objZip = Server.CreateObject("XStandard.Zip")
objZip.UnPack "C:\Temp\images.zip", "C:\Temp\"
Set objZip = Nothing
%>
How to unzip files using wildcards
<%
Dim objZip
Set objZip = Server.CreateObject("XStandard.Zip")
objZip.UnPack "C:\Temp\images.zip", "C:\Temp\", "*.jpg"
Set objZip = Nothing
%>
How to get a listing of files and folder in an archive
<%
Dim objZip, objItem
Set objZip = Server.CreateObject("XStandard.Zip")
For Each objItem In objZip.Contents("C:\Temp\images.zip")
Response.Write objItem.Path & objItem.Name & "<br />"
Next
Set objZip = Nothing
Set objItem = Nothing
%>
How to remove a file from an archive
<%
Dim objZip
Set objZip = Server.CreateObject("XStandard.Zip")
objZip.Delete "headshots/smith.jpg", "C:\Temp\images.zip"
Set objZip = Nothing
%>
How to move a file in an archive
<%
Dim objZip
Set objZip = Server.CreateObject("XStandard.Zip")
objZip.Move "headshots/jones.jpg", "staff/jones.jpg", "C:\Temp\images.zip"
Set objZip = Nothing
%>
How to rename a file in an archive
<%
Dim objZip
Set objZip = Server.CreateObject("XStandard.Zip")
objZip.Move "headshots/jones.jpg", "headshots/randy-jones.jpg", "C:\Temp\images.zip"
Set objZip = Nothing
%>
Known Issues
- The
UnPack()
method will first remove all files from the destination folder. This behavior will change in future releases. - This component was not designed to work with huge archives. Max archive size depends on the amount of available RAM.
- This component is designed for 32-bit operating systems and will not work natively on 64-bit operating systems.