Shell.Application
使用 Shell.Application,您可以模拟 explorer.exe 压缩文件和文件夹的方式
该脚本名为zipjs.bat:
:: unzip content of a zip to given folder.content of the zip will be not preserved (-keep no).Destination will be not overwritten (-force no)
call zipjs.bat unzip -source C:\myDir\myZip.zip -destination C:\MyDir -keep no -force no
:: lists content of a zip file and full paths will be printed (-flat yes)
call zipjs.bat list -source C:\myZip.zip\inZipDir -flat yes
:: lists content of a zip file and the content will be list as a tree (-flat no)
call zipjs.bat list -source C:\myZip.zip -flat no
:: prints uncompressed size in bytes
zipjs.bat getSize -source C:\myZip.zip
:: zips content of folder without the folder itself
call zipjs.bat zipDirItems -source C:\myDir\ -destination C:\MyZip.zip -keep yes -force no
:: zips file or a folder (with the folder itslelf)
call zipjs.bat zipItem -source C:\myDir\myFile.txt -destination C:\MyZip.zip -keep yes -force no
:: unzips only part of the zip with given path inside
call zipjs.bat unZipItem -source C:\myDir\myZip.zip\InzipDir\InzipFile -destination C:\OtherDir -keep no -force yes
call zipjs.bat unZipItem -source C:\myDir\myZip.zip\InzipDir -destination C:\OtherDir
:: adds content to a zip file
call zipjs.bat addToZip -source C:\some_file -destination C:\myDir\myZip.zip\InzipDir -keep no
call zipjs.bat addToZip -source C:\some_file -destination C:\myDir\myZip.zip
MAKECAB
Makecab 是 windows 自带的默认压缩工具。虽然它可以使用不同的压缩算法(包括 zip),但文件格式始终是 .cab 文件。通过一些扩展,它也可以在 linux 机器上使用。
压缩文件:
makecab file.txt "file.cab"
压缩整个文件夹需要更多的工作。这里一个目录是用cabDir.bat压缩的:
call cabDir.bat ./myDir compressedDir.cab
使用expand 命令解压缩相当容易:
EXPAND cabfile -F:* .
更骇人听闻的方法是使用 extrac32 命令创建自解压可执行文件:
copy /b "%windir%\system32\extrac32.exe"+"mycab.cab" "expandable.exe"
call expandable.exe
焦油
在 windows 的 build 17063 中,我们有 tar 命令:
::compress directory
tar -cvf archive.tar c:\my_dir
::extract to dir
tar -xvf archive.tar.gz -C c:\data
.NET 工具
.net(和 powershell)提供了许多压缩和解压缩文件的方法。最直接的是使用 gzip 流。该脚本名为gzipjs.bat:
::zip
call gzipjs.bat -c my.file my.zip
::unzip
call gzipjs.bat -u my.zip my.file