如何在 Powershell 中壓縮/解壓縮檔案?

如何在 Powershell 中壓縮/解壓縮檔案?

是否有一種可以在 PowerShell 中壓縮/解壓縮檔案 (*.zip) 的單行程式碼?

答案1

點網壓縮將允許您從 PowerShell 執行此操作。它不是一句簡單的話,但該程式庫將允許您編寫所需的 PowerShell 腳本。

您也可以使用 COM 接口,請參閱使用 Windows PowerShell 壓縮文件,然後打包 Windows Vista 側邊欄小工具

谷歌搜尋“zip powershell”或“unzip powershell”也可能會出現有用的結果。

答案2

這就是您無需任何外部工具即可純粹透過 Powershell 完成此操作的方法。這會將名為 test.zip 的檔案解壓縮到目前工作目錄中:

$shell_app=new-object -com shell.application
$filename = "test.zip"
$zip_file = $shell_app.namespace((Get-Location).Path + "\$filename")
$destination = $shell_app.namespace((Get-Location).Path)
$destination.Copyhere($zip_file.items())

答案3

現在在 .NET Framework 4.5 中,有一個壓縮檔案您可以像這樣使用的類別:

[System.Reflection.Assembly]::LoadWithPartialName('System.IO.Compression.FileSystem')
[System.IO.Compression.ZipFile]::ExtractToDirectory($sourceFile, $targetFolder)

答案4

您可能想查看PowerShell 社群擴展 (PSCX)其中有專門用於此目的的 cmdlet。

相關內容