-
Notifications
You must be signed in to change notification settings - Fork 4
/
New-ZipFile.ps1
39 lines (34 loc) · 1.06 KB
/
New-ZipFile.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<#
.SYNOPSIS
Creates a zip archive out of the given input files
.DESCRIPTION
Either specify the contents of the archive using the pipeline:
PS> gi .\dir1, .\dir2 | .\New-ZipFile.ps1 dirs.zip
(the zip will have dir1 and dir2 in its root)
...or given by argument:
PS> .\New-ZipFile.ps1 dir.zip .\dir1
Credit to the author of and inspiration from the original item:
http://poshcode.org/4198
#>
param(
[parameter(mandatory=$true)]
$target,
[parameter(mandatory=$true, ValueFromPipelineByPropertyName=$true)]
[alias("FullName")]
[string[]]$files
)
begin {
Add-type -AssemblyName "System.IO.Compression.FileSystem"
if(Test-Path $target) { Remove-Item $target -force }
$zip = [System.IO.Compression.ZipFile]::Open( $target, "Create" )
}
process {
$files | Resolve-Path | ls -recurse -force -file | % FullName | % {
$relativePath = Resolve-Path $_ -Relative
[void][System.IO.Compression.ZipFileExtensions]::CreateEntryFromFile($zip, $_, $relativePath.TrimStart(".\"), [System.IO.Compression.CompressionLevel]::Optimal)
}
}
end {
$zip.Dispose()
Get-Item $target
}