-
Notifications
You must be signed in to change notification settings - Fork 0
/
zip-objects.ps1
74 lines (55 loc) · 2.22 KB
/
zip-objects.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
72
73
74
$verbose = $args.length -eq 1 -and $args[0] -eq '-v'
$homepath = Get-Location
$objectTotal = 0
$objectSuccess = 0
$outdir = "$($homepath)/dist"
New-Item -Path $outdir -ItemType Directory -Force | Out-Null
Set-Location ./objects
$groupPaths = Get-ChildItem -Directory
foreach ($groupPath in $groupPaths) {
Set-Location $groupPath
$objectPaths = Get-ChildItem -Directory
foreach ($objectPath in $objectPaths) {
Set-Location $objectPath
$objectTotal++
if (Test-Path "images.json") {
# Ignore paths with images.json in them, as they are probably not done
Set-Location ..
continue
}
# Get object identifier from the file itself
$objectName = (Select-String -Path .\object.json '"id": "([\w\.]*)"' -AllMatches).Matches.Groups[1].Value
$outfile = "$($outdir)/$($objectName).parkobj"
# Remove old zip file, if it exists
if (Test-Path $outfile) {
Remove-Item $outfile -ErrorAction Stop
}
if ($verbose) {
Write-Output "Zipping $objectName..."
}
# Get file list
$Files = @(Get-ChildItem "./" -Recurse -File)
$FullFilenames = $files | ForEach-Object -Process {Write-Output -InputObject $_.FullName}
# Create zip file
Add-Type -AssemblyName System.IO.Compression
Add-Type -AssemblyName System.IO.Compression.FileSystem
$zip = [System.IO.Compression.ZipFile]::Open(($outfile), [System.IO.Compression.ZipArchiveMode]::Create)
# Write entries with relative paths as names
foreach ($fname in $FullFilenames) {
$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()
}
# Release zip file
$zip.Dispose()
$objectSuccess += 1
Set-Location ..
}
Set-Location ..
}
# Go home
Set-Location $homepath
Write-Output "Created $objectSuccess out of $objectTotal objects."