[Powershell] 如何解壓檔案

在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
}

 

About C.H. Ling 262 Articles
a .net / Java developer from Hong Kong and currently located in United Kingdom. Thanks for Google because it solve many technical problems so I build this blog as return. Besides coding and trying advance technology, hiking and traveling is other favorite to me, so I will write down something what I see and what I feel during it. Happy reading!!!

Be the first to comment

Leave a Reply

Your email address will not be published.


*


This site uses Akismet to reduce spam. Learn how your comment data is processed.