Skip to content

Commit e6b2e3d

Browse files
niheavenr15ch13
authored andcommitted
feature(decompress): Add 'ExtractDir' to 'Expand-...' functions (#3466)
* feature(decompress): Add 'ExtractDir' to 'Expand-...' functions * Fix 'TrimEnd' and nested 7z
1 parent b9deb57 commit e6b2e3d

File tree

2 files changed

+45
-37
lines changed

2 files changed

+45
-37
lines changed

lib/decompress.ps1

+44-11
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ function Expand-7zipArchive {
4444
[Parameter(Position = 1)]
4545
[String]
4646
$DestinationPath = (Split-Path $Path),
47+
[String]
48+
$ExtractDir,
4749
[Parameter(ValueFromRemainingArguments = $true)]
4850
[String]
4951
$Switches,
@@ -64,6 +66,10 @@ function Expand-7zipArchive {
6466
}
6567
$LogPath = "$(Split-Path $Path)\7zip.log"
6668
$ArgList = @('x', "`"$Path`"", "-o`"$DestinationPath`"", '-y')
69+
$IsTar = ((strip_ext $Path) -match '\.tar$') -or ($Path -match '\.t[abgpx]z2?$')
70+
if (!$IsTar -and $ExtractDir) {
71+
$ArgList += "-ir!$ExtractDir\*"
72+
}
6773
if ($Switches) {
6874
$ArgList += (-split $Switches)
6975
}
@@ -74,17 +80,20 @@ function Expand-7zipArchive {
7480
}
7581
$Status = Invoke-ExternalCommand $7zPath $ArgList -LogPath $LogPath
7682
if (!$Status) {
77-
abort "Failed to extract files from $Path.`nLog file:`n $(friendly_path $LogPath)"
83+
abort "Failed to extract files from $Path.`nLog file:`n $(friendly_path $LogPath)`n$(new_issue_msg $app $bucket 'decompress error')"
84+
}
85+
if (!$IsTar -and $ExtractDir) {
86+
movedir "$DestinationPath\$ExtractDir" $DestinationPath | Out-Null
7887
}
7988
if (Test-Path $LogPath) {
8089
Remove-Item $LogPath -Force
8190
}
82-
if ((strip_ext $Path) -match '\.tar$' -or $Path -match '\.t[abgpx]z2?$') {
91+
if ($IsTar) {
8392
# Check for tar
8493
$Status = Invoke-ExternalCommand $7zPath @('l', "`"$Path`"") -LogPath $LogPath
8594
if ($Status) {
8695
$TarFile = (Get-Content -Path $LogPath)[-4] -replace '.{53}(.*)', '$1' # get inner tar file name
87-
Expand-7zipArchive "$DestinationPath\$TarFile" $DestinationPath -Removal
96+
Expand-7zipArchive -Path "$DestinationPath\$TarFile" -DestinationPath $DestinationPath -ExtractDir $ExtractDir -Removal
8897
} else {
8998
abort "Failed to list files in $Path.`nNot a 7-Zip supported archive file."
9099
}
@@ -104,29 +113,38 @@ function Expand-MsiArchive {
104113
[Parameter(Position = 1)]
105114
[String]
106115
$DestinationPath = (Split-Path $Path),
116+
[String]
117+
$ExtractDir,
107118
[Parameter(ValueFromRemainingArguments = $true)]
108119
[String]
109120
$Switches,
110121
[Switch]
111122
$Removal
112123
)
124+
$DestinationPath = $DestinationPath.TrimEnd("\")
113125
if ((get_config MSIEXTRACT_USE_LESSMSI)) {
114126
$MsiPath = Get-HelperPath -Helper Lessmsi
115127
$ArgList = @('x', "`"$Path`"", "`"$DestinationPath\\`"")
116128
} else {
117129
$MsiPath = 'msiexec.exe'
118-
$ArgList = @('/a', "`"$Path`"", '/qn', "TARGETDIR=`"$DestinationPath`"")
130+
$ArgList = @('/a', "`"$Path`"", '/qn', "TARGETDIR=`"$DestinationPath\\SourceDir`"")
119131
}
120132
$LogPath = "$(Split-Path $Path)\msi.log"
121133
if ($Switches) {
122134
$ArgList += (-split $Switches)
123135
}
124136
$Status = Invoke-ExternalCommand $MsiPath $ArgList -LogPath $LogPath
125137
if (!$Status) {
126-
abort "Failed to extract files from $Path.`nLog file:`n $(friendly_path $LogPath)"
138+
abort "Failed to extract files from $Path.`nLog file:`n $(friendly_path $LogPath)`n$(new_issue_msg $app $bucket 'decompress error')"
127139
}
128-
if (Test-Path "$DestinationPath\SourceDir") {
129-
movedir "$DestinationPath\SourceDir" "$DestinationPath" | Out-Null
140+
if ($ExtractDir -and (Test-Path "$DestinationPath\SourceDir")) {
141+
movedir "$DestinationPath\SourceDir\$ExtractDir" $DestinationPath | Out-Null
142+
Remove-Item "$DestinationPath\SourceDir" -Recurse -Force
143+
} elseif ($ExtractDir) {
144+
Get-ChildItem $DestinationPath -Exclude $ExtractDir | Remove-Item -Recurse -Force
145+
movedir "$DestinationPath\$ExtractDir" $DestinationPath | Out-Null
146+
} elseif (Test-Path "$DestinationPath\SourceDir") {
147+
movedir "$DestinationPath\SourceDir" $DestinationPath | Out-Null
130148
}
131149
if (($DestinationPath -ne (Split-Path $Path)) -and (Test-Path "$DestinationPath\$(fname $Path)")) {
132150
Remove-Item "$DestinationPath\$(fname $Path)" -Force
@@ -149,20 +167,22 @@ function Expand-InnoArchive {
149167
[Parameter(Position = 1)]
150168
[String]
151169
$DestinationPath = (Split-Path $Path),
170+
[String]
171+
$ExtractDir,
152172
[Parameter(ValueFromRemainingArguments = $true)]
153173
[String]
154174
$Switches,
155175
[Switch]
156176
$Removal
157177
)
158178
$LogPath = "$(Split-Path $Path)\innounp.log"
159-
$ArgList = @('-x', "-d`"$DestinationPath`"", "-c`{app`}", "`"$Path`"", '-y')
179+
$ArgList = @('-x', "-d`"$DestinationPath`"", "-c`{app`}\$ExtractDir", "`"$Path`"", '-y')
160180
if ($Switches) {
161181
$ArgList += (-split $Switches)
162182
}
163183
$Status = Invoke-ExternalCommand (Get-HelperPath -Helper Innounp) $ArgList -LogPath $LogPath
164184
if (!$Status) {
165-
abort "Failed to extract files from $Path.`nLog file:`n $(friendly_path $LogPath)"
185+
abort "Failed to extract files from $Path.`nLog file:`n $(friendly_path $LogPath)`n$(new_issue_msg $app $bucket 'decompress error')"
166186
}
167187
if (Test-Path $LogPath) {
168188
Remove-Item $LogPath -Force
@@ -182,6 +202,8 @@ function Expand-ZipArchive {
182202
[Parameter(Position = 1)]
183203
[String]
184204
$DestinationPath = (Split-Path $Path),
205+
[String]
206+
$ExtractDir,
185207
[Switch]
186208
$Removal
187209
)
@@ -213,6 +235,10 @@ function Expand-ZipArchive {
213235
# Compatible with Pscx (https://github.com/Pscx/Pscx)
214236
Microsoft.PowerShell.Archive\Expand-Archive -Path $Path -DestinationPath $DestinationPath -Force
215237
}
238+
if ($ExtractDir) {
239+
Get-ChildItem $DestinationPath -Exclude $ExtractDir | Remove-Item -Recurse -Force
240+
movedir "$DestinationPath\$ExtractDir" $DestinationPath | Out-Null
241+
}
216242
if ($Removal) {
217243
# Remove original archive file
218244
Remove-Item $Path -Force
@@ -221,31 +247,38 @@ function Expand-ZipArchive {
221247

222248
function Expand-DarkArchive {
223249
[CmdletBinding()]
224-
param(
250+
param (
225251
[Parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true)]
226252
[String]
227253
$Path,
228254
[Parameter(Position = 1)]
229255
[String]
230256
$DestinationPath = (Split-Path $Path),
257+
[String]
258+
$ExtractDir,
231259
[Parameter(ValueFromRemainingArguments = $true)]
232260
[String]
233261
$Switches,
234262
[Switch]
235263
$Removal
236264
)
265+
$DestinationPath = $DestinationPath.TrimEnd("\")
237266
$LogPath = "$(Split-Path $Path)\dark.log"
238267
$ArgList = @('-nologo', "-x `"$DestinationPath`"", "`"$Path`"")
239268
if ($Switches) {
240269
$ArgList += (-split $Switches)
241270
}
242271
$Status = Invoke-ExternalCommand (Get-HelperPath -Helper Dark) $ArgList -LogPath $LogPath
243272
if (!$Status) {
244-
abort "Failed to extract files from $Path.`nLog file:`n $(friendly_path $LogPath)"
273+
abort "Failed to extract files from $Path.`nLog file:`n $(friendly_path $LogPath)`n$(new_issue_msg $app $bucket 'decompress error')"
245274
}
246275
if (Test-Path $LogPath) {
247276
Remove-Item $LogPath -Force
248277
}
278+
if ($ExtractDir) {
279+
Get-ChildItem $DestinationPath -Exclude $ExtractDir | Remove-Item -Recurse -Force
280+
movedir "$DestinationPath\$ExtractDir" $DestinationPath | Out-Null
281+
}
249282
if ($Removal) {
250283
# Remove original archive file
251284
Remove-Item $Path -Force

lib/install.ps1

+1-26
Original file line numberDiff line numberDiff line change
@@ -555,33 +555,8 @@ function dl_urls($app, $version, $manifest, $bucket, $architecture, $dir, $use_c
555555
Write-Host "Extracting " -NoNewline
556556
Write-Host $fname -f Cyan -NoNewline
557557
Write-Host " ... " -NoNewline
558-
ensure "$dir\_tmp" | Out-Null
559-
& $extract_fn "$dir\$fname" "$dir\_tmp" -Removal
560-
if ($extract_to) {
561-
ensure "$dir\$extract_to" | Out-Null
562-
}
563-
# fails if zip contains long paths (e.g. atom.json)
564-
#cp "$dir\_tmp\$extract_dir\*" "$dir\$extract_to" -r -force -ea stop
565-
try {
566-
movedir "$dir\_tmp\$extract_dir" "$dir\$extract_to"
567-
}
568-
catch {
569-
error $_
570-
abort $(new_issue_msg $app $bucket "extract_dir error")
571-
}
572-
573-
if(Test-Path "$dir\_tmp") { # might have been moved by movedir
574-
try {
575-
Remove-Item -r -force "$dir\_tmp" -ea stop
576-
} catch [system.io.pathtoolongexception] {
577-
& "$env:COMSPEC" /c "rmdir /s /q $dir\_tmp"
578-
} catch [system.unauthorizedaccessexception] {
579-
warn "Couldn't remove $dir\_tmp: unauthorized access."
580-
}
581-
}
582-
558+
& $extract_fn -Path "$dir\$fname" -DestinationPath "$dir\$extract_to" -ExtractDir $extract_dir -Removal
583559
Write-Host "done." -f Green
584-
585560
$extracted++
586561
}
587562
}

0 commit comments

Comments
 (0)