在Linux 中, 解壓檔案可以利用 tar 或unzip 指令, 但在Powershell 中沒有相對的指令, 須要自行建立.可以透過.net 的ExtractToDirectory() 實現. 方法如下:
Add-Type -AssemblyName System.IO.Compression.FileSystem
function Unzip
{
param([string]$zipfile, [string]$outpath)
[System.IO.Compression.ZipFile]::ExtractToDirectory($zipfile, $outpath)
}
Add-Type -AssemblyName System.IO.Compression.FileSystem
function Unzip
{
param([string]$zipfile, [string]$outpath)
[System.IO.Compression.ZipFile]::ExtractToDirectory($zipfile, $outpath)
}
然而, 若重覆解壓時, 會因為相同名字而throw exception. 在Powershell 5中, 已經有command 可以直接override它.
function Unzip
{
param([string]$zipfile, [string]$outpath)
Expand-Archive -Path $zipfile -DestinationPath $outpath -Force
}
Leave a Reply