在Windows中,你可以压缩一些文件

右键单击→发送到→压缩(压缩)文件夹

双击.zip文件解压缩并解压缩文件。

是否有一种方法可以从脚本(.bat文件)应用这些功能,而不需要安装任何第三方软件?


当前回答

PowerShell 5.0

从Microsoft.PowerShell.Archive可以使用:

Compress-Archive Expand-Archive

例如:

从整个Test文件夹中创建result.zip: 压缩文件路径C:\Test -DestinationPath C:\result 在指定的Test文件夹中提取result.zip的内容: 扩展-归档-路径result.zip -目的地路径C:\Test .zip

其他回答

我发现了一种方法来解压缩压缩文件与批处理文件:

@echo off
::unziper
set /p zip=zip:
powershell -Command "Expand-Archive %zip%"
echo done
pause

我把这个贴出来,以防有人需要。 你为什么要修改我的答案?

壳。应用程序

与壳牌。应用程序可以模拟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

TAR

在windows的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

更新的windows版本包括tar.exe。 Tar.exe可用于CMD或Windows批处理文件中。

//Compress a folder:
tar -caf aa.zip c:\aa

为了扩展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。

可以使用此命令压缩整个文件夹及其子文件夹

/c /s " c:\Temp\my_folder" /I /Q

来解压

compact /u /s:"C:\Temp\my_folder" /I /Q

c代表压缩,u代表解压