From 06fdd45c361f13d4eb7f32ba6b2e05d68ac7a9a2 Mon Sep 17 00:00:00 2001 From: David Yoder Date: Fri, 6 Oct 2023 16:06:59 -0400 Subject: [PATCH] overhaul on zip funcs --- Tools.ps1 | 48 ++++++++++++++++++++++++++++++++---------------- 1 file changed, 32 insertions(+), 16 deletions(-) diff --git a/Tools.ps1 b/Tools.ps1 index 1bdd7ec..0413c7f 100644 --- a/Tools.ps1 +++ b/Tools.ps1 @@ -532,13 +532,21 @@ Function Install-MSP360Module { } # 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 { param( - [Parameter(ValueFromPipeline=$true)][string]$Path, - [Parameter(ValueFromPipeline=$false)][string]$DestinationPath, - [Parameter(ValueFromPipeline=$false)][switch]$Force=$false + [Parameter(ValueFromPipeline=$true,Mandatory=$true)][string]$Path, + [Parameter(ValueFromPipeline=$false,Mandatory=$false)][string]$DestinationPath, + [Parameter(ValueFromPipeline=$false,Mandatory=$false)][switch]$Force=$false ) 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) ) { Write-Output "Deleting existing archive: ""${DestinationPath}""" 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) } Else { 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 } Else { 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 } [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 - $DestinationArchive.Dispose() } } } Catch { Write-Error $_.Exception.Message } + Finally { If ( $DestinationArchive ) { $DestinationArchive.Dispose() } } } END { If ( !$CreateNewArchive ) { Write-Output "Finished updating existing archive: ""${DestinationPath}""" } @@ -581,18 +590,25 @@ Function Compress-Archive { } } -# Function to UNZIP a file or directory -Function Expand-Archive ([Parameter(ValueFromPipeline=$true)][string]$path) { - If ( [System.IO.Path]::GetExtension($path) -eq ".zip" ) { - $dest = Split-Path -Parent $path - Write-Output "Extracting ""${path}"" to ""${dest}""" - Try { - Add-Type -Assembly "System.IO.Compression.FileSystem" - [IO.Compression.ZipFile]::ExtractToDirectory($path, $dest) - } - Catch { Write-Error $_.Exception.Message } +# Function to unzip a file or directory +# TODO: extracting an archive will fail if the destination file(s) already exists +Function Expand-Archive { + param( + [Parameter(ValueFromPipeline=$true,Mandatory=$true)][string]$Path, + [Parameter(ValueFromPipeline=$false,Mandatory=$false)][string]$DestinationPath, + [Parameter(ValueFromPipeline=$false,Mandatory=$false)][switch]$Force=$false + ) + If ( !(Test-Path $Path) ) { Write-Output "The specified zip archive does not exist: ""${Path}""" ; Exit } + 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 @@ -623,7 +639,7 @@ Function IsRunning ([Parameter(ValueFromPipeline=$true)][string]$Name) { } # Function to create a local user account -Function Create-LocalUser { +Function New-LocalUser { param( [Parameter(Mandatory=$true)][string]$Username, [Parameter(Mandatory=$true)][string]$Password,