forked from VFPX/GoFish
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBuildCloudZip.ps1
71 lines (61 loc) · 2.4 KB
/
BuildCloudZip.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
$exclude = "*.bak,*.bat,*.fxp,*.zip"
# Ensure no spaces before or after comma
$excludeFiles = $exclude.Split(',')
# Ensure no spaces before or after comma
$exclude = "*Documentation*"
$excludeFolders = $exclude.Split(',')
# Get the name of the zip file.
$zipFileName = "Source.zip"
try
{
# Delete the zip file if it exists.
$exists = Test-Path ($zipFileName)
if ($exists)
{
del ($zipFileName)
}
# Loop through all the files in the project folder except those we don't want
# and add them to a zip file.
# See https://stackoverflow.com/questions/15294836/how-can-i-exclude-multiple-folders-using-get-childitem-exclude
# for how to exclude folders when -Recurse is used
$files = @(Get-ChildItem . -recurse -file -exclude $excludeFiles |
%{
$allowed = $true
foreach ($exclude in $excludeFolders)
{
if ((Split-Path $_.FullName -Parent) -ilike $exclude)
{
$allowed = $false
break
}
}
if ($allowed)
{
$_
}
}
);
# See https://stackoverflow.com/questions/51392050/compress-archive-and-preserve-relative-paths to compress
# exclude directory entries and generate fullpath list
$filesFullPath = $files | Where-Object -Property Attributes -CContains Archive | ForEach-Object -Process {Write-Output -InputObject $_.FullName}
#create zip file
Add-Type -AssemblyName System.IO.Compression, System.IO.Compression.FileSystem
$zip = [System.IO.Compression.ZipFile]::Open((Join-Path -Path $(Resolve-Path -Path ".") -ChildPath $zipFileName), [System.IO.Compression.ZipArchiveMode]::Create)
#write entries with relative paths as names
foreach ($fname in $filesFullPath)
{
$rname = $(Resolve-Path -Path $fname -Relative) -replace '\.\\',''
$zentry = $zip.CreateEntry($rname)
$zentryWriter = New-Object -TypeName System.IO.BinaryWriter $zentry.Open()
$zentryWriter.Write([System.IO.File]::ReadAllBytes($fname))
$zentryWriter.Flush()
$zentryWriter.Close()
}
# clean up
Get-Variable -exclude Runspace | Where-Object {$_.Value -is [System.IDisposable]} | Foreach-Object {$_.Value.Dispose(); Remove-Variable $_.Name};
}
catch
{
Write-Host "Error occurred at $(Get-Date): $($Error[0].Exception.Message)"
pause
}