Fix #6515 "Expand-7zipArchive won't remove top folder if $ExtractDir depth exceeds 2" - Idea 2#6517
Conversation
WalkthroughRefactors Expand-7zipArchive to invoke 7z/tar with explicit -FilePath and arguments, standardizes path/log cmdlet parameter usage, and fixes extract-dir cleanup by using a depth-based iterative removal so the top folder is removed correctly when ExtractDir depth exceeds two. Changes
Sequence Diagram(s)sequenceDiagram
autonumber
participant Caller as Caller
participant Expand as Expand-7zipArchive
participant CLI as 7z/tar CLI
participant FS as File System
Caller->>Expand: extract(archive, destination, ExtractDir)
note right of Expand: Resolve paths, construct log path (using Split-Path)
Expand->>FS: Pre-check existing directories (recursive checks)
Expand->>CLI: Invoke via -FilePath with explicit ArgumentList (list/extract)
CLI-->>Expand: Exit code & output
alt success
Expand->>FS: Depth-based iterative cleanup (remove empty segments from top until leaf content)
note right of FS: Stop when non-empty or reached intended depth
Expand-->>Caller: success
else error
Expand-->>Caller: error/status
end
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Poem
Pre-merge checks and finishing touches❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (1)
lib/decompress.ps1 (1)
124-145: Core bug fix looks correct with minor suggestions.The depth-based iterative cleanup algorithm correctly addresses the issue where parent directories weren't being removed when
$ExtractDirdepth exceeds 2. The logic properly checks each directory level from deepest to shallowest and stops when encountering a non-empty directory.Minor suggestions for robustness:
Line 130: Consider using
[int]instead of[byte]for$Depthto avoid the 255-level limit, though this should be sufficient for all practical cases.Line 136: The
.Countproperty might not exist whenGet-ChildItemreturns a single item in PowerShell versions prior to 6. Consider wrapping in@()for consistency:- if ((Get-ChildItem -Path $CurrentDir -Force -ErrorAction 'Ignore').'Count' -gt 0) { + if (@(Get-ChildItem -Path $CurrentDir -Force -ErrorAction 'Ignore').Count -gt 0) {Lines 131-135: The path construction is correct but could be simplified using
Join-Path:- $CurrentDir = [string] [System.IO.Path]::Combine( - $DestinationPath, ( - ($ExtractDirs | Select-Object -First $Depth) -join [System.IO.Path]::DirectorySeparatorChar - ) - ) + $CurrentDir = Join-Path $DestinationPath (($ExtractDirs | Select-Object -First $Depth) -join [System.IO.Path]::DirectorySeparatorChar)
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
CHANGELOG.md(1 hunks)lib/decompress.ps1(3 hunks)
🔇 Additional comments (4)
CHANGELOG.md (1)
18-18: LGTM! Changelog entry is clear and accurate.The bug fix entry correctly describes the issue and references the appropriate GitHub issue.
lib/decompress.ps1 (3)
94-94: LGTM! Explicit parameter names improve clarity.The changes to use explicit parameter names (
-FilePath,-Path) and-notinstead of!follow PowerShell best practices and make the code more readable.Also applies to: 109-111, 115-115
146-148: LGTM! Improved log file cleanup safety.Adding the
-PathType 'Leaf'check ensures only actual files are removed, preventing potential issues if the log path unexpectedly becomes a directory.
152-152: LGTM! Consistent use of explicit parameters.Adding explicit
-Pathparameters toGet-ChildItemandRemove-Itemcalls maintains consistency with the other improvements in this file and follows PowerShell best practices.Also applies to: 155-155, 158-158
Description
Make
Expand-7zipArchive -ExtractDirhandle$ExtractDirdepth of two or more.This PR tries to fix it in a non-breaking way, unlike #6516.
Motivation and Context
Closes #6515
How Has This Been Tested?
Checklist:
developbranch.Summary by CodeRabbit
Bug Fixes
Refactor
Documentation