通过 Powershell 从 zip 中提取某个文件似乎不在子文件夹中查找

Extract a certain file from zip via Powershell seems like not to look in sub folders

我对 Powershell 非常陌生,尤其是对 Powershell 和 ZIP 文件。我想从传递的 zipfile 中解压缩特定文件。为此,我有以下代码。下面的代码应该从 zip 中得到一个 *.update

我遇到的问题是特定文件位于另一个文件夹中。 运行 脚本似乎不会在 zip 文件夹中查找更多文件。

我已经在 $item 上尝试了 GetFolder and/or foreach 通过 $item。到目前为止没有成功。有人有想法或方向吗?

function ExtractFromZip ($File, $Destination) {
    $ShellApp = new-object -com shell.application
    $zipFile = $ShellApp.NameSpace($File)
    foreach($item in $zipFile.Items())
    {
        Write-Host $item.Name

        if ($item.GetFolder -ne $Null) {
            Write-Host "test"
        }

        if ($item.Name -like "*.update") {
            $ShellApp.Namespace($Destination).copyhere($item)
            break;    
        }
    }
}

使用下面的脚本解决了这个问题:

Add-Type -Path 'C:\dev\Libraries\DotNetZip\Ionic.Zip.dll'

$zip = [Ionic.Zip.ZIPFile]::Read($sourceFile)
foreach ($file in $zip.Entries) {    
if ($file -like "*.update") {
        $zip | %{$file.Extract("C:\temp\test", [Ionic.Zip.ExtractExistingFileAction]::OverWriteSilently)}
    }
}

以下是在较新版本的 Powershell 中本机执行此操作的方法:

Add-Type -Assembly System.IO.Compression.FileSystem
$zip = [IO.Compression.ZipFile]::OpenRead($sourceFile)
$zip.Entries | where {$_.Name -like '*.update'} | foreach {[System.IO.Compression.ZipFileExtensions]::ExtractToFile($_, "C:\temp\test", $true)}
$zip.Dispose()