2015-04-01 100 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 +