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

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

方法一:

打包单个文件:makecab src.txt tar.zip 

解压单个文件:expand src.txt tar.zip

方法二:

解决过程:

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

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

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

编写VBS脚本,zip.vbs

Set objArgs = WScript.Arguments
ZipFile = objArgs(0)
 
' Create empty ZIP file and open for adding
CreateObject("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 file
For 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.txt
set TAR=d:\tar.zip
 
echo '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
 

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值