首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Powershell [System.IO.Compression.ZipArchiveEntry]不包含名为“ExtractToFile”的方法

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

Stack Overflow用户
提问于 2018-06-09 23:17:57
回答 1查看 2K关注 0票数 3

我有一个powershell脚本,我想在其中提取特定的条目。我似乎想不出如何克服在.NET ZipArchiveEntry对象上找不到扩展方法的错误。

我的下面的脚本将运行,并从压缩文件中“写出”文件,但当它到达$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()
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-10 00:08:07

尝试使用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()
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50775744

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档