Skip to content

Instantly share code, notes, and snippets.

@PolarBearGod
Created June 29, 2018 18:07
Show Gist options
  • Save PolarBearGod/89671eb7da0d0d613abd9d03d2696b92 to your computer and use it in GitHub Desktop.
Save PolarBearGod/89671eb7da0d0d613abd9d03d2696b92 to your computer and use it in GitHub Desktop.
Function ZipFiles {
<#
.SYNOPSIS
A function to zip or unzip files.
.DESCRIPTION
This function has 3 possible uses.
1) Zip a folder or files and save the zip to specified location.
2) Unzip a zip file to a specified folder.
3) Unzip a zip file and delete the original zip when complete.
.PARAMETER ZipPath
The full path of the file to unzip or the full path of the zip file to be created.
.PARAMETER FolderPath
The path to the files to zip or the path to the directory to unzip the files to.
.PARAMETER Unzip
If $true the function will perform an unzip instead of a zip
.PARAMETER DeleteZip
If set to $True the zip file will be removed at then end of the unzip operation.
.EXAMPLE
PS C:\> ZipFiles -ZipPath 'C:\Windows\Temp\ziptest.zip' -FolderPath
PS C:\> ZipFiles -ZipPath 'C:\Windows\Temp\ziptest.zip' -FolderPath 'C:\Windows\Temp\ZipTest' -Unzip $true
PS C:\> ZipFiles -ZipPath 'C:\Windows\Temp\ziptest.zip' -FolderPath 'C:\Windows\Temp\ZipTest' -Unzip $true -DeleteZip $True
.NOTES
Additional information about the function.
#>
[CmdletBinding(DefaultParameterSetName = 'Zip')]
param
(
[Parameter(ParameterSetName = 'Unzip')]
[Parameter(ParameterSetName = 'Zip',
Mandatory = $true,
Position = 0)]
[ValidateNotNull()]
[string]$ZipPath,
[Parameter(ParameterSetName = 'Unzip')]
[Parameter(ParameterSetName = 'Zip',
Mandatory = $true,
Position = 1)]
[ValidateNotNull()]
[string]$FolderPath,
[Parameter(ParameterSetName = 'Unzip',
Mandatory = $false,
Position = 2)]
[ValidateNotNull()]
[bool]$Unzip,
[Parameter(ParameterSetName = 'Unzip',
Mandatory = $false,
Position = 3)]
[ValidateNotNull()]
[bool]$DeleteZip
)
Log-Message "Entering Zip-Actions Function."
switch ($PsCmdlet.ParameterSetName) {
'Zip' {
If ([int]$psversiontable.psversion.Major -lt 3) {
Log-Message "Step 1"
New-Item $ZipPath -ItemType file
$shellApplication = new-object -com shell.application
$zipPackage = $shellApplication.NameSpace($ZipPath)
$files = Get-ChildItem -Path $FolderPath -Recurse
Log-Message "Step 2"
foreach ($file in $files) {
$zipPackage.CopyHere($file.FullName)
Start-sleep -milliseconds 500
}
Log-Message "Exiting Zip-Actions Function."
break
}
Else {
Log-Message "Step 3"
Add-Type -assembly "system.io.compression.filesystem"
$Compression = [System.IO.Compression.CompressionLevel]::Optimal
[io.compression.zipfile]::CreateFromDirectory($FolderPath, $ZipPath, $Compression, $True)
Log-Message "Exiting Zip-Actions Function."
break
}
}
'Unzip' {
$shellApplication = new-object -com shell.application
$zipPackage = $shellApplication.NameSpace($ZipPath)
$destinationFolder = $shellApplication.NameSpace($FolderPath)
$destinationFolder.CopyHere($zipPackage.Items(), 20)
Log-Message "Exiting Unzip Section"
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment