2015-04-01 114 views
2

我試圖解壓縮壓縮文件。我寫的代碼如下Powershell解壓縮包含大量文件的zip文件

function Unzip($sourceZip, $destination) { 

     $shell = new-object -com shell.application 
     $zip = $shell.NameSpace($sourceZip) 
     $dest = $shell.NameSpace($destination) 
     foreach($item in $zip.items()) 
     { 
      $dest.Copyhere($item, 0x14) 
     } 
} 

,我號召其包含大約14K的文件,如下

Unzip "Source.zip" "Destination" 

此文件被卡住了超過30分鐘的輸入zip文件這個腳本。而且還沒有進展。

從我發現它得到這一行$zip.items()它可能需要很長的時間來發現所有的文件。

有沒有更好的,最佳的方式來做到這一點?

在此先感謝。問候,Ganesh。

回答

0

你可以安裝類似7zip這樣的命令行工具來做這種事情,但我不知道任何更好的本地方式。

+0

不幸的是,我無法安裝7zip的在每臺將運行此腳本的計算機上運行 – 2015-04-01 10:46:33

+0

不必安裝,如果無法將其複製到每臺計算機上,則可以在某個地方使用獨立控制檯版本。但我會走這條路線作爲最後的手段。 – 2015-04-01 11:26:49

2

你可以用它做System.IO.Compression(需要.NET 4.5):

[System.Reflection.Assembly]::LoadWithPartialName('System.IO.Compression.FileSystem') 
[System.IO.Compression.ZipFile]::ExtractToDirectory($sourceZip, $destination) 
0
[CmdletBinding()] 
param(
    [string] 
    $Source, 

    [string] 
    $Destination 
) 

Write-Verbose "Loading System.IO.Compression.FileSystem Assembly" 
[System.Reflection.Assembly]::LoadWithPartialName('System.IO.Compression.FileSystem') 

Write-Verbose "Unpacking $Source to $Destination" 
[System.IO.Compression.ZipFile]::ExtractToDirectory($Source, $Destination) 

但它需要.net4.5 +