-
Notifications
You must be signed in to change notification settings - Fork 4
/
Expand-ZipFile.ps1
48 lines (43 loc) · 1.12 KB
/
Expand-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
40
41
42
43
44
45
46
47
48
<#
.SYNOPSIS
Expands the a zip file to a target directory
.DESCRIPTION
All items in the zip file will be extracted to the directory
given by the -target argument. The given target directory will
be created if it does not exist.
The file will expand into the current directory if -target is
omitted.
PS> .\Expand-ZipFile.ps1 file.zip
PS> .\Expand-ZipFile.ps1 file.zip c:\temp\createme
Credit to the author of and inspiration from the original item:
http://poshcode.org/4198
#>
param(
[parameter(mandatory=$true)]
$path,
$target = $pwd
)
Add-type -AssemblyName "System.IO.Compression.FileSystem"
$path = Resolve-Path $path | % Path
if( !(Test-Path $target)) {
mkdir $target
}
$zip = [System.IO.Compression.ZipFile]::Open( $path, "Read" )
try {
$zip.Entries | % {
$entryPath = join-path $target $_.FullName
if( $_.Name -eq '' ) {
mkdir $entryPath -force
} else {
$directory = $entryPath | Split-Path
if( !(Test-Path $directory)) {
mkdir $directory -force
}
[System.IO.Compression.ZipFileExtensions]::ExtractToFile( $_, $entryPath, $true )
}
}
} finally {
if( $zip -ne $null ) {
$zip.Dispose()
}
}