overhaul on zip funcs

This commit is contained in:
2023-10-06 16:06:59 -04:00
parent 25efd5ef03
commit 06fdd45c36
+32 -16
View File
@@ -532,13 +532,21 @@ Function Install-MSP360Module {
} }
# Function to ZIP a file or directory, returns the newly created zip file # Function to ZIP a file or directory, returns the newly created zip file
# TODO: updating an archive with files that already exist inside will create duplicates and cause problems during extraction
Function Compress-Archive { Function Compress-Archive {
param( param(
[Parameter(ValueFromPipeline=$true)][string]$Path, [Parameter(ValueFromPipeline=$true,Mandatory=$true)][string]$Path,
[Parameter(ValueFromPipeline=$false)][string]$DestinationPath, [Parameter(ValueFromPipeline=$false,Mandatory=$false)][string]$DestinationPath,
[Parameter(ValueFromPipeline=$false)][switch]$Force=$false [Parameter(ValueFromPipeline=$false,Mandatory=$false)][switch]$Force=$false
) )
BEGIN { BEGIN {
If ( !(Test-Path $Path ) ) { Write-Error "The specified path does not exist: ""${Path}""" ; Exit }
If ( [string]::IsNullOrEmpty($DestinationPath) ) {
Switch ( Get-Item -Path $Path ) {
{$_ -is [System.IO.DirectoryInfo]} { $DestinationPath = '{0}.zip' -f ($Path.TrimEnd('\'))}
{$_ -is [System.IO.FileInfo]} { $DestinationPath = '{0}\{1}.zip' -f (Split-Path $Path -Parent), ([System.IO.Path]::GetFileNameWithoutExtension($Path)) }
}
}
If ( ($Force) -and (Test-Path $DestinationPath) ) { If ( ($Force) -and (Test-Path $DestinationPath) ) {
Write-Output "Deleting existing archive: ""${DestinationPath}""" Write-Output "Deleting existing archive: ""${DestinationPath}"""
Remove-Item -Path $DestinationPath -Force -ErrorAction Stop Remove-Item -Path $DestinationPath -Force -ErrorAction Stop
@@ -556,6 +564,7 @@ Function Compress-Archive {
[System.IO.Compression.ZipFile]::CreateFromDirectory($Path, $DestinationPath, [System.IO.Compression.CompressionLevel]::Optimal, $true) [System.IO.Compression.ZipFile]::CreateFromDirectory($Path, $DestinationPath, [System.IO.Compression.CompressionLevel]::Optimal, $true)
} Else { } Else {
If ( (Get-Command Microsoft.Powershell.Archive\Compress-Archive -ErrorAction SilentlyContinue) ) { If ( (Get-Command Microsoft.Powershell.Archive\Compress-Archive -ErrorAction SilentlyContinue) ) {
Write-Output "Calling Microsoft util"
Microsoft.Powershell.Archive\Compress-Archive -Path $Path -DestinationPath $DestinationPath -Update -CompressionLevel Optimal Microsoft.Powershell.Archive\Compress-Archive -Path $Path -DestinationPath $DestinationPath -Update -CompressionLevel Optimal
} Else { } Else {
Write-Error "Updating an existing zip archive with a folder structure is not currently supported" Write-Error "Updating an existing zip archive with a folder structure is not currently supported"
@@ -569,11 +578,11 @@ Function Compress-Archive {
} Else { $ArchiveMode = [System.IO.Compression.ZipArchiveMode]::Update } } Else { $ArchiveMode = [System.IO.Compression.ZipArchiveMode]::Update }
[System.IO.Compression.ZipArchive]$DestinationArchive = [System.IO.Compression.ZipFile]::Open($DestinationPath, $ArchiveMode) [System.IO.Compression.ZipArchive]$DestinationArchive = [System.IO.Compression.ZipFile]::Open($DestinationPath, $ArchiveMode)
[System.IO.Compression.ZipFileExtensions]::CreateEntryFromFile($DestinationArchive, $Path, (Split-Path $Path -Leaf)) | Out-Null [System.IO.Compression.ZipFileExtensions]::CreateEntryFromFile($DestinationArchive, $Path, (Split-Path $Path -Leaf)) | Out-Null
$DestinationArchive.Dispose()
} }
} }
} }
Catch { Write-Error $_.Exception.Message } Catch { Write-Error $_.Exception.Message }
Finally { If ( $DestinationArchive ) { $DestinationArchive.Dispose() } }
} }
END { END {
If ( !$CreateNewArchive ) { Write-Output "Finished updating existing archive: ""${DestinationPath}""" } If ( !$CreateNewArchive ) { Write-Output "Finished updating existing archive: ""${DestinationPath}""" }
@@ -581,18 +590,25 @@ Function Compress-Archive {
} }
} }
# Function to UNZIP a file or directory # Function to unzip a file or directory
Function Expand-Archive ([Parameter(ValueFromPipeline=$true)][string]$path) { # TODO: extracting an archive will fail if the destination file(s) already exists
If ( [System.IO.Path]::GetExtension($path) -eq ".zip" ) { Function Expand-Archive {
$dest = Split-Path -Parent $path param(
Write-Output "Extracting ""${path}"" to ""${dest}""" [Parameter(ValueFromPipeline=$true,Mandatory=$true)][string]$Path,
Try { [Parameter(ValueFromPipeline=$false,Mandatory=$false)][string]$DestinationPath,
Add-Type -Assembly "System.IO.Compression.FileSystem" [Parameter(ValueFromPipeline=$false,Mandatory=$false)][switch]$Force=$false
[IO.Compression.ZipFile]::ExtractToDirectory($path, $dest) )
} If ( !(Test-Path $Path) ) { Write-Output "The specified zip archive does not exist: ""${Path}""" ; Exit }
Catch { Write-Error $_.Exception.Message } If ( [System.IO.Path]::GetExtension($Path) -ne ".zip" ) { Write-Output "The path specified is not a zip file: ""${Path}""" ; Exit }
If ( [string]::IsNullOrEmpty($DestinationPath) ) { $DestinationPath = Split-Path -Parent $Path }
Try {
Add-Type -Assembly "System.IO.Compression.FileSystem"
Write-Output "Extracting ""${Path}"" to ""${DestinationPath}"""
[System.IO.Compression.ZipArchive]$SourceArchive = [System.IO.Compression.ZipFile]::Open($Path, [System.IO.Compression.ZipArchiveMode]::Read)
[System.IO.Compression.ZipFileExtensions]::ExtractToDirectory($SourceArchive, $DestinationPath)
} }
Else { Write-Output "The path specified is not a zip file: ${path}" } Catch { Write-Error $_.Exception.Message }
Finally { If ( $SourceArchive ) { $SourceArchive.Dispose() } }
} }
# Function to get a copy of the LiquidFiles CLI agent # Function to get a copy of the LiquidFiles CLI agent
@@ -623,7 +639,7 @@ Function IsRunning ([Parameter(ValueFromPipeline=$true)][string]$Name) {
} }
# Function to create a local user account # Function to create a local user account
Function Create-LocalUser { Function New-LocalUser {
param( param(
[Parameter(Mandatory=$true)][string]$Username, [Parameter(Mandatory=$true)][string]$Username,
[Parameter(Mandatory=$true)][string]$Password, [Parameter(Mandatory=$true)][string]$Password,