Skip to content

Commit

Permalink
fix(decompress): '$ExtractDir' error with '.zip' and subdir (ScoopIns…
Browse files Browse the repository at this point in the history
…taller#3472)

For some `extract_dir` in .zip that has sub-dir, the former one would remove parent dir and get error (flac, flac-1.3.2-win\win64), now use temp dir instead. And also fix .msi, in case of some installer that don't have `SourceDir`.

`Expand-DarkArchive` will be used in wix extraction, so it doesn't need `-ExtractDir`.

`Expand-7zipArchive` and `Expand-InnoArchive` works well.

Tested:

- calibre-normal (msi, PFiles\Calibre2)
- flac (zip, flac-1.3.2-win\win64)
- pkg-config (zip, multiple url and extract_dir)

BTW, `Expand-Archive` has `-PassThru` param in PowerShell 6+, and it will be more convinient to support `-ExtractDir` with it. I'll check function's source code and plan to rewrite `Expand-ZipArchive`.

Fix ScoopInstaller#3473, ref ScoopInstaller#3473 (comment)
  • Loading branch information
niheaven authored and r15ch13 committed May 17, 2019
1 parent f5fdc27 commit a775d45
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions lib/decompress.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@ function Expand-MsiArchive {
$Removal
)
$DestinationPath = $DestinationPath.TrimEnd("\")
if ($ExtractDir) {
$OriDestinationPath = $DestinationPath
$DestinationPath = "$DestinationPath\_tmp"
}
if ((get_config MSIEXTRACT_USE_LESSMSI)) {
$MsiPath = Get-HelperPath -Helper Lessmsi
$ArgList = @('x', "`"$Path`"", "`"$DestinationPath\\`"")
Expand All @@ -138,11 +142,11 @@ function Expand-MsiArchive {
abort "Failed to extract files from $Path.`nLog file:`n $(friendly_path $LogPath)`n$(new_issue_msg $app $bucket 'decompress error')"
}
if ($ExtractDir -and (Test-Path "$DestinationPath\SourceDir")) {
movedir "$DestinationPath\SourceDir\$ExtractDir" $DestinationPath | Out-Null
Remove-Item "$DestinationPath\SourceDir" -Recurse -Force
movedir "$DestinationPath\SourceDir\$ExtractDir" $OriDestinationPath | Out-Null
Remove-Item $DestinationPath -Recurse -Force
} elseif ($ExtractDir) {
Get-ChildItem $DestinationPath -Exclude $ExtractDir, $Path | Remove-Item -Recurse -Force
movedir "$DestinationPath\$ExtractDir" $DestinationPath | Out-Null
movedir "$DestinationPath\$ExtractDir" $OriDestinationPath | Out-Null
Remove-Item $DestinationPath -Recurse -Force
} elseif (Test-Path "$DestinationPath\SourceDir") {
movedir "$DestinationPath\SourceDir" $DestinationPath | Out-Null
}
Expand Down Expand Up @@ -207,6 +211,10 @@ function Expand-ZipArchive {
[Switch]
$Removal
)
if ($ExtractDir) {
$OriDestinationPath = $DestinationPath
$DestinationPath = "$DestinationPath\_tmp"
}
# All methods to unzip the file require .NET4.5+
if ($PSVersionTable.PSVersion.Major -lt 5) {
Add-Type -AssemblyName System.IO.Compression.FileSystem
Expand Down Expand Up @@ -236,8 +244,8 @@ function Expand-ZipArchive {
Microsoft.PowerShell.Archive\Expand-Archive -Path $Path -DestinationPath $DestinationPath -Force
}
if ($ExtractDir) {
Get-ChildItem $DestinationPath -Exclude $ExtractDir, $Path | Remove-Item -Recurse -Force
movedir "$DestinationPath\$ExtractDir" $DestinationPath | Out-Null
movedir "$DestinationPath\$ExtractDir" $OriDestinationPath | Out-Null
Remove-Item $DestinationPath -Recurse -Force
}
if ($Removal) {
# Remove original archive file
Expand All @@ -254,15 +262,12 @@ function Expand-DarkArchive {
[Parameter(Position = 1)]
[String]
$DestinationPath = (Split-Path $Path),
[String]
$ExtractDir,
[Parameter(ValueFromRemainingArguments = $true)]
[String]
$Switches,
[Switch]
$Removal
)
$DestinationPath = $DestinationPath.TrimEnd("\")
$LogPath = "$(Split-Path $Path)\dark.log"
$ArgList = @('-nologo', "-x `"$DestinationPath`"", "`"$Path`"")
if ($Switches) {
Expand All @@ -275,10 +280,6 @@ function Expand-DarkArchive {
if (Test-Path $LogPath) {
Remove-Item $LogPath -Force
}
if ($ExtractDir) {
Get-ChildItem $DestinationPath -Exclude $ExtractDir, $Path | Remove-Item -Recurse -Force
movedir "$DestinationPath\$ExtractDir" $DestinationPath | Out-Null
}
if ($Removal) {
# Remove original archive file
Remove-Item $Path -Force
Expand Down

0 comments on commit a775d45

Please sign in to comment.