功能说明
window操作系统未安装压缩的软件有需要压缩、解压缩文件支持单文件压缩支持目录及子目录压缩目录结构
压缩脚本(zip.vbs)
Set objArgs = WScript.ArgumentsZip objArgs(0),objArgs(1)Sub Zip(ByVal mySourceDir, ByVal myZipFile)Set fso = CreateObject("Scripting.FileSystemObject")If fso.GetExtensionName(myZipFile) <> "zip" ThenExit SubElseIf fso.FolderExists(mySourceDir) ThenFType = "Folder"ElseIf fso.FileExists(mySourceDir) ThenFType = "File"FileName = fso.GetFileName(mySourceDir)FolderPath = Left(mySourceDir, Len(mySourceDir) - Len(FileName))ElseExit SubEnd IfSet f = fso.CreateTextFile(myZipFile, True)f.Write "PK" & Chr(5) & Chr(6) & String(18, Chr(0))f.CloseSet objShell = CreateObject("Shell.Application")Select Case FtypeCase "Folder"Set objSource = objShell.NameSpace(mySourceDir)Set objFolderItem = objSource.Items()Case "File"Set objSource = objShell.NameSpace(FolderPath)Set objFolderItem = objSource.ParseName(FileName)End SelectSet objTarget = objShell.NameSpace(myZipFile)intOptions = 256objTarget.CopyHere objFolderItem, intOptionsDoWScript.Sleep 1000Loop Until objTarget.Items.Count > 0End Sub
解压缩(unzip.vbs)
Set objArgs = WScript.ArgumentsUnZip objArgs(0),objArgs(1)Sub UnZip(ByVal myZipFile, ByVal myTargetDir)Set fso = CreateObject("Scripting.FileSystemObject")If NOT fso.FileExists(myZipFile) ThenExit SubElseIf fso.GetExtensionName(myZipFile) <> "zip" ThenExit SubElseIf NOT fso.FolderExists(myTargetDir) Thenfso.CreateFolder(myTargetDir)End IfSet objShell = CreateObject("Shell.Application")Set objSource = objShell.NameSpace(myZipFile)Set objFolderItem = objSource.Items()Set objTarget = objShell.NameSpace(myTargetDir)intOptions = 256objTarget.CopyHere objFolderItem, intOptionsEnd Sub
批处理(Bat)示例
::获取当前目录set curDir=%cd%:: 压缩目录unzip "D:\test" "D:\test.zip":: 压缩文件unzip "D:\test.txt" "D:\test.zip":: 解压缩unzip "D:\test.zip" "D:\test"::压缩目录到当前路径下zip文件zip "D:\SmartC" "%cd%\t1.zip"::解压当前目录下zip文件到当前路径下子目录unzip "%cd%\t1.zip" "%cd%\t1"pause
参考资料
http://demon.tw/programming/vbs-unzip-file.html
常见问题
示例中bat文件无法执行可能是注释的中文乱码导致,尝试删除中文注释