小编Vee*_*Vee的帖子

Powershell [System.IO.Compression.ZipArchiveEntry] 不包含名为“ExtractToFile”的方法

我有一个 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()
Run Code Online (Sandbox Code Playgroud)

.net powershell scripting

3
推荐指数
1
解决办法
1855
查看次数

标签 统计

.net ×1

powershell ×1

scripting ×1