在Windows中,你可以压缩一些文件
右键单击→发送到→压缩(压缩)文件夹
双击.zip文件解压缩并解压缩文件。
是否有一种方法可以从脚本(.bat文件)应用这些功能,而不需要安装任何第三方软件?
在Windows中,你可以压缩一些文件
右键单击→发送到→压缩(压缩)文件夹
双击.zip文件解压缩并解压缩文件。
是否有一种方法可以从脚本(.bat文件)应用这些功能,而不需要安装任何第三方软件?
当前回答
壳。应用程序
与壳牌。应用程序可以模拟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
其他回答
加上@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
我对所有这些解决方案都有疑问。
它们并不完全相同,当从相同的源文件夹生成时,它们都创建的文件与RMB—> send to—>压缩(压缩)文件夹相比有轻微的大小差异。我有过的最接近的大小差异是300kb的差异(脚本>手册),使用:
powershell Compress-Archive -Path C:\sourceFolder -CompressionLevel Fastest -DestinationPath C:\destinationArchive.zip
(注意-CompressionLevel。有三个可能的值:最快,无压缩和最佳,(默认值:最佳))
我想做一个。bat文件,它可以自动将我正在使用的WordPress插件文件夹压缩成。zip文件,这样我就可以把它上传到WordPress站点并测试插件。
但出于某种原因,它不能与任何这些自动压缩,但它可以与手动人民币压缩,我发现真的很奇怪。
脚本生成的。zip文件实际上破坏了WordPress插件,使其无法激活,也不能从WordPress内部删除。我必须SSH到服务器的“后台”,手动删除上传的插件文件。而手动生成的人民币文件正常工作。
在2013年,这是不可能的。微软没有为此提供任何可执行文件。
看这个链接,一些VBS的方法来做到这一点。 https://superuser.com/questions/201371/create-zip-folder-from-the-command-line-windows
从Windows 8开始,.NET Framework 4.5默认安装,有system . io . compress . ziparchive和PowerShell可用,你可以写脚本来实现这一点,见 https://stackoverflow.com/a/26843122/71312
为了扩展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。
正如@Noam Manos所回答的,我们可以通过在.bashrc文件中使用别名来改进压缩:
别名zip=“jar -cMf”
然后命令(在bash终端中)将是
邮编目标.zip目录1 目录2
注意:你应该安装java和git bash,并在windows中进行安装。