我想製作一個 BAT 檔案來壓縮或解壓縮檔案。對於壓縮文件,我發現了這個問題: 您可以僅使用 Windows 的內建壓縮檔案功能從命令提示字元壓縮檔案嗎?
那裡給出的答案非常好並且對我有用,但我找不到有關如何解壓縮檔案的任何資訊。就像連結中一樣,我不能假設任何第三方工具(winRAR 除外)。
在此先感謝並抱歉英語錯誤
答案1
此批次檔程式碼將幫助您解壓縮檔案。
@echo off
setlocal
cd /d %~dp0
Call :UnZipFile "C:\Temp\" "c:\FolderName\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%
注意C:\Temp 是儲存提取(解壓縮)檔案的資料夾。
並且,c:\FolderName\batch.zip 是來源路徑(儲存 Zip 檔案的位置)。
請根據您的需求變更完整檔案路徑(磁碟機、資料夾和 Zip 檔案名稱)。
答案2
在 Windows 10 build 17063 或更高版本上,您可以使用tar.exe
(類似 *nix)。 Nanoserver docker 容器中也提供了此功能
C:\> tar -xf archive.zip
注意:zip 支援沒有詳細記錄
參考:https://www.freebsd.org/cgi/man.cgi?query=bsdtar&sektion=1&manpath=FreeBSD+5.3-stable
答案3
如果您使用的是 Windows 10,則可以使用更短的 Powershell 等效項
Expand-Archive -Force C:\path\to\archive.zip C:\where\to\extract\to
答案4
ZipFile="C:\Users\spvaidya\Music\folder.zip"
ExtractTo="C:\Users\spvaidya\Music\"
'If the extraction location does not exist create it.
Set fso = CreateObject("Scripting.FileSystemObject")
If NOT fso.FolderExists(ExtractTo) Then
fso.CreateFolder(ExtractTo)
End If
'Extract the contants of the zip file.
set objShell = CreateObject("Shell.Application")
set FilesInZip=objShell.NameSpace(ZipFile).items
objShell.NameSpace(ExtractTo).CopyHere(FilesInZip)
Set fso = Nothing
Set objShell = Nothing
以下 vbscript 可以儲存為 file.vbs,然後使用批次腳本執行,例如:
file.vbs
將其保存在 .bat 檔案中並運行它。