refactor: 两个入口统一 7z 的查找方式(消除“备份找得到、恢复找不到”的隐患)
原先同一段查找逻辑有两份:模块的 Resolve-BakNRetCompressionTool 里一份,Restore.ps1 自带的 Get-7zExecutable 里又一份(PATH → Program Files → Program Files (x86))。两套写法哪怕只差一个候选路径,就会出现“备份找得到、恢复找不到”这种最难看的不一致。 抽成 Find-BakNRet7zExecutable,两边都改用它。**保持策略不同**:备份可以退到 RAR 或内置 ZIP(能打包就行),恢复必须真能解压 7z、找不到就明确报错 —— 所以抽出来的只负责“找 7z”,不负责“找不到怎么办”。我自己在抽之前差点把它当成顺手删的重复代码,看清楚才发现策略差异是刻意的。 验收:test.ps1 9/9 全绿(7 与 5.1)、103 个文件两版解析零错、真实清单只读冒烟 4/4、构建工具仍能合回单文件。当前查找器在本机解析到 C:\Programs\Scoop\shims\7z.exe,备份侧解析器仍返回 7z。
This commit is contained in:
1 parent
fcf83ecb74
commit
67ee2af006
5 files changed
+31
-24
No files matched your search
+3
-15
@@ -163,18 +163,6 @@ function Get-ArchiveForEntry {
|
||||
return $null
|
||||
}
|
||||
|
||||
function Get-7zExecutable {
|
||||
<# .SYNOPSIS 定位 7z.exe(PATH 优先,其次是常见安装位置)。 #>
|
||||
$sevenZip = Get-Command 7z -ErrorAction SilentlyContinue | Select-Object -First 1 -ExpandProperty Source
|
||||
if (-not $sevenZip) {
|
||||
$candidates = @(
|
||||
(Join-Path $env:ProgramFiles '7-Zip\7z.exe'),
|
||||
(Join-Path ${env:ProgramFiles(x86)} '7-Zip\7z.exe')
|
||||
)
|
||||
$sevenZip = $candidates | Where-Object { $_ -and (Test-Path -LiteralPath $_) } | Select-Object -First 1
|
||||
}
|
||||
return $sevenZip
|
||||
}
|
||||
|
||||
function Invoke-ExtractionRaw {
|
||||
<#
|
||||
@@ -197,7 +185,7 @@ function Invoke-ExtractionRaw {
|
||||
New-Item -ItemType Directory -Path $Destination -Force | Out-Null
|
||||
}
|
||||
|
||||
$sevenZip = Get-7zExecutable
|
||||
$sevenZip = Find-BakNRet7zExecutable
|
||||
if ($sevenZip) {
|
||||
Write-BakNRetLog '使用 7z 解压' -Level DEBUG
|
||||
$argument = @('x', '-bsp2', '-y', "-o$Destination")
|
||||
@@ -371,7 +359,7 @@ function Test-BakNRetArchivePath {
|
||||
[string]$Password
|
||||
)
|
||||
|
||||
$sevenZip = Get-7zExecutable
|
||||
$sevenZip = Find-BakNRet7zExecutable
|
||||
if (-not $sevenZip) { return $true } # 没有 7z 时不预判,交给解压分支自己去失败
|
||||
|
||||
$item = ([string]$RelativePath).Trim([char[]]@('\', '/'))
|
||||
@@ -646,7 +634,7 @@ foreach ($line in $lines) {
|
||||
Write-BakNRetLog "跳过校验(非 7z): $($archiveFile.Name)" -Level DEBUG
|
||||
continue
|
||||
}
|
||||
$verifyTool = Get-7zExecutable
|
||||
$verifyTool = Find-BakNRet7zExecutable
|
||||
if (-not $verifyTool) {
|
||||
Write-BakNRetLog '未找到 7z,无法校验' -Level ERROR
|
||||
$stats.failed++
|
||||
|
||||
Reference in new issue
Block a user