-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcompress-map.ps1
More file actions
149 lines (124 loc) · 4.63 KB
/
Copy pathcompress-map.ps1
File metadata and controls
149 lines (124 loc) · 4.63 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# PowerShell script to compress map files to bz2 and split if needed
param(
[Parameter(Mandatory=$false)]
[string]$MapFile
)
# Configuration
$SPLIT_THRESHOLD_BYTES = 26214400 # 25 MiB
$SPLIT_CHUNK_SIZE = 20MB # 20 MiB in bytes
# Function to write colored output
function Write-ColorOutput {
param(
[string]$Message,
[string]$Color = "White"
)
Write-Host $Message -ForegroundColor $Color
}
# If no parameter provided, check if file was dragged
if (-not $MapFile) {
if ($args.Count -gt 0) {
$MapFile = $args[0]
} else {
Write-ColorOutput "Error: No map file provided" "Red"
Write-Host "Usage: .\compress-map.ps1 <map_file.bsp>"
Write-Host "Or drag and drop a .bsp file onto this script"
Read-Host "Press Enter to exit"
exit 1
}
}
# Validate file exists
if (-not (Test-Path $MapFile)) {
Write-ColorOutput "Error: File not found: $MapFile" "Red"
Read-Host "Press Enter to exit"
exit 1
}
# Validate file extension
if (-not $MapFile.EndsWith(".bsp")) {
Write-ColorOutput "Error: File must have .bsp extension" "Red"
Read-Host "Press Enter to exit"
exit 1
}
# Get absolute path
$MapFile = (Resolve-Path $MapFile).Path
$targetPath = "$MapFile.bz2"
$partsDir = "$targetPath.parts"
# Check if output already exists
if ((Test-Path $targetPath) -or (Test-Path $partsDir)) {
Write-ColorOutput "Warning: Output already exists for $(Split-Path $MapFile -Leaf)" "Yellow"
$response = Read-Host "Overwrite? (y/N)"
if ($response -ne "y" -and $response -ne "Y") {
Write-Host "Cancelled"
Read-Host "Press Enter to exit"
exit 0
}
if (Test-Path $targetPath) { Remove-Item $targetPath -Force }
if (Test-Path $partsDir) { Remove-Item $partsDir -Recurse -Force }
}
Write-ColorOutput "Compressing $(Split-Path $MapFile -Leaf) to bzip2..." "Green"
# Check if bzip2 is available
$bzip2Path = Get-Command bzip2 -ErrorAction SilentlyContinue
if ($bzip2Path) {
# Use bzip2 if available
& bzip2 -ck $MapFile | Set-Content -Path $targetPath -Encoding Byte
} else {
# Try to use 7-zip as fallback
$7zipPaths = @(
"C:\Program Files\7-Zip\7z.exe",
"C:\Program Files (x86)\7-Zip\7z.exe",
"$env:ProgramFiles\7-Zip\7z.exe",
"${env:ProgramFiles(x86)}\7-Zip\7z.exe"
)
$7zipPath = $7zipPaths | Where-Object { Test-Path $_ } | Select-Object -First 1
if ($7zipPath) {
& $7zipPath a -tbzip2 $targetPath $MapFile | Out-Null
} else {
Write-ColorOutput "Error: Neither bzip2 nor 7-Zip found" "Red"
Write-Host "Please install one of the following:"
Write-Host " - bzip2 (via chocolatey: choco install bzip2)"
Write-Host " - 7-Zip (https://www.7-zip.org/)"
Read-Host "Press Enter to exit"
exit 1
}
}
Write-ColorOutput "✓ Created $targetPath" "Green"
# Check file size and split if needed
$fileSize = (Get-Item $targetPath).Length
$fileSizeMB = [math]::Round($fileSize / 1MB, 2)
Write-Host "Compressed size: $fileSizeMB MiB"
if ($fileSize -gt $SPLIT_THRESHOLD_BYTES) {
Write-ColorOutput "File exceeds $SPLIT_THRESHOLD_BYTES bytes threshold" "Yellow"
Write-ColorOutput "Splitting into 20 MiB chunks..." "Green"
# Create parts directory
New-Item -ItemType Directory -Path $partsDir -Force | Out-Null
$baseName = Split-Path $targetPath -Leaf
# Read and split the file
$stream = [System.IO.File]::OpenRead($targetPath)
$buffer = New-Object byte[] $SPLIT_CHUNK_SIZE
$partNumber = 0
try {
while ($true) {
$bytesRead = $stream.Read($buffer, 0, $buffer.Length)
if ($bytesRead -eq 0) { break }
$partFile = Join-Path $partsDir ("{0}.part.{1:D3}" -f $baseName, $partNumber)
[System.IO.File]::WriteAllBytes($partFile, $buffer[0..($bytesRead-1)])
$partNumber++
}
}
finally {
$stream.Close()
}
# Remove the original compressed file
Remove-Item $targetPath -Force
Write-ColorOutput "✓ Split into $partNumber part(s) in $partsDir" "Green"
# List the parts
Write-Host "`nParts created:"
Get-ChildItem $partsDir | ForEach-Object {
$sizeMB = [math]::Round($_.Length / 1MB, 2)
Write-Host " $($_.Name) - $sizeMB MiB"
}
} else {
Write-ColorOutput "✓ File size is within threshold, no splitting needed" "Green"
}
Write-ColorOutput "`nDone!" "Green"
# Always pause at the end
Read-Host "Press Enter to exit"