function DownloadFileInChunks {
    param (
        [string]$url,
        [string]$destinationFile,
        [int]$chunkSizeGB = 1
    )

    # 将 GB 转换为字节
    $chunkSize = $chunkSizeGB * 1024 * 1024 * 1024

    # 获取文件的总大小
    $response = Invoke-WebRequest -Uri $url -Method Head
    $totalSize = [long]$response.Headers["Content-Length"]

    # 计算分段数量
    $chunks = [math]::Ceiling($totalSize / $chunkSize)

    # 下载每一段
    for ($i = 0; $i -lt $chunks; $i++) {
        $start = $i * $chunkSize
        $end = [math]::Min($start + $chunkSize - 1, $totalSize - 1)

        $headers = @{
            "Range" = "bytes=$start-$end"
        }

        $chunkResponse = Invoke-WebRequest -Uri $url -Headers $headers -Method Get
        $chunkData = $chunkResponse.Content

        # 以追加模式写入文件
        [System.IO.File]::WriteAllBytes($destinationFile, $chunkData)
    }

    Write-Host "dowonload ok"
}

# 指定文件路径
$filePath = "D:\CmiiDeployOffline\ZheJiangErJiPingTai\all-gzip-image-list.txt"
# 指定下载的目标目录
$destinationFolder = "D:\CmiiDeployOffline\ZheJiangErJiPingTai"

$oss_prefix_url="https://oss.demo.uavcmlc.com/cmlc-installation/6.1.1/"

# 创建目标目录（如果不存在）
#if (-not (Test-Path $destinationFolder)) {
#    New-Item -ItemType Directory -Path $destinationFolder
#}

#Write-Host "[DLTU] - 开始下载镜像压缩文件"
Write-Host ""

# 读取文件并下载每一行的 URL
Get-Content $filePath | ForEach-Object {
    $url = $_
    Write-Host "start to download $oss_prefix_url$url"

    $fileName = Join-Path $destinationFolder $url
    Write-Host $fileName
    DownloadFileInChunks -url $oss_prefix_url$url -destinationFile $fileName -chunkSizeGB 0.1
    #    Invoke-WebRequest -Uri $oss_prefix_url$url -OutFile $fileName
    Write-Host ""
}







