windows 创建批处理脚本来解压缩文件而无需额外的 zip 工具
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21704041/
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
creating batch script to unzip a file without additional zip tools
提问by JustinBieber
I'm trying to make a .bat script for windows 7 x64 to create a folder, unzip a file into that folder without having to use additional addons like 7zip or unzip. Been searching and it seemed like windows doesn't have builtins to allow unzip easily in command. Can I unzip/expand files without additional addons?
我正在尝试为 Windows 7 x64 制作一个 .bat 脚本来创建一个文件夹,将文件解压缩到该文件夹中,而无需使用 7zip 或 unzip 等附加插件。一直在搜索,似乎 Windows 没有内置命令可以轻松地在命令中解压缩。我可以在没有附加插件的情况下解压缩/扩展文件吗?
回答by Matt Williamson
Try this:
尝试这个:
@echo off
setlocal
cd /d %~dp0
Call :UnZipFile "C:\Temp\" "c:\path\to\batch.zip"
exit /b
:UnZipFile <ExtractTo> <newzipfile>
set vbs="%temp%\_.vbs"
if exist %vbs% del /f /q %vbs%
>%vbs% echo Set fso = CreateObject("Scripting.FileSystemObject")
>>%vbs% echo If NOT fso.FolderExists(%1) Then
>>%vbs% echo fso.CreateFolder(%1)
>>%vbs% echo End If
>>%vbs% echo set objShell = CreateObject("Shell.Application")
>>%vbs% echo set FilesInZip=objShell.NameSpace(%2).items
>>%vbs% echo objShell.NameSpace(%1).CopyHere(FilesInZip)
>>%vbs% echo Set fso = Nothing
>>%vbs% echo Set objShell = Nothing
cscript //nologo %vbs%
if exist %vbs% del /f /q %vbs%
Revision
修订
To have it perform the unzip on each zip file creating a folder for each use:
要让它对每个 zip 文件执行解压缩,为每次使用创建一个文件夹:
@echo off
setlocal
cd /d %~dp0
for %%a in (*.zip) do (
Call :UnZipFile "C:\Temp\%%~na\" "c:\path\to\%%~nxa"
)
exit /b
If you don't want it to create a folder for each zip, change
Call :UnZipFile "C:\Temp\%%~na\" "c:\path\to\%%~nxa"
to
Call :UnZipFile "C:\Temp\" "c:\path\to\%%~nxa"
如果您不希望它为每个 zip 创建一个文件夹,请更改
Call :UnZipFile "C:\Temp\%%~na\" "c:\path\to\%%~nxa"
为
Call :UnZipFile "C:\Temp\" "c:\path\to\%%~nxa"
回答by Artem
If you have PowerShell 5.0 or higher (pre-installed with Windows 10 and Windows Server 2016):
如果您有 PowerShell 5.0 或更高版本(预装 Windows 10 和 Windows Server 2016):
powershell Expand-Archive your.zip -DestinationPath your_destination
回答by Benjamin Krupp
Here is a quick and simple solution using PowerShell:
这是一个使用 PowerShell 的快速简单的解决方案:
powershell.exe -nologo -noprofile -command "& { $shell = New-Object -COM Shell.Application; $target = $shell.NameSpace('C:\extractToThisDirectory'); $zip = $shell.NameSpace('C:\extractThis.zip'); $target.CopyHere($zip.Items(), 16); }"
This uses the built-in extract functionality of the Explorer and will also show the typical extract progress window. The second parameter 16
to CopyHere
answers all questions with yes.
这使用资源管理器的内置提取功能,还将显示典型的提取进度窗口。第二个参数16
来CopyHere
回答与肯定的所有问题。
回答by npocmaka
Here's my overview about built-in zi/unzip (compress/decompress) capabilities in windows - How can I compress (/ zip ) and uncompress (/ unzip ) files and folders with batch file without using any external tools?
这是我对 Windows 中内置 zi/unzip(压缩/解压缩)功能的概述 -如何在不使用任何外部工具的情况下使用批处理文件压缩(/ zip )和解压缩(/ unzip )文件和文件夹?
To unzip file you can use this script:
要解压缩文件,您可以使用此脚本:
zipjs.bat unzip -source C:\myDir\myZip.zip -destination C:\MyDir -keep yes -force no
回答by John
Another approach to this issue could be to create a self extracting executable (.exe) using something like winzip and use this as the install vector rather than the zip file. Similarly, you could use NSIS to create an executable installer and use that instead of the zip.
解决此问题的另一种方法可能是使用诸如 winzip 之类的东西创建自解压可执行文件 (.exe),并将其用作安装向量而不是 zip 文件。同样,您可以使用 NSIS 创建一个可执行安装程序并使用它而不是 zip。