我有一个PowerShell脚本,我想提取特定的条目。我似乎无法弄清楚如何在.NET ZipArchiveEntry对象上找不到扩展方法的错误。
我的下面的脚本会运行,并且会写出"写出"来自zip的文件,但当它到达 $ entry.ExtractToFile($ dst)行时,会产生以下错误:
方法调用失败,因为[System.IO.Compression.ZipArchiveEntry]不包含名为' ExtractToFile'的方法。
Add-Type -AssemblyName System.IO.Compression -ErrorAction Stop
Add-Type -AssemblyName System.IO.Compression.FileSystem -ErrorAction Stop
$filesToExtract = @(
"appsettings.json";
"Content\cms\index.xml";
)
$archive = "C:\Users\User1\Desktop\archive.zip"
$dstRoot = "C:\Users\User1\Desktop\test"
$zip = [IO.Compression.ZipFile]::OpenRead($archive)
foreach($entry in $zip.Entries){
if ($filesToExtract -contains $entry.FullName){
$dst = [io.path]::combine($dstRoot, $entry.FullName)
%{ "Extract ==> " + $dst }
$entry.ExtractToFile($dst)
}
}
$zip.Dispose()
答案 0 :(得分:3)
尝试使用[System.IO.Compression.ZipFileExtensions]。如果提取文件已经存在,它将失败。
[cmdletbinding()]
Param()
Add-Type -AssemblyName System.IO.Compression.FileSystem -ErrorAction Stop
$filesToExtract = @(
"t.txt";
)
$archive = "$PSScriptRoot\t.zip"
$dstRoot = "$PSScriptRoot"
$zip = [System.IO.Compression.ZipFile]::OpenRead($archive)
foreach($entry in $zip.Entries){
if ($filesToExtract -contains $entry.FullName){
$dst = [io.path]::combine($dstRoot, $entry.FullName)
if (Test-Path -Path $dst) { Remove-Item -Path $dst }
Write-Verbose $("Extract ==> {0}" -f @($dst))
[System.IO.Compression.ZipFileExtensions]::ExtractToFile($entry, $dst)
}
}
$zip.Dispose()