@@ -44,6 +44,8 @@ function Expand-7zipArchive {
44
44
[Parameter (Position = 1 )]
45
45
[String ]
46
46
$DestinationPath = (Split-Path $Path ),
47
+ [String ]
48
+ $ExtractDir ,
47
49
[Parameter (ValueFromRemainingArguments = $true )]
48
50
[String ]
49
51
$Switches ,
@@ -64,6 +66,10 @@ function Expand-7zipArchive {
64
66
}
65
67
$LogPath = " $ ( Split-Path $Path ) \7zip.log"
66
68
$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
+ }
67
73
if ($Switches ) {
68
74
$ArgList += (-split $Switches )
69
75
}
@@ -74,17 +80,20 @@ function Expand-7zipArchive {
74
80
}
75
81
$Status = Invoke-ExternalCommand $7zPath $ArgList - LogPath $LogPath
76
82
if (! $Status ) {
77
- abort " Failed to extract files from $Path .`n Log file:`n $ ( friendly_path $LogPath ) "
83
+ abort " Failed to extract files from $Path .`n Log 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
78
87
}
79
88
if (Test-Path $LogPath ) {
80
89
Remove-Item $LogPath - Force
81
90
}
82
- if ((strip_ext $Path ) -match ' \.tar$ ' -or $Path -match ' \.t[abgpx]z2?$ ' ) {
91
+ if ($IsTar ) {
83
92
# Check for tar
84
93
$Status = Invoke-ExternalCommand $7zPath @ (' l' , " `" $Path `" " ) - LogPath $LogPath
85
94
if ($Status ) {
86
95
$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
88
97
} else {
89
98
abort " Failed to list files in $Path .`n Not a 7-Zip supported archive file."
90
99
}
@@ -104,29 +113,38 @@ function Expand-MsiArchive {
104
113
[Parameter (Position = 1 )]
105
114
[String ]
106
115
$DestinationPath = (Split-Path $Path ),
116
+ [String ]
117
+ $ExtractDir ,
107
118
[Parameter (ValueFromRemainingArguments = $true )]
108
119
[String ]
109
120
$Switches ,
110
121
[Switch ]
111
122
$Removal
112
123
)
124
+ $DestinationPath = $DestinationPath.TrimEnd (" \" )
113
125
if ((get_config MSIEXTRACT_USE_LESSMSI)) {
114
126
$MsiPath = Get-HelperPath - Helper Lessmsi
115
127
$ArgList = @ (' x' , " `" $Path `" " , " `" $DestinationPath \\`" " )
116
128
} else {
117
129
$MsiPath = ' msiexec.exe'
118
- $ArgList = @ (' /a' , " `" $Path `" " , ' /qn' , " TARGETDIR=`" $DestinationPath `" " )
130
+ $ArgList = @ (' /a' , " `" $Path `" " , ' /qn' , " TARGETDIR=`" $DestinationPath \\SourceDir `" " )
119
131
}
120
132
$LogPath = " $ ( Split-Path $Path ) \msi.log"
121
133
if ($Switches ) {
122
134
$ArgList += (-split $Switches )
123
135
}
124
136
$Status = Invoke-ExternalCommand $MsiPath $ArgList - LogPath $LogPath
125
137
if (! $Status ) {
126
- abort " Failed to extract files from $Path .`n Log file:`n $ ( friendly_path $LogPath ) "
138
+ abort " Failed to extract files from $Path .`n Log file:`n $ ( friendly_path $LogPath ) `n $ ( new_issue_msg $app $bucket ' decompress error ' ) "
127
139
}
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
130
148
}
131
149
if (($DestinationPath -ne (Split-Path $Path )) -and (Test-Path " $DestinationPath \$ ( fname $Path ) " )) {
132
150
Remove-Item " $DestinationPath \$ ( fname $Path ) " - Force
@@ -149,20 +167,22 @@ function Expand-InnoArchive {
149
167
[Parameter (Position = 1 )]
150
168
[String ]
151
169
$DestinationPath = (Split-Path $Path ),
170
+ [String ]
171
+ $ExtractDir ,
152
172
[Parameter (ValueFromRemainingArguments = $true )]
153
173
[String ]
154
174
$Switches ,
155
175
[Switch ]
156
176
$Removal
157
177
)
158
178
$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' )
160
180
if ($Switches ) {
161
181
$ArgList += (-split $Switches )
162
182
}
163
183
$Status = Invoke-ExternalCommand (Get-HelperPath - Helper Innounp) $ArgList - LogPath $LogPath
164
184
if (! $Status ) {
165
- abort " Failed to extract files from $Path .`n Log file:`n $ ( friendly_path $LogPath ) "
185
+ abort " Failed to extract files from $Path .`n Log file:`n $ ( friendly_path $LogPath ) `n $ ( new_issue_msg $app $bucket ' decompress error ' ) "
166
186
}
167
187
if (Test-Path $LogPath ) {
168
188
Remove-Item $LogPath - Force
@@ -182,6 +202,8 @@ function Expand-ZipArchive {
182
202
[Parameter (Position = 1 )]
183
203
[String ]
184
204
$DestinationPath = (Split-Path $Path ),
205
+ [String ]
206
+ $ExtractDir ,
185
207
[Switch ]
186
208
$Removal
187
209
)
@@ -213,6 +235,10 @@ function Expand-ZipArchive {
213
235
# Compatible with Pscx (https://github.com/Pscx/Pscx)
214
236
Microsoft.PowerShell.Archive\Expand-Archive - Path $Path - DestinationPath $DestinationPath - Force
215
237
}
238
+ if ($ExtractDir ) {
239
+ Get-ChildItem $DestinationPath - Exclude $ExtractDir | Remove-Item - Recurse - Force
240
+ movedir " $DestinationPath \$ExtractDir " $DestinationPath | Out-Null
241
+ }
216
242
if ($Removal ) {
217
243
# Remove original archive file
218
244
Remove-Item $Path - Force
@@ -221,31 +247,38 @@ function Expand-ZipArchive {
221
247
222
248
function Expand-DarkArchive {
223
249
[CmdletBinding ()]
224
- param (
250
+ param (
225
251
[Parameter (Mandatory = $true , Position = 0 , ValueFromPipeline = $true )]
226
252
[String ]
227
253
$Path ,
228
254
[Parameter (Position = 1 )]
229
255
[String ]
230
256
$DestinationPath = (Split-Path $Path ),
257
+ [String ]
258
+ $ExtractDir ,
231
259
[Parameter (ValueFromRemainingArguments = $true )]
232
260
[String ]
233
261
$Switches ,
234
262
[Switch ]
235
263
$Removal
236
264
)
265
+ $DestinationPath = $DestinationPath.TrimEnd (" \" )
237
266
$LogPath = " $ ( Split-Path $Path ) \dark.log"
238
267
$ArgList = @ (' -nologo' , " -x `" $DestinationPath `" " , " `" $Path `" " )
239
268
if ($Switches ) {
240
269
$ArgList += (-split $Switches )
241
270
}
242
271
$Status = Invoke-ExternalCommand (Get-HelperPath - Helper Dark) $ArgList - LogPath $LogPath
243
272
if (! $Status ) {
244
- abort " Failed to extract files from $Path .`n Log file:`n $ ( friendly_path $LogPath ) "
273
+ abort " Failed to extract files from $Path .`n Log file:`n $ ( friendly_path $LogPath ) `n $ ( new_issue_msg $app $bucket ' decompress error ' ) "
245
274
}
246
275
if (Test-Path $LogPath ) {
247
276
Remove-Item $LogPath - Force
248
277
}
278
+ if ($ExtractDir ) {
279
+ Get-ChildItem $DestinationPath - Exclude $ExtractDir | Remove-Item - Recurse - Force
280
+ movedir " $DestinationPath \$ExtractDir " $DestinationPath | Out-Null
281
+ }
249
282
if ($Removal ) {
250
283
# Remove original archive file
251
284
Remove-Item $Path - Force
0 commit comments