-
Notifications
You must be signed in to change notification settings - Fork 1
/
CreateEntryFromFile.ps1
46 lines (34 loc) · 1.05 KB
/
CreateEntryFromFile.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
add-type -assembly 'System.IO.Compression'
add-type -assembly 'System.IO.Compression.FileSystem'
set-strictMode -version latest
function create-zip {
param (
[string] $dirRoot ,
[scriptBlock] $callBack,
[string] $zipFile
)
if (test-path $zipFile) {
remove-item $zipFile
}
[System.IO.Compression.ZipArchive] $zip = [System.IO.Compression.ZipFile]::Open(
"$pwd/$zipFile",
([System.IO.Compression.ZipArchiveMode]::Create)
)
pushd $dirRoot
get-childItem -attribute !directory -recurse |
where-object $callBack |
foreach-object {
$fileToAdd = $_.fullName
$fileToAddRelative = resolve-path $fileToAdd -relative
# remove the dot slash (./) from the relative path:
$entryName = $fileToAddRelative.substring(2)
# write-host "$fileToAddRelative -> $entryName"
$null = [System.IO.Compression.ZipFileExtensions]::CreateEntryFromFile(
$zip,
$fileToAdd,
$entryName
)
}
popd
$zip.Dispose()
}