private/BootMedia/Steps/Step-BootImageAppZip.ps1

#Requires -PSEdition Core

function Step-BootImageAppZip {
    <#
    .SYNOPSIS
        Downloads, caches, and injects 7-Zip into the mounted WinPE image.
 
    .NOTES
        Author: David Segura
        Version: 0.1.0
    #>

    [CmdletBinding()]
    param ()

    $Architecture = $global:BuildMedia.Architecture
    $MountPath = $global:BuildMedia.MountPath
    $WinPEAppsPath = Join-Path $Script:OSDeployCorePath 'cache' 'winpe-apps'
    $zipConfig = $global:OSDeployModule.BootImage.winpeapps.sevenzip

    $zipVersion = $zipConfig.version
    $CacheZipRoot = Join-Path $WinPEAppsPath '7zip'
    $CacheZip = Join-Path $CacheZipRoot $zipVersion

    # Cleanup old versions
    if (Test-Path -Path $CacheZipRoot) {
        Get-ChildItem -Path $CacheZipRoot -File -ErrorAction SilentlyContinue | Remove-Item -Force
        Get-ChildItem -Path $CacheZipRoot -Directory -ErrorAction SilentlyContinue |
            Where-Object { $_.Name -ne $zipVersion } |
            Remove-Item -Recurse -Force
    }

    if (Test-Path -Path $CacheZip) {
        Write-OSDeployCoreProgress "Using 7-Zip cache: $CacheZip"
    }
    else {
        Write-OSDeployCoreProgress "Creating 7-Zip cache: $CacheZip"
        New-Item -Path $CacheZip -ItemType Directory -Force | Out-Null
    }

    # Download 7zr.exe standalone
    if (-not (Test-Path -Path "$CacheZip\7zr.exe")) {
        $downloadUrl = $zipConfig.standalone
        Write-OSDeployCoreProgress "Downloading 7zr.exe"
        $downloadPath = Join-Path $CacheZip '7zr.exe'
        if (Get-Command 'curl.exe' -ErrorAction SilentlyContinue) {
            & curl.exe --silent --location --output $downloadPath $downloadUrl
        }
        else {
            Invoke-WebRequest -UseBasicParsing -Uri $downloadUrl -OutFile $downloadPath
        }
    }

    # Download and extract 7z extra archive
    if (-not (Test-Path -Path "$CacheZip\7za")) {
        $downloadUrl = $zipConfig.extra
        $extraFileName = Split-Path $downloadUrl -Leaf
        $downloadPath = Join-Path $CacheZip $extraFileName
        Write-OSDeployCoreProgress "Downloading 7-Zip extra: $extraFileName"
        if (Get-Command 'curl.exe' -ErrorAction SilentlyContinue) {
            & curl.exe --silent --location --output $downloadPath $downloadUrl
        }
        else {
            Invoke-WebRequest -UseBasicParsing -Uri $downloadUrl -OutFile $downloadPath
        }
        $null = & "$CacheZip\7zr.exe" x "$downloadPath" -o"$CacheZip\7za" -y
    }

    # Copy to WinPE
    if ($Architecture -eq 'amd64') {
        if (Test-Path "$CacheZip\7za\x64") {
            Write-OSDeployCoreProgress 'Injecting 7-Zip (x64) into WinPE'
            Copy-Item -Path "$CacheZip\7za\x64\*" -Destination "$MountPath\Windows\System32" -Recurse -Force
            $global:BuildMedia.InstalledApps += '7zip'
        }
    }
    if ($Architecture -eq 'arm64') {
        if (Test-Path "$CacheZip\7za\arm64") {
            Write-OSDeployCoreProgress 'Injecting 7-Zip (arm64) into WinPE'
            Copy-Item -Path "$CacheZip\7za\arm64\*" -Destination "$MountPath\Windows\System32" -Recurse -Force
            $global:BuildMedia.InstalledApps += '7zip'
        }
    }
}