add functions to zip and unzip files

This commit is contained in:
2021-11-10 20:18:43 -05:00
parent b4396973cb
commit 61ea9303d3
+44
View File
@@ -538,5 +538,49 @@ function Remove-File ([Parameter(ValueFromPipeline=$true)][string]$path) {
}
}
# Function to ZIP a file or directory
function Zip-Object ([Parameter(ValueFromPipeline=$true)][string]$path) {
If ( $(Get-Item $path) -is [System.IO.DirectoryInfo] ) {
$dest = '{0}\{1}.zip' -f $(Split-Path -Parent $path),$(Split-Path -Leaf $path)
LogMsg "Creating zip file of: ${path}"
Try {
Add-Type -Assembly "System.IO.Compression.FileSystem"
[IO.Compression.ZipFile]::CreateFromDirectory($path, $dest)
}
Catch { LogErr $_.Exception.Message }
If ( Test-Path $dest ) { Return $dest } Else { Return $false }
}
ElseIf ( $(Get-Item $path) -is [System.IO.FileInfo] ) {
$dir = $path.Substring(0, $path.LastIndexOf('.'))
Try {
LogMsg "Creating directory: ${path}"
New-Item -ItemType Directory -Path $dir -ErrorAction Stop | Out-Null
LogMsg "Moving ""${path}"" into ""${dir}"""
Move-Item -Path $path -Destination $dir -Force -ErrorAction Stop | Out-Null
}
Catch { LogErr $_.Exception.Message }
Zip-Object $dir
}
Else { LogMsg "Could not determine the file/folder status: ${path}" }
}
# Function to UNZIP a file or directory
function Unzip-Object ([Parameter(ValueFromPipeline=$true)][string]$path) {
$ext = [System.IO.Path]::GetExtension($path)
If ( $ext -eq ".zip" ) {
LogMsg "Extracting ""${path}"" to ""${dest}"""
$dest = Split-Path -Parent $path
Try {
Add-Type -Assembly "System.IO.Compression.FileSystem"
[IO.Compression.ZipFile]::ExtractToDirectory($path, $dest)
}
Catch { LogErr $_.Exception.Message }
}
Else { LogMsg "The path specified is not a zip file: ${path}" }
}
Setup-MSPDirectories
Setup-LogFile
#Zip-Object "C:\Users\dyoder\Desktop\wifi"
Unzip-Object "C:\Users\dyoder\Desktop\wifi.zip"