在Windows中,你可以压缩一些文件
右键单击→发送到→压缩(压缩)文件夹
双击.zip文件解压缩并解压缩文件。
是否有一种方法可以从脚本(.bat文件)应用这些功能,而不需要安装任何第三方软件?
在Windows中,你可以压缩一些文件
右键单击→发送到→压缩(压缩)文件夹
双击.zip文件解压缩并解压缩文件。
是否有一种方法可以从脚本(.bat文件)应用这些功能,而不需要安装任何第三方软件?
当前回答
更新的windows版本包括tar.exe。 Tar.exe可用于CMD或Windows批处理文件中。
//Compress a folder:
tar -caf aa.zip c:\aa
其他回答
它并不是ZIP文件,但使用Windows工具压缩文件的唯一方法是:
makecab <source> <dest>.cab
减压:
expand <source>.cab <dest>
高级示例(来自ss64.com):
Create a self extracting archive containing movie.mov:
C:\> makecab movie.mov "temp.cab"
C:\> copy /b "%windir%\system32\extrac32.exe"+"temp.cab" "movie.exe"
C:\> del /q /f "temp.cab"
更多信息:makecab,扩展,makecab高级用法
如果你安装了Java,你可以使用jar命令压缩成ZIP文件:
jar -cMf targetArchive.zip sourceDirectory
创建一个新的归档文件。 M =指定清单文件不应添加到存档。 f =目标文件名。
如果您需要将此作为脚本的一部分,那么最好的方法是使用Java。假设bin目录在你的路径中(在大多数情况下),你可以使用命令行:
jar xf test.zip
如果Java不在你的路径上,直接引用它:
C:\Java\jdk1.6.0_03\bin>jar xf test.zip
为了扩展Steven Penny的PowerShell解决方案,你可以通过调用PowerShell .exe将其合并到一个批处理文件中,就像这样:
powershell.exe -nologo -noprofile -command "& { Add-Type -A 'System.IO.Compression.FileSystem'; [IO.Compression.ZipFile]::ExtractToDirectory('foo.zip', 'bar'); }"
正如Ivan Shilo所说,这在PowerShell 2中行不通,它需要PowerShell 3或更高版本和. net Framework 4。
加上@Jason Duffett的回答及其注释,这里有一个脚本,从用户那里获得输入和输出(分别是文件名和目录名):
@echo off
set input=%1
set output=%2
powershell.exe "Add-Type -A 'System.IO.Compression.FileSystem'; [IO.Compression.ZipFile]::ExtractToDirectory('%input%', '%output%');"
用法:
unzip.bat path\to\file.zip path\to\output\directory