使用windows自带的命令进行文件或文件夹的压缩

来源:互联网 发布:蓝牙助手源码 编辑:程序博客网 时间:2024/06/05 05:22

系统环境:

需求:因为是作为服务器端的,不想安装任何第三方软件,所以决定使用windows自带的命令进行文件或文件夹的压缩

解决过程:

通常我们使用C:\Documents and Settings\root\SendTo\压缩(zipped)文件夹这个功能进行压缩,但是可以查看这个仅仅是 Explorer shell command,具体解释可以查看原文链接

因为不是一个可执行程序,所以我们无法通过在cmd中直接调用,貌似无法进展了

既然无法直接调用,那我们可以通过间接调用,通过VSB脚本调用COM接口从而调用自带的zip,因为SCript.exe从Windows98开始就默认安装了有了思路就开始解决吧

编写VBS脚本,zip.vbs

Set objArgs = WScript.ArgumentsZipFile = objArgs(0)' Create empty ZIP file and open for addingCreateObject("Scripting.FileSystemObject").CreateTextFile(ZipFile, True).Write "PK" & Chr(5) & Chr(6) & String(18, vbNullChar)Set zip = CreateObject("Shell.Application").NameSpace(ZipFile)' Add all files/directories to the .zip fileFor i = 1 To objArgs.count-1  zip.CopyHere(objArgs(i))  WScript.Sleep 10000 'REQUIRED!! (Depending on file/dir size)Next


调用脚本

cscript zip.vbs target.zip sourceFile1 sourceDir2 
实例:


当然如果你更懒的话可以编写一个bat处理脚本直接写好需要压缩的文件双击运行即可

例如:zip.bat

set SRC=d:\src.txtset TAR=d:\tar.zipecho 'begin zip files'cscript d:\zip.vbs %TAR% %SRC%echo 'success'pause

参考链接:

windows自带zip说明    http://filext.com/faq/compressed_zip_folder.php

VBS脚本    http://superuser.com/questions/110991/can-you-zip-a-file-from-the-command-prompt-using-only-windows-built-in-capabili