如何仅使用 Windows 的内置功能从脚本中压缩或解压缩?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17546016/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can you zip or unzip from the script using ONLY Windows' built-in capabilities?
提问by Roee Gavirel
In Windows you can zip some files by
在 Windows 中,您可以通过以下方式压缩一些文件
right click → Send to→ Compressed (zipped) folder
右键单击 →发送至→压缩(zipped)文件夹
And unzip by double clicking on the .zip
file and extract the files.
并通过双击.zip
文件解压缩并解压缩文件。
Is there a way to apply those abilities from a script (.bat file) without the need to install any third-party software?
有没有办法从脚本(.bat 文件)应用这些功能,而无需安装任何第三方软件?
采纳答案by Diryboy
Back in 2013, that was not possible. Microsoft didn't provide any executable for this.
早在 2013 年,这是不可能的。微软没有为此提供任何可执行文件。
See this link for some VBS way to do this. https://superuser.com/questions/201371/create-zip-folder-from-the-command-line-windows
有关执行此操作的某些 VBS 方法,请参阅此链接。 https://superuser.com/questions/201371/create-zip-folder-from-the-command-line-windows
From Windows 8 on, .NET Framework 4.5 is installed by default, with System.IO.Compression.ZipArchiveand PowerShell available, one can write scripts to achieve this, see https://stackoverflow.com/a/26843122/71312
从 Windows 8 开始,默认安装 .NET Framework 4.5,有System.IO.Compression.ZipArchive和 PowerShell 可用,可以编写脚本来实现这一点,参见 https://stackoverflow.com/a/26843122/71312
回答by Jason Duffett
To expand upon Steven Penny's PowerShell solution, you can incorporate it into a batch file by calling powershell.exe like this:
要扩展 Steven Penny 的 PowerShell 解决方案,您可以通过调用 powershell.exe 将其合并到一个批处理文件中,如下所示:
powershell.exe -nologo -noprofile -command "& { Add-Type -A 'System.IO.Compression.FileSystem'; [IO.Compression.ZipFile]::ExtractToDirectory('foo.zip', 'bar'); }"
As Ivan Shilo said, this won't work with PowerShell 2, it requires PowerShell 3 or greater and .NET Framework 4.
正如 Ivan Shilo 所说,这不适用于 PowerShell 2,它需要 PowerShell 3 或更高版本和 .NET Framework 4。
回答by Steven Penny
As of .NET 4.5, PowerShell can do this:
从.NET 4.5 开始,PowerShell 可以执行以下操作:
Add-Type -A System.IO.Compression.FileSystem
[IO.Compression.ZipFile]::CreateFromDirectory('foo', 'foo.zip')
[IO.Compression.ZipFile]::ExtractToDirectory('foo.zip', 'bar')
For the last two lines, bar
and foo
can be replaced with the name you would like the folder to have, or whatever the name is of the preexisting folder. foo.zip
in both cases acts as the zip file either to save as or to unzip from.
对于最后两行,bar
并foo
可以用名字来代替你想的文件夹有,或任何名称预先存在的文件夹中。foo.zip
在这两种情况下,都用作 zip 文件,可以另存为或从中解压缩。
回答by Noam Manos
If you have Java installed, you can compress to a ZIP archive using the jar
command:
如果您安装了 Java,则可以使用以下jar
命令压缩为 ZIP 存档:
jar -cMf targetArchive.zip sourceDirectory
c = Creates a new archive file.
M = Specifies that a manifest file should not be added to the archive.
f = Indicates target file name.
c = 创建一个新的存档文件。
M = 指定不应将清单文件添加到存档中。
f = 表示目标文件名。
回答by ROMANIA_engineer
PowerShell 5.0
PowerShell 5.0
From Microsoft.PowerShell.Archive
you can use:
从Microsoft.PowerShell.Archive
您可以使用:
E.g.:
例如:
Create
result.zip
from the entireTest
folder:Compress-Archive -Path C:\Test -DestinationPath C:\result
Extract the content of
result.zip
in the specifiedTest
folder:Expand-Archive -Path result.zip -DestinationPath C:\Test
result.zip
从整个Test
文件夹创建:Compress-Archive -Path C:\Test -DestinationPath C:\result
提取
result.zip
指定Test
文件夹中的内容:Expand-Archive -Path result.zip -DestinationPath C:\Test
回答by Federico Santamorena
It isn't exactly a ZIP, but the only way to compress a file using Windows tools is:
它不完全是 ZIP,但使用 Windows 工具压缩文件的唯一方法是:
makecab <source> <dest>.cab
To decompress:
解压:
expand <source>.cab <dest>
Advanced example (from ss64.com):
高级示例(来自 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"
More information: makecab, expand, makecab advanced uses
更多信息:makecab、expand、makecab 高级用途
回答by Monir
Using 7-Zip:
使用 7-Zip:
Zip: you have a folder foo
, and want to zip it to myzip.zip
Zip: 你有一个文件夹foo
,想把它压缩到myzip.zip
"C:\Program Files-Zipz.exe" a -r myzip.zip -w foo -mem=AES256
Unzip: you want to unzip it (myzip.zip
) to current directory (./
)
解压缩:您想将其解压缩 ( myzip.zip
) 到当前目录 ( ./
)
"C:\Program Files-Zipz.exe" x myzip.zip -o./ -y -r
回答by LostUser
I've been looking to answer this exact question and from my research, DiryBoy's response seems to be accurate.
我一直在寻找确切的答案,从我的研究来看,DiryBoy 的回答似乎是准确的。
I found the compact.exe program compresses files but not to create a highly compressed file (or set of files). It is similar to the option you get when right clicking on a drive letter or partition in Windows. You get the option to do cleanup (remove temp files, etc) as well as compress files. The compressed files are still accessible but are just compressed to create space on a drive that is low on space.
我发现 compact.exe 程序会压缩文件,但不会创建高度压缩的文件(或文件集)。它类似于在 Windows 中右键单击驱动器号或分区时获得的选项。您可以选择进行清理(删除临时文件等)以及压缩文件。压缩文件仍然可以访问,但只是被压缩以在空间不足的驱动器上创建空间。
I also found compress.exe which I did happen to have on my computer. It isn't natively on most windows machines and is part of the 2003 resource kit. It does make a zipped file of sorts but it is really more similar to files from a windows setup disk (has the underscore as the last character of the file extension or name). And the extract.exe command extracts those files.
我还找到了 compress.exe,我的电脑上确实有它。它不是在大多数 Windows 机器上本机的,而是 2003 资源工具包的一部分。它确实制作了各种压缩文件,但它实际上更类似于 Windows 安装盘中的文件(下划线作为文件扩展名或名称的最后一个字符)。并且extract.exe 命令提取这些文件。
However, the mantra is, if it can be done natively via the GUI then there is likely a way to do it via batch, .vbs, or some other type of script within the command line. Since windows has had the 'send to' option to create a zip file, I knew there had to be a way to do it via command line and I found some options.
然而,口头禅是,如果它可以通过 GUI 本地完成,那么可能有一种方法可以通过批处理、.vbs 或命令行中的其他类型的脚本来完成。由于 Windows 有“发送到”选项来创建 zip 文件,我知道必须有一种方法可以通过命令行来完成,我找到了一些选项。
Here is a great link that shows how to zip a file using windows native commands.
这是一个很好的链接,显示了如何使用 Windows 本机命令压缩文件。
I tested it with a directory containing multiple nested files and folders and it worked perfectly. Just follow the format of the command line.
我使用包含多个嵌套文件和文件夹的目录对其进行了测试,它运行良好。只需遵循命令行的格式即可。
There is also a way to unzip the files via command line which I found as well. One way, just brings open an explorer window showing what the content of the zipped file is. Some of these also use Java which isn't necessarily native to windows but is so common that it nearly seems so.
还有一种方法可以通过我发现的命令行解压缩文件。一种方法是打开一个资源管理器窗口,显示压缩文件的内容。其中一些还使用 Java,它不一定是 Windows 原生的,但很常见,几乎看起来如此。
回答by PodTech.io
You can use a VBScript script wrapped in a BAT file. This code works on a relative PATH.
您可以使用包装在 BAT 文件中的 VBScript 脚本。此代码适用于相对路径。
There isn't any need for any third-party tools or dependencies. Just set SOURCEDIR
and OUTPUTZIP
.
不需要任何第三方工具或依赖项。只需设置SOURCEDIR
和OUTPUTZIP
。
Filename: ZipUp.bat
文件名:ZipUp.bat
echo Set fso = CreateObject("Scripting.FileSystemObject") > _zipup.vbs
echo InputFolder = fso.GetAbsolutePathName(WScript.Arguments.Item(0)) >> _zipup.vbs
echo ZipFile = fso.GetAbsolutePathName(WScript.Arguments.Item(1)) >> _zipup.vbs
' Create empty ZIP file.
echo CreateObject("Scripting.FileSystemObject").CreateTextFile(ZipFile, True).Write "PK" ^& Chr(5) ^& Chr(6) ^& String(18, vbNullChar) >> _zipup.vbs
echo Set objShell = CreateObject("Shell.Application") >> _zipup.vbs
echo Set source = objShell.NameSpace(InputFolder).Items >> _zipup.vbs
echo objShell.NameSpace(ZipFile).CopyHere(source) >> _zipup.vbs
echo ' Keep script waiting until compression is done
echo Do Until objShell.NameSpace( ZipFile ).Items.Count = objShell.NameSpace( InputFolder ).Items.Count >> _zipup.vbs
echo WScript.Sleep 200 >> _zipup.vbs
echo Loop >> _zipup.vbs
CScript _zipup.vbs %SOURCEDIR% %OUTPUTZIP%
del _zipup.vbs
Example usage
示例用法
SET SOURCEDIR=C:\Some\Path
SET OUTPUTZIP=C:\Archive.zip
CALL ZipUp
Alternatively, you can parametrize this file by replacing the line CScript _zipup.vbs %SOURCEDIR% %OUTPUTZIP%
with CScript _zipup.vbs %1 %2
, in which case it can be even more easily called from by simply calling CALL ZipUp C:\Source\Dir C:\Archive.zip
.
另外,您也可以通过更换线参数化这个文件CScript _zipup.vbs %SOURCEDIR% %OUTPUTZIP%
用CScript _zipup.vbs %1 %2
,在这种情况下,可以通过简单地调用更加容易叫从CALL ZipUp C:\Source\Dir C:\Archive.zip
。
回答by Sebastian Norr
I have a problem with all these solutions.
我对所有这些解决方案都有问题。
They're not exactlythe same, and they all create files that have a slight size difference compared to the RMB --> send to --> compressed (zipped) folder
when made from the same source folder. The closest size-difference I have had is 300 KB difference (script > manual), made with:
它们并不完全相同,并且它们创建的文件与RMB --> send to --> compressed (zipped) folder
从同一源文件夹创建的文件相比,它们的大小略有不同。我所拥有的最接近的大小差异是 300 KB 差异(脚本 > 手册),由:
powershell Compress-Archive -Path C:\sourceFolder -CompressionLevel Fastest -DestinationPath C:\destinationArchive.zip
(Notice the -CompressionLevel. There are three possible values: Fastest, NoCompression & Optimal, (Default: Optimal))
(注意-CompressionLevel。有三个可能的值:最快、无压缩和最佳,(默认值:最佳))
I wanted to make a .bat file that should automatically compress a WordPress plugin folder I'm working on, into a .zip archive, so I can upload it into the WordPress site and test the plugin.
我想制作一个 .bat 文件,该文件应自动将我正在处理的 WordPress 插件文件夹压缩为 .zip 存档,以便我可以将其上传到 WordPress 站点并测试插件。
But for some reason it doesn't work with any of these automatic compressions, butit does work with the manual RMB compression, witch I find really strange.
但出于某种原因,它不适用于任何这些自动压缩,但它确实适用于手动人民币压缩,我觉得很奇怪。
And the script-generated .zip files actually break the WordPress plugins to the point where they can't be activated, and they can also not be deleted from inside WordPress. I have to SSH into the "back side" of the server and delete the uploaded plugin files themselves, manually. While the manually RMB-generated files work normally.
而脚本生成的 .zip 文件实际上将 WordPress 插件破坏到无法激活的程度,也无法从 WordPress 内部删除它们。我必须通过 SSH 连接到服务器的“后端”并手动删除上传的插件文件。而手动人民币生成的文件工作正常。