Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(decompress): PSScriptAnalyzer #64

Merged
merged 9 commits into from
Sep 5, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
zip
  • Loading branch information
Ash258 committed Sep 5, 2020
commit 4df2add39f6d7668024997ba2e711a658c3a06d9
81 changes: 42 additions & 39 deletions lib/decompress.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ function Expand-7zipArchive {
Set-TerminatingError -Title "Ignore|-Cannot find external 7-Zip (7z.exe) while '7ZIPEXTRACT_USE_EXTERNAL' is 'true'!`nRun 'scoop config 7ZIPEXTRACT_USE_EXTERNAL false' or install 7-Zip manually and try again."
}
} else {
$7zPath = Get-HelperPath -Helper 7zip
$7zPath = Get-HelperPath -Helper '7zip'
}
}

Expand Down Expand Up @@ -127,7 +127,7 @@ function Expand-MsiArchive {
begin {
$DestinationPath = $DestinationPath.TrimEnd('\')
if ((get_config 'MSIEXTRACT_USE_LESSMSI' $false)) {
$msiPath = Get-HelperPath -Helper Lessmsi
$msiPath = Get-HelperPath -Helper 'Lessmsi'
$argList = @('x', "`"$Path`"", "`"$DestinationPath\\`"")
} else {
$msiPath = 'msiexec.exe'
Expand Down Expand Up @@ -199,7 +199,7 @@ function Expand-InnoArchive {
try {
# TODO: Find out extract_dir issue.
# When there is no specified directory in archive innounp will just exit with 0 and version of file
$status = Invoke-ExternalCommand (Get-HelperPath -Helper Innounp) $argList -LogPath $logPath
$status = Invoke-ExternalCommand (Get-HelperPath -Helper 'Innounp') $argList -LogPath $logPath
} catch [System.Management.Automation.ParameterBindingException] {
Set-TerminatingError -Title 'Ignore|-''innounp'' is not installed or cannot be used'
}
Expand All @@ -224,45 +224,48 @@ function Expand-ZipArchive {
[Switch] $Removal
)

if ($ExtractDir) {
$OriDestinationPath = $DestinationPath
$DestinationPath = Join-Path $DestinationPath '_tmp'
}
process {
if ($ExtractDir) {
$originalDestination = $DestinationPath
$DestinationPath = Join-Path $DestinationPath '_tmp'
}

# All methods to unzip the file require .NET4.5+
if ($PSVersionTable.PSVersion.Major -lt 5) {
Add-Type -AssemblyName System.IO.Compression.FileSystem
try {
[System.IO.Compression.ZipFile]::ExtractToDirectory($Path, $DestinationPath)
} catch [System.IO.PathTooLongException] {
# Try to fall back to 7zip if path is too long
if (Test-HelperInstalled -Helper 7zip) {
Expand-7zipArchive $Path $DestinationPath -Removal:$Removal
return
} else {
Set-TerminatingError -Title "Ignore|-Unzip failed: Windows cannot handle long paths in this zip file.`nRun 'scoop install 7zip' and try again."
}
} catch [System.IO.IOException] {
if (Test-HelperInstalled -Helper 7zip) {
Expand-7zipArchive $Path $DestinationPath -Removal:$Removal
return
} else {
Set-TerminatingError -Title "Ignore|-Unzip failed: Windows cannot handle the file names in this zip file.`nRun 'scoop install 7zip' and try again."
# All methods to unzip the file require .NET4.5+
if ($PSVersionTable.PSVersion.Major -lt 5) {
Add-Type -AssemblyName System.IO.Compression.FileSystem
try {
[System.IO.Compression.ZipFile]::ExtractToDirectory($Path, $DestinationPath)
} catch [System.IO.PathTooLongException] {
# Try to fall back to 7zip if path is too long
if (Test-HelperInstalled -Helper '7zip') {
Expand-7zipArchive $Path $DestinationPath -Removal:$Removal
return
} else {
Set-TerminatingError -Title "Ignore|-Unzip failed: Windows cannot handle long paths in this zip file.`nRun 'scoop install 7zip' and try again."
}
} catch [System.IO.IOException] {
if (Test-HelperInstalled -Helper '7zip') {
Expand-7zipArchive $Path $DestinationPath -Removal:$Removal
return
} else {
Set-TerminatingError -Title "Ignore|-Unzip failed: Windows cannot handle the file names in this zip file.`nRun 'scoop install 7zip' and try again."
}
} catch {
Set-TerminatingError -Title "Decompress error|-Unzip failed: $_"
}
} catch {
Set-TerminatingError -Title "Decompress error|-Unzip failed: $_"
} else {
# Use Expand-Archive to unzip in PowerShell 5+
# Compatible with Pscx (https://github.com/Pscx/Pscx)
Microsoft.PowerShell.Archive\Expand-Archive -Path $Path -DestinationPath $DestinationPath -Force
}
} else {
# Use Expand-Archive to unzip in PowerShell 5+
# Compatible with Pscx (https://github.com/Pscx/Pscx)
Microsoft.PowerShell.Archive\Expand-Archive -Path $Path -DestinationPath $DestinationPath -Force
}
if ($ExtractDir) {
movedir (Join-Path $DestinationPath $ExtractDir) $OriDestinationPath | Out-Null
Remove-Item $DestinationPath -Recurse -Force

if ($ExtractDir) {
movedir (Join-Path $DestinationPath $ExtractDir) $originalDestination | Out-Null
Remove-Item $DestinationPath -Recurse -Force
}
# Remove original archive file
if ($Removal) { Remove-Item $Path -Force }
}
# Remove original archive file
if ($Removal) { Remove-Item $Path -Force }
}

function Expand-DarkArchive {
Expand All @@ -282,7 +285,7 @@ function Expand-DarkArchive {
if ($Switches) { $ArgList += (-split $Switches) }

try {
$Status = Invoke-ExternalCommand (Get-HelperPath -Helper Dark) $ArgList -LogPath $LogPath
$Status = Invoke-ExternalCommand (Get-HelperPath -Helper 'Dark') $ArgList -LogPath $LogPath
} catch [System.Management.Automation.ParameterBindingException] {
Set-TerminatingError -Title 'Ignore|-''dark'' is not installed or cannot be used'
}
Expand Down