[QTP]vbscript对文件或文件夹进行打包与解包

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

  1. <%
  2. Dim objZip
  3. Set objZip = Server.CreateObject("XStandard.Zip")
  4. objZip.Pack "C:\Temp\golf.jpg", "C:\Temp\images.zip"
  5. objZip.Pack "C:\Temp\racing.gif", "C:\Temp\images.zip"
  6. Set objZip = Nothing
  7. %>

How to archive (or zip) multiple files with different compression levels

  1. <%
  2. Dim objZip
  3. Set objZip = Server.CreateObject("XStandard.Zip")
  4. objZip.Pack "C:\Temp\reports.doc", "C:\Temp\archive.zip", , , 9
  5. objZip.Pack "C:\Temp\boat.jpg", "C:\Temp\archive.zip", , , 1
  6. Set objZip = Nothing
  7. %>

How to archive (or zip) multiple files with default path

  1. <%
  2. Dim objZip
  3. Set objZip = Server.CreateObject("XStandard.Zip")
  4. objZip.Pack "C:\Temp\reports.doc", "C:\Temp\archive.zip", True
  5. objZip.Pack "C:\Temp\boat.jpg", "C:\Temp\archive.zip", True
  6. Set objZip = Nothing
  7. %>

How to archive (or zip) multiple files with a custom path

  1. <%
  2. Dim objZip
  3. Set objZip = Server.CreateObject("XStandard.Zip")
  4. objZip.Pack "C:\Temp\reports.doc", "C:\Temp\archive.zip", True, "files/word"
  5. objZip.Pack "C:\Temp\boat.jpg", "C:\Temp\archive.zip", True, "files/images"
  6. Set objZip = Nothing
  7. %>

How to archive (or zip) multiple files using wildcards

  1. <%
  2. Dim objZip
  3. Set objZip = Server.CreateObject("XStandard.Zip")
  4. objZip.Pack "C:\Temp\*.jpg", "C:\Temp\images.zip"
  5. Set objZip = Nothing
  6. %>

How to unzip files

  1. <%
  2. Dim objZip
  3. Set objZip = Server.CreateObject("XStandard.Zip")
  4. objZip.UnPack "C:\Temp\images.zip", "C:\Temp\"
  5. Set objZip = Nothing
  6. %>

How to unzip files using wildcards

  1. <%
  2. Dim objZip
  3. Set objZip = Server.CreateObject("XStandard.Zip")
  4. objZip.UnPack "C:\Temp\images.zip", "C:\Temp\", "*.jpg"
  5. Set objZip = Nothing
  6. %>

How to get a listing of files and folder in an archive

  1. <%
  2. Dim objZip, objItem
  3. Set objZip = Server.CreateObject("XStandard.Zip")
  4. For Each objItem In objZip.Contents("C:\Temp\images.zip")
  5. Response.Write objItem.Path & objItem.Name & "<br />"
  6. Next
  7. Set objZip = Nothing
  8. Set objItem = Nothing
  9. %>

How to remove a file from an archive

  1. <%
  2. Dim objZip
  3. Set objZip = Server.CreateObject("XStandard.Zip")
  4. objZip.Delete "headshots/smith.jpg", "C:\Temp\images.zip"
  5. Set objZip = Nothing
  6. %>

How to move a file in an archive

  1. <%
  2. Dim objZip
  3. Set objZip = Server.CreateObject("XStandard.Zip")
  4. objZip.Move "headshots/jones.jpg", "staff/jones.jpg", "C:\Temp\images.zip"
  5. Set objZip = Nothing
  6. %>

How to rename a file in an archive

  1. <%
  2. Dim objZip
  3. Set objZip = Server.CreateObject("XStandard.Zip")
  4. objZip.Move "headshots/jones.jpg", "headshots/randy-jones.jpg", "C:\Temp\images.zip"
  5. Set objZip = Nothing
  6. %>

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.

你可能感兴趣的:(VBScript)