casualexpiesource

cu-trainer/Ultra Paradise Linux/zipunix.ps1

38 lines · 1.8 KB · plain text · download

1param([string]$Src, [string]$Out, [string[]]$Exec)
2$Exec = @($Exec | ForEach-Object { $_ -split ',' } | Where-Object { $_ })
3Add-Type -AssemblyName System.IO.Compression, System.IO.Compression.FileSystem
4if (Test-Path $Out) { Remove-Item $Out -Force }
5$root = (Resolve-Path $Src).Path.TrimEnd('\')
6$parent = Split-Path $root -Parent
7$zip = [IO.Compression.ZipFile]::Open($Out, 'Create')
8try {
9 $dirEntry = $zip.CreateEntry((Split-Path $root -Leaf) + '/')
10 $dirEntry.ExternalAttributes = [BitConverter]::ToInt32([BitConverter]::GetBytes(([uint32]0x41ED -shl 16) -bor 0x10), 0)
11 Get-ChildItem $root -Recurse -File | ForEach-Object {
12 $name = $_.FullName.Substring($parent.Length + 1).Replace('\', '/')
13 $e = [IO.Compression.ZipFileExtensions]::CreateEntryFromFile($zip, $_.FullName, $name, 'Optimal')
14 $mode = if ($Exec -contains $_.Name) { 0x81ED } else { 0x81A4 }
15 $e.ExternalAttributes = [BitConverter]::ToInt32([BitConverter]::GetBytes([uint32]$mode -shl 16), 0)
16 }
17} finally { $zip.Dispose() }
18
19$b = [IO.File]::ReadAllBytes($Out)
20$n = 0
21for ($i = $b.Length - 22; $i -ge 0; $i--) {
22 if ($b[$i] -eq 0x50 -and $b[$i + 1] -eq 0x4B -and $b[$i + 2] -eq 0x05 -and $b[$i + 3] -eq 0x06) {
23 $cd = [BitConverter]::ToUInt32($b, $i + 16)
24 $count = [BitConverter]::ToUInt16($b, $i + 10)
25 $p = [int]$cd
26 for ($k = 0; $k -lt $count; $k++) {
27 if ([BitConverter]::ToUInt32($b, $p) -ne 0x02014b50) { throw "bad central directory at $p" }
28 $b[$p + 4] = 30
29 $b[$p + 5] = 3
30 $n++
31 $fl = [BitConverter]::ToUInt16($b, $p + 28); $xl = [BitConverter]::ToUInt16($b, $p + 30); $cl = [BitConverter]::ToUInt16($b, $p + 32)
32 $p += 46 + $fl + $xl + $cl
33 }
34 break
35 }
36}
37[IO.File]::WriteAllBytes($Out, $b)
38Write-Host "$(Split-Path $Out -Leaf): $n entries marked as Unix"