Skip to content

Commit 7f99c49

Browse files
o-l-a-vniheaven
andauthored
fix (decompress): Expand-7zipArchive only delete temp dir / $extractDir if it is empty (#6092)
Co-authored-by: Hsiao-nan Cheung <[email protected]>
1 parent 859d1db commit 7f99c49

File tree

4 files changed

+22
-5
lines changed

4 files changed

+22
-5
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
## [Unreleased](https://github.com/ScoopInstaller/Scoop/compare/master...develop)
2+
3+
### Bug Fixes
4+
5+
- **decompress**: `Expand-7zipArchive` Only delete temp dir / `$extractDir` if it is empty ([#6092](https://github.com/ScoopInstaller/Scoop/issues/6092))
6+
17
## [v0.5.2](https://github.com/ScoopInstaller/Scoop/compare/v0.5.1...v0.5.2) - 2024-07-26
28

39
### Bug Fixes

lib/decompress.ps1

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,15 +123,18 @@ function Expand-7zipArchive {
123123
}
124124
if (!$IsTar -and $ExtractDir) {
125125
movedir "$DestinationPath\$ExtractDir" $DestinationPath | Out-Null
126-
# Remove temporary directory
127-
Remove-Item "$DestinationPath\$($ExtractDir -replace '[\\/].*')" -Recurse -Force -ErrorAction Ignore
126+
# Remove temporary directory if it is empty
127+
$ExtractDirTopPath = [string] "$DestinationPath\$($ExtractDir -replace '[\\/].*')"
128+
if ((Get-ChildItem -Path $ExtractDirTopPath -Force -ErrorAction Ignore).Count -eq 0) {
129+
Remove-Item -Path $ExtractDirTopPath -Recurse -Force -ErrorAction Ignore
130+
}
128131
}
129132
if (Test-Path $LogPath) {
130133
Remove-Item $LogPath -Force
131134
}
132135
if ($Removal) {
133136
if (($Path -replace '.*\.([^\.]*)$', '$1') -eq '001') {
134-
# Remove splited 7-zip archive parts
137+
# Remove splitted 7-zip archive parts
135138
Get-ChildItem "$($Path -replace '\.[^\.]*$', '').???" | Remove-Item -Force
136139
} elseif (($Path -replace '.*\.part(\d+)\.rar$', '$1')[-1] -eq '1') {
137140
# Remove splitted RAR archive parts

test/Scoop-Decompress.Tests.ps1

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ Describe 'Decompression function' -Tag 'Scoop', 'Windows', 'Decompress' {
2525
}
2626
It 'Test cases should exist and hash should match' {
2727
$testcases | Should -Exist
28-
(Get-FileHash -Path $testcases -Algorithm SHA256).Hash.ToLower() | Should -Be 'afb86b0552187b8d630ce25d02835fb809af81c584f07e54cb049fb74ca134b6'
28+
(Get-FileHash -Path $testcases -Algorithm SHA256).Hash.ToLower() | Should -Be '591072faabd419b77932b7023e5899b4e05c0bf8e6859ad367398e6bfe1eb203'
2929
}
3030
It 'Test cases should be extracted correctly' {
3131
{ Microsoft.PowerShell.Archive\Expand-Archive -Path $testcases -DestinationPath $working_dir } | Should -Not -Throw
@@ -61,7 +61,7 @@ Describe 'Decompression function' -Tag 'Scoop', 'Windows', 'Decompress' {
6161
$to = test_extract 'Expand-7zipArchive' $test1
6262
$to | Should -Exist
6363
"$to\empty" | Should -Exist
64-
(Get-ChildItem $to).Count | Should -Be 3
64+
(Get-ChildItem $to).Count | Should -Be 4
6565
}
6666

6767
It 'extract "extract_dir" correctly' {
@@ -78,6 +78,14 @@ Describe 'Decompression function' -Tag 'Scoop', 'Windows', 'Decompress' {
7878
(Get-ChildItem $to).Count | Should -Be 1
7979
}
8080

81+
It 'extract "extract_dir" with nested folder with same name' {
82+
$to = test_extract 'Expand-7zipArchive' $test1 $false 'keep\sub'
83+
$to | Should -Exist
84+
"$to\keep\empty" | Should -Exist
85+
(Get-ChildItem $to).Count | Should -Be 1
86+
(Get-ChildItem "$to\keep").Count | Should -Be 1
87+
}
88+
8189
It 'extract nested compressed file' {
8290
# file ext: tgz
8391
$to = test_extract 'Expand-7zipArchive' $test2
66 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)