Zipping can be done using the CreateFromDirectory
method of the .NET class System.IO.Compression.ZipFile
. I prefer using .NET over installing community extensions and other add-ons, in order to keep the server environment as clean as possible.
I believe you cannot use it to add to a zip archive, but would need to temporarily copy the files and folders in the correct structure to a staging directory. From there you could create the zip file and delete the staging content.
To use .NET for zipping you first need to load the class assembly:
[System.Reflection.Assembly]::LoadWithPartialName("System.IO.Compression.FileSystem")
Then you can call the class:
[System.IO.Compression.ZipFile]::CreateFromDirectory($DirPath, $ZipFilePath, $CompressionLevel, $includeBaseDir)
Where:
$DirPath
is the directory you want to zip.
$ZipFilePath
is the name of the zipfile you want to create.
$Compressionlevel
can be set to Optimal
, Fastest
or NoCompression
.
$includeBaseDir
can be $true
or $false
.
I wrote a function out of it to save me the bother of having to look up the .NET reference page all the time. If you look at the page you will see there is a method for unzipping too.
.NET 4.5 is required and I would strongly encourage you to upgrade powershell too. Every version has brought massive improvements. It also turns out using .NET is efficient, having benchmarked it against popular 3rd party zip applications it comes out strongly.
The function is pretty self explanatory, just make sure you run the function itself before calling it. Here is a short syntax rundown if you need it:
New-ZipFile {DirToZip} {ZipfilePathAndName} {fastORoptimalORnocompression} {IncludeBaseDirectoryInZip($true or $false)}
So to zip E:\stuff
to D:\zipfile.zip
using optimal compression and include the basedir .\stuff
in the zipfile, you would run:
New-ZipFile 'E:\stuff' 'D:\zipfile.zip' Optimal $true
Here is the function:
#region function New-ZipFile
<#
.Synopsis
Creates a zip file from a directory.
.DESCRIPTION
Creates a zip file from a directory. Options include compression level and wether or not to include the base directory. Class reference: https://msdn.microsoft.com/en-us/library/hh875104(v=vs.110).aspx
.EXAMPLE
New-ZipFile c:\dir c:\zipfile.zip Fastest $True
.EXAMPLE
New-ZipFile c:\dir c:\zipfile.zip Optimal $True
.INPUTS
Inputs to this cmdlet (if any)
.OUTPUTS
Output from this cmdlet (if any)
.NOTES
General notes
.COMPONENT
The component this cmdlet belongs to
.ROLE
The role this cmdlet belongs to
.FUNCTIONALITY
The functionality that best describes this cmdlet
#>
function New-ZipFile
{
Param
(
# The directory to zip.
[Parameter(Mandatory=$true,
Position=0)]
[ValidateNotNullOrEmpty()]
[string]$DirPath,
# The zipfile name.
[Parameter(Mandatory=$true,
Position=1)]
[ValidateNotNullOrEmpty()]
[string]$ZipFilePath,
# Specifies values that indicate whether a compression operation emphasizes speed or compression size.
[Parameter(Mandatory=$true,
Position=2)]
[ValidateNotNullOrEmpty()]
[ValidateSet("Fastest", "NoCompression", "Optimal", ignorecase=$True)]
[string]$CompressionLevel,
# $True to include the directory name from sourceDirectoryName at the root of the archive; $False to include only the contents of the directory.
[Parameter(Mandatory=$true,
Position=3)]
[ValidateNotNullOrEmpty()]
[bool]$includeBaseDir
)
#Load the .NET 4.5 zip-assembly
[System.Reflection.Assembly]::LoadWithPartialName("System.IO.Compression.FileSystem") | Out-Null
# Zip the directory.
[System.IO.Compression.ZipFile]::CreateFromDirectory($DirPath, $ZipFilePath, $CompressionLevel, $includeBaseDir)
}
#endregion
EDIT: The Scripting guy wrote an article where he shows an alternative way of loading the assembly, it's a bit cleaner looking.