8.15iO流

异常处理与IO流详解


8.12

异常分为可查异常和不可查异常

异常子类必须放在父类前面.

可以抛出父类异常

如果父类的方法抛出异常,子类重写此方法是否需要抛出异常,可不可以抛出父类异常的子类

 

IO流(输入输出


按单位分类分为字节流(8位)和字符流(16位)

字节(输入输出)流:网络传输

字符(输入输出)流:易懂


Modify:最后修改时间                 Directory:路径          mkdir建立子文件


节点流:普通的输入输出

处理流:对字节流进行了处理     


缓冲流:节省IO次数。

input和output不仅可以对文件操作,也可以对网络和外围设备操作

InputStream是从文件里面读出来,OutputStream是从为文件里面写进去。

File构造方法可以输入一个对象 

一般定义一个字节数组长度为1024Byte

字符流


Io流都有可能出现Io异常(编译异常)。(文件与结束异常,文件未找到异常)

字符流返回的实际长度小于1024,已经读完。


把对象的状态存储起来称为序列化


PS C:\Users\Administrator> # 创建模块目录 >> $moduleDir = "$env:USERPROFILE\Documents\WindowsPowerShell\Modules\CurlTools" >> New-Item -Path $moduleDir -ItemType Directory -Force | Out-Null >> >> # 修正后的 CurlTools.psm1 文件内容 >> $moduleContent = @' >> # 模块主路径 >> $script:CurlPath = "E:\curl-8.15.0_4-win64-mingw\bin" >> >> # 获取 curl 版本信息 >> function Get-CurlVersion { >> # 获取已安装版本 >> try { >> $versionOutput = curl --version 2>&1 | Out-String >> if (-not ($versionOutput -match 'curl (\d+\.\d+\.\d+)')) { >> throw "无法解析 curl 版本信息" >> } >> $installedVersion = $Matches[1] >> } catch { >> Write-Warning "获取已安装版本失败: $_" >> $installedVersion = "0.0.0" >> } >> >> # 获取最新版本 >> try { >> # 尝试从 GitHub API 获取最新版本 >> $releaseInfo = curl -s -L https://api.github.com/repos/curl/curl/releases/latest >> $json = $releaseInfo | ConvertFrom-Json >> $latestVersion = $json.tag_name -replace 'curl-', '' -replace '_', '.' >> >> if (-not $latestVersion) { >> throw "无法从 GitHub 解析版本" >> } >> } catch { >> Write-Warning "获取最新版本失败: $_" >> $latestVersion = $installedVersion >> } >> >> return [PSCustomObject]@{ >> Installed = $installedVersion >> Latest = $latestVersion >> UpdateCmd = "winget install curl.curl" >> } >> } >> >> # 切换 curl 版本 >> function Set-CurlVersion { >> [CmdletBinding()] >> param( >> [Parameter(Mandatory=$true)] >> [ValidateSet("8.15", "system")] >> [string]$Version >> ) >> >> $pathItems = [System.Collections.ArrayList]@( >> [Environment]::GetEnvironmentVariable("Path", "Machine") -split ';' | >> Where-Object { $_ -ne "" } >> ) >> >> switch ($Version) { >> "8.15" { >> # 移除系统路径并添加自定义路径 >> $pathItems.Remove("C:\Windows\System32") | Out-Null >> if (-not $pathItems.Contains($script:CurlPath)) { >> $pathItems.Insert(0, $script:CurlPath) >> } >> } >> "system" { >> # 移除自定义路径并添加系统路径 >> $pathItems.Remove($script:CurlPath) | Out-Null >> if (-not $pathItems.Contains("C:\Windows\System32")) { >> $pathItems.Add("C:\Windows\System32") | Out-Null >> } >> } >> } >> >> $newPath = $pathItems -join ';' >> [Environment]::SetEnvironmentVariable("Path", $newPath, "Machine") >> $env:Path = [Environment]::GetEnvironmentVariable("Path", "Machine") >> >> Write-Host "已切换到 curl $Version 版本" -ForegroundColor Green >> } >> >> # 安全下载函数 >> function Invoke-SecureDownload { >> [CmdletBinding()] >> param( >> [Parameter(Mandatory=$true)] >> [string]$Url, >> >> [Parameter(Mandatory=$true)] >> [string]$ExpectedHash, >> >> [ValidateSet("SHA1", "SHA256", "SHA384", "SHA512", "MD5")] >> [string]$Algorithm = "SHA256", >> >> [string]$OutputPath = (Split-Path -Leaf $Url) >> ) >> >> try { >> # 创建下载目录 >> $downloadDir = Split-Path -Path $OutputPath -Parent >> if (-not [string]::IsNullOrEmpty($downloadDir) -and -not (Test-Path $downloadDir)) { >> New-Item -ItemType Directory -Path $downloadDir -Force | Out-Null >> } >> >> # 下载文件 >> & "$script:CurlPath\curl.exe" -L -o $OutputPath -C - $Url >> >> # 验证哈希 >> $actualHash = (Get-FileHash -Path $OutputPath -Algorithm $Algorithm).Hash >> >> if ($actualHash -ne $ExpectedHash) { >> Remove-Item $OutputPath -Force >> throw "文件哈希验证失败! 期望: $ExpectedHash, 实际: $actualHash" >> } >> >> Write-Host "✅ 文件验证成功: $OutputPath" -ForegroundColor Green >> return $OutputPath >> } catch { >> Write-Error "下载失败: $_" >> return $null >> } >> } >> >> # 导出模块成员 >> Export-ModuleMember -Function Get-CurlVersion, Set-CurlVersion, Invoke-SecureDownload >> '@ >> >> $moduleContent | Set-Content -Path "$moduleDir\CurlTools.psm1" >> >> # 创建模块清单文件 >> $manifestContent = @' >> @{ >> ModuleVersion = '1.0.0' >> GUID = 'a5b3c7d9-ef01-4f67-9e12-34567890abcd' >> Author = 'PowerShell Administrator' >> Description = '高级 curl 工具集' >> RootModule = 'CurlTools.psm1' >> FunctionsToExport = @('Get-CurlVersion', 'Set-CurlVersion', 'Invoke-SecureDownload') >> PowerShellVersion = '5.1' >> } >> '@ >> >> $manifestContent | Set-Content -Path "$moduleDir\CurlTools.psd1" >> PS C:\Users\Administrator> # 1. 导入模块 >> Import-Module CurlTools -Force >> >> # 2. 检查 curl 版本 >> $versionInfo = Get-CurlVersion >> if ([version]$versionInfo.Installed -lt [version]$versionInfo.Latest) { >> Write-Host "⚠️ 发现新版本: $($versionInfo.Latest) (当前: $($versionInfo.Installed))" -ForegroundColor Yellow >> Write-Host "执行更新命令: $($versionInfo.UpdateCmd)" -ForegroundColor Cyan >> } else { >> Write-Host "✅ 已安装最新版 curl $($versionInfo.Installed)" -ForegroundColor Green >> } >> >> # 3. 安全下载示例(使用实际哈希值) >> $fileUrl = "https://github.com/curl/curl/releases/download/curl-8_15_0/curl-8.15.0.zip" >> $expectedHash = "B7B1409A8C05C6C464488F8CB38AF2090D39AC88B5DCC3FEF0E1EFE8103F77E2" # 实际哈希值 >> $downloadedFile = Invoke-SecureDownload -Url $fileUrl -ExpectedHash $expectedHash >> >> # 4. 版本切换测试 >> Set-CurlVersion -Version system >> curl --version # 验证系统版本 >> >> Set-CurlVersion -Version 8.15 >> curl --version # 验证自定义版本 >> >> # 5. 综合测试函数 >> function Test-CurlToolsFunctionality { >> Write-Host "=== 开始 CurlTools 功能测试 ===" -ForegroundColor Cyan >> >> # 测试1: 版本检查 >> Write-Host "测试1: 版本检查" -ForegroundColor Yellow >> $versionInfo = Get-CurlVersion >> Write-Host "当前版本: $($versionInfo.Installed)" >> Write-Host "最新版本: $($versionInfo.Latest)" >> >> # 测试2: 版本切换 >> Write-Host "`n测试2: 版本切换" -ForegroundColor Yellow >> Set-CurlVersion -Version system >> $sysVersion = curl --version | Select-String "curl" >> Write-Host "系统版本: $($sysVersion.ToString().Trim())" >> >> Set-CurlVersion -Version 8.15 >> $customVersion = curl --version | Select-String "curl" >> Write-Host "自定义版本: $($customVersion.ToString().Trim())" >> >> # 测试3: 安全下载 >> Write-Host "`n测试3: 安全下载" -ForegroundColor Yellow >> $testFileUrl = "https://raw.githubusercontent.com/curl/curl/master/docs/examples/multi-app.c" >> $testFileHash = "7A3A6A6C2A4C5D5B6A7C8D9E0F1A2B3C4D5E6F7A8B9C0D1E2F3A4B5C6D7E8F9A0B1" # 示例哈希 >> try { >> $downloaded = Invoke-SecureDownload -Url $testFileUrl -ExpectedHash $testFileHash >> Write-Host "下载成功: $downloaded" -ForegroundColor Green >> } catch { >> Write-Host "下载测试失败: $_" -ForegroundColor Red >> } >> >> Write-Host "`n=== 测试完成 ===" -ForegroundColor Green >> } >> >> # 运行测试 >> Test-CurlToolsFunctionality >> 警告: 获取最新版本失败: 无法从 GitHub 解析版本 ✅ 已安装最新版 curl 8.15.0 Invoke-SecureDownload : 下载失败: 文件哈希验证失败! 期望: B7B1409A8C05C6C464488F8CB38AF2090D39AC88B5DCC3FEF0E1EFE8103F7 7E2, 实际: B15A6C0F56FBC8FDF92DBE594F8FBD6E5917793CE9C000864916F862F0178D3B 所在位置 行:16 字符: 19 + ... oadedFile = Invoke-SecureDownload -Url $fileUrl -ExpectedHash $expect ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Invoke-SecureDownload 已切换到 curl system 版本 curl 8.15.0 (x86_64-w64-mingw32) libcurl/8.15.0 LibreSSL/4.1.0 zlib/1.3.1.zlib-ng brotli/1.1.0 zstd/1.5.7 WinIDN libpsl/0.21.5 libssh2/1.11.1 nghttp2/1.66.0 ngtcp2/1.14.0 nghttp3/1.11.0 Release-Date: 2025-07-16 Protocols: dict file ftp ftps gopher gophers http https imap imaps ipfs ipns ldap ldaps mqtt pop3 pop3s rtsp scp sftp smb smbs smtp smtps telnet tftp ws wss Features: alt-svc AsynchDNS brotli CAcert HSTS HTTP2 HTTP3 HTTPS-proxy IDN IPv6 Kerberos Largefile libz NTLM PSL SPNEGO SSL SSLS-EXPORT SSPI threadsafe UnixSockets zstd 已切换到 curl 8.15 版本 curl 8.15.0 (x86_64-w64-mingw32) libcurl/8.15.0 LibreSSL/4.1.0 zlib/1.3.1.zlib-ng brotli/1.1.0 zstd/1.5.7 WinIDN libpsl/0.21.5 libssh2/1.11.1 nghttp2/1.66.0 ngtcp2/1.14.0 nghttp3/1.11.0 Release-Date: 2025-07-16 Protocols: dict file ftp ftps gopher gophers http https imap imaps ipfs ipns ldap ldaps mqtt pop3 pop3s rtsp scp sftp smb smbs smtp smtps telnet tftp ws wss Features: alt-svc AsynchDNS brotli CAcert HSTS HTTP2 HTTP3 HTTPS-proxy IDN IPv6 Kerberos Largefile libz NTLM PSL SPNEGO SSL SSLS-EXPORT SSPI threadsafe UnixSockets zstd === 开始 CurlTools 功能测试 === 测试1: 版本检查 警告: 获取最新版本失败: 无法从 GitHub 解析版本 当前版本: 8.15.0 最新版本: 8.15.0 测试2: 版本切换 已切换到 curl system 版本 系统版本: curl 8.15.0 (x86_64-w64-mingw32) libcurl/8.15.0 LibreSSL/4.1.0 zlib/1.3.1.zlib-ng brotli/1.1.0 zstd/1.5.7 WinIDN libpsl/0.21.5 libssh2/1.11.1 nghttp2/1.66.0 ngtcp2/1.14.0 nghttp3/1.11.0 已切换到 curl 8.15 版本 自定义版本: curl 8.15.0 (x86_64-w64-mingw32) libcurl/8.15.0 LibreSSL/4.1.0 zlib/1.3.1.zlib-ng brotli/1.1.0 zstd/1.5.7 WinIDN libpsl/0.21.5 libssh2/1.11.1 nghttp2/1.66.0 ngtcp2/1.14.0 nghttp3/1.11.0 测试3: 安全下载 Invoke-SecureDownload : 下载失败: 文件哈希验证失败! 期望: 7A3A6A6C2A4C5D5B6A7C8D9E0F1A2B3C4D5E6F7A8B9C0D1E2F3A4B5C6D7E8 F9A0B1, 实际: 81F03B4A99256458B688B8E0E8A50BE2DEBEBEC19CBDD597ECF219574195730F 所在位置 行:50 字符: 23 + ... ownloaded = Invoke-SecureDownload -Url $testFileUrl -ExpectedHash $te ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Invoke-SecureDownload 下载成功: === 测试完成 === PS C:\Users\Administrator>
08-13
PS C:\Users\Administrator> function Get-CurlVersion { >> # 获取已安装版本 >> try { >> $versionOutput = curl --version 2>&1 | Out-String >> if (-not ($versionOutput -match 'curl (\d+\.\d+\.\d+)')) { >> throw "无法解析 curl 版本信息" >> } >> $installedVersion = $Matches[1] >> } catch { >> Write-Warning "获取已安装版本失败: $_" >> $installedVersion = "0.0.0" >> } >> >> # 获取最新版本 - 使用更可靠的备用方案 >> try { >> # 方案1: 使用官方下载页面解析 >> $releasePage = curl -s -L https://curl.se/download.html >> if ($releasePage -match 'The latest release: curl (\d+\.\d+\.\d+)') { >> $latestVersion = $Matches[1] >> } >> # 方案2: 使用 GitHub API 带认证 >> elseif (-not $latestVersion) { >> $headers = @{ >> "User-Agent" = "PowerShell-CurlTools" >> "Accept" = "application/vnd.github.v3+json" >> } >> $releaseInfo = curl -s -L -H $headers https://api.github.com/repos/curl/curl/releases/latest >> $json = $releaseInfo | ConvertFrom-Json >> $latestVersion = $json.tag_name -replace 'curl-', '' -replace '_', '.' >> } >> >> if (-not $latestVersion) { >> throw "无法获取最新版本" >> } >> } catch { >> Write-Warning "获取最新版本失败: $_" >> $latestVersion = $installedVersion >> } >> >> return [PSCustomObject]@{ >> Installed = $installedVersion >> Latest = $latestVersion >> UpdateCmd = "winget install curl.curl" >> } >> } >> PS C:\Users\Administrator> function Get-FileHashFromUrl { >> param( >> [string]$Url, >> [string]$Algorithm = "SHA256" >> ) >> >> $tempFile = [System.IO.Path]::GetTempFileName() >> try { >> & "$script:CurlPath\curl.exe" -L -o $tempFile $Url >> return (Get-FileHash -Path $tempFile -Algorithm $Algorithm).Hash >> } catch { >> Write-Warning "无法获取文件哈希: $_" >> return $null >> } finally { >> Remove-Item $tempFile -Force -ErrorAction SilentlyContinue >> } >> } >> >> function Invoke-SecureDownload { >> [CmdletBinding()] >> param( >> [Parameter(Mandatory=$true)] >> [string]$Url, >> >> [string]$ExpectedHash, # 改为可选参数 >> >> [ValidateSet("SHA1", "SHA256", "SHA384", "SHA512", "MD5")] >> [string]$Algorithm = "SHA256", >> >> [string]$OutputPath = (Split-Path -Leaf $Url), >> >> [switch]$AutoVerify # 新增自动验证开关 >> ) >> >> try { >> # 创建下载目录 >> $downloadDir = Split-Path -Path $OutputPath -Parent >> if (-not [string]::IsNullOrEmpty($downloadDir) -and -not (Test-Path $downloadDir)) { >> New-Item -ItemType Directory -Path $downloadDir -Force | Out-Null >> } >> >> # 下载文件 >> Write-Host "下载文件: $Url" -ForegroundColor Cyan >> & "$script:CurlPath\curl.exe" -L -o $OutputPath -C - $Url >> >> # 自动获取哈希值(如果需要) >> if ($AutoVerify -or (-not $ExpectedHash)) { >> $ExpectedHash = Get-FileHashFromUrl -Url $Url -Algorithm $Algorithm >> if (-not $ExpectedHash) { >> throw "无法获取远程文件的$Algorithm哈希值" >> } >> Write-Host "自动获取哈希值: $ExpectedHash" -ForegroundColor Yellow >> } >> >> # 验证哈希 >> $actualHash = (Get-FileHash -Path $OutputPath -Algorithm $Algorithm).Hash >> >> if ($actualHash -ne $ExpectedHash) { >> Remove-Item $OutputPath -Force >> throw "文件哈希验证失败! 期望: $ExpectedHash, 实际: $actualHash" >> } >> >> Write-Host "✅ 文件验证成功: $OutputPath" -ForegroundColor Green >> return $OutputPath >> } catch { >> Write-Error "下载失败: $_" >> return $null >> } >> } >> PS C:\Users\Administrator> # 更新模块内容 >> $moduleDir = "$env:USERPROFILE\Documents\WindowsPowerShell\Modules\CurlTools" >> $moduleContent = @" >> # 模块主路径 >> `$script:CurlPath = "E:\curl-8.15.0_4-win64-mingw\bin" >> >> function Get-FileHashFromUrl { >> param( >> [string]`$Url, >> [string]`$Algorithm = "SHA256" >> ) >> >> `$tempFile = [System.IO.Path]::GetTempFileName() >> try { >> & "`$script:CurlPath\curl.exe" -L -o `$tempFile `$Url >> return (Get-FileHash -Path `$tempFile -Algorithm `$Algorithm).Hash >> } catch { >> Write-Warning "无法获取文件哈希: `$_" >> return `$null >> } finally { >> Remove-Item `$tempFile -Force -ErrorAction SilentlyContinue >> } >> } >> >> function Get-CurlVersion { >> # 获取已安装版本 >> try { >> `$versionOutput = curl --version 2>&1 | Out-String >> if (-not (`$versionOutput -match 'curl (\d+\.\d+\.\d+)')) { >> throw "无法解析 curl 版本信息" >> } >> `$installedVersion = `$Matches[1] >> } catch { >> Write-Warning "获取已安装版本失败: `$_" >> `$installedVersion = "0.0.0" >> } >> >> # 获取最新版本 - 使用更可靠的备用方案 >> try { >> # 方案1: 使用官方下载页面解析 >> `$releasePage = curl -s -L https://curl.se/download.html >> if (`$releasePage -match 'The latest release: curl (\d+\.\d+\.\d+)') { >> `$latestVersion = `$Matches[1] >> } >> # 方案2: 使用 GitHub API 带认证 >> elseif (-not `$latestVersion) { >> `$headers = @{ >> "User-Agent" = "PowerShell-CurlTools" >> "Accept" = "application/vnd.github.v3+json" >> } >> `$releaseInfo = curl -s -L -H `$headers https://api.github.com/repos/curl/curl/releases/latest >> `$json = `$releaseInfo | ConvertFrom-Json >> `$latestVersion = `$json.tag_name -replace 'curl-', '' -replace '_', '.' >> } >> >> if (-not `$latestVersion) { >> throw "无法获取最新版本" >> } >> } catch { >> Write-Warning "获取最新版本失败: `$_" >> `$latestVersion = `$installedVersion >> } >> >> return [PSCustomObject]@{ >> Installed = `$installedVersion >> Latest = `$latestVersion >> UpdateCmd = "winget install curl.curl" >> } >> } >> >> function Set-CurlVersion { >> [CmdletBinding()] >> param( >> [Parameter(Mandatory=`$true)] >> [ValidateSet("8.15", "system")] >> [string]`$Version >> ) >> >> `$pathItems = [System.Collections.ArrayList]@( >> [Environment]::GetEnvironmentVariable("Path", "Machine") -split ';' | >> Where-Object { `$_ -ne "" } >> ) >> >> switch (`$Version) { >> "8.15" { >> # 移除系统路径并添加自定义路径 >> `$pathItems.Remove("C:\Windows\System32") | Out-Null >> if (-not `$pathItems.Contains(`$script:CurlPath)) { >> `$pathItems.Insert(0, `$script:CurlPath) >> } >> } >> "system" { >> # 移除自定义路径并添加系统路径 >> `$pathItems.Remove(`$script:CurlPath) | Out-Null >> if (-not `$pathItems.Contains("C:\Windows\System32")) { >> `$pathItems.Add("C:\Windows\System32") | Out-Null >> } >> } >> } >> >> `$newPath = `$pathItems -join ';' >> [Environment]::SetEnvironmentVariable("Path", `$newPath, "Machine") >> `$env:Path = [Environment]::GetEnvironmentVariable("Path", "Machine") >> >> Write-Host "已切换到 curl `$Version 版本" -ForegroundColor Green >> } >> >> function Invoke-SecureDownload { >> [CmdletBinding()] >> param( >> [Parameter(Mandatory=`$true)] >> [string]`$Url, >> >> [string]`$ExpectedHash, # 改为可选参数 >> >> [ValidateSet("SHA1", "SHA256", "SHA384", "SHA512", "MD5")] >> [string]`$Algorithm = "SHA256", >> >> [string]`$OutputPath = (Split-Path -Leaf `$Url), >> >> [switch]`$AutoVerify # 新增自动验证开关 >> ) >> >> try { >> # 创建下载目录 >> `$downloadDir = Split-Path -Path `$OutputPath -Parent >> if (-not [string]::IsNullOrEmpty(`$downloadDir) -and -not (Test-Path `$downloadDir)) { >> New-Item -ItemType Directory -Path `$downloadDir -Force | Out-Null >> } >> >> # 下载文件 >> Write-Host "下载文件: `$Url" -ForegroundColor Cyan >> & "`$script:CurlPath\curl.exe" -L -o `$OutputPath -C - `$Url >> >> # 自动获取哈希值(如果需要) >> if (`$AutoVerify -or (-not `$ExpectedHash)) { >> `$ExpectedHash = Get-FileHashFromUrl -Url `$Url -Algorithm `$Algorithm >> if (-not `$ExpectedHash) { >> throw "无法获取远程文件的`$Algorithm哈希值" >> } >> Write-Host "自动获取哈希值: `$ExpectedHash" -ForegroundColor Yellow >> } >> >> # 验证哈希 >> `$actualHash = (Get-FileHash -Path `$OutputPath -Algorithm `$Algorithm).Hash >> >> if (`$actualHash -ne `$ExpectedHash) { >> Remove-Item `$OutputPath -Force >> throw "文件哈希验证失败! 期望: `$ExpectedHash, 实际: `$actualHash" >> } >> >> Write-Host "✅ 文件验证成功: `$OutputPath" -ForegroundColor Green >> return `$OutputPath >> } catch { >> Write-Error "下载失败: `$_" >> return `$null >> } >> } >> >> # 导出模块成员 >> Export-ModuleMember -Function Get-CurlVersion, Set-CurlVersion, Invoke-SecureDownload, Get-FileHashFromUrl >> "@ >> >> $moduleContent | Set-Content -Path "$moduleDir\CurlTools.psm1" -Force >> PS C:\Users\Administrator> # 1. 重新加载模块 >> Remove-Module CurlTools -ErrorAction SilentlyContinue >> Import-Module CurlTools -Force >> >> # 2. 测试版本检查 >> Write-Host "`n=== 测试版本检查 ===" -ForegroundColor Cyan >> $versionInfo = Get-CurlVersion >> Write-Host "当前版本: $($versionInfo.Installed)" >> Write-Host "最新版本: $($versionInfo.Latest)" >> if ([version]$versionInfo.Installed -lt [version]$versionInfo.Latest) { >> Write-Host "⚠️ 发现新版本!" -ForegroundColor Yellow >> } else { >> Write-Host "✅ 已安装最新版" -ForegroundColor Green >> } >> >> # 3. 测试安全下载(自动获取哈希) >> Write-Host "`n=== 测试安全下载(自动哈希)===" -ForegroundColor Cyan >> $testFileUrl = "https://raw.githubusercontent.com/curl/curl/master/docs/examples/multi-app.c" >> $downloadedFile = Invoke-SecureDownload -Url $testFileUrl -AutoVerify >> if ($downloadedFile) { >> Write-Host "下载成功: $downloadedFile" -ForegroundColor Green >> } >> >> # 4. 测试安全下载(手动指定哈希) >> Write-Host "`n=== 测试安全下载(指定哈希)===" -ForegroundColor Cyan >> $fileUrl = "https://github.com/curl/curl/releases/download/curl-8_15_0/curl-8.15.0.zip" >> $expectedHash = "B7B1409A8C05C6C464488F8CB38AF2090D39AC88B5DCC3FEF0E1EFE8103F77E2" >> $downloaded = Invoke-SecureDownload -Url $fileUrl -ExpectedHash $expectedHash >> if ($downloaded) { >> Write-Host "下载成功: $downloaded" -ForegroundColor Green >> } >> >> # 5. 测试版本切换 >> Write-Host "`n=== 测试版本切换 ===" -ForegroundColor Cyan >> Set-CurlVersion -Version system >> curl --version | Select-String "curl" | ForEach-Object { >> Write-Host "系统版本: $($_.ToString().Trim())" >> } >> >> Set-CurlVersion -Version 8.15 >> curl --version | Select-String "curl" | ForEach-Object { >> Write-Host "自定义版本: $($_.ToString().Trim())" >> } >> === 测试版本检查 === 警告: 获取最新版本失败: 无法获取最新版本 当前版本: 8.15.0 最新版本: 8.15.0 ✅ 已安装最新版 === 测试安全下载(自动哈希)=== 下载文件: https://raw.githubusercontent.com/curl/curl/master/docs/examples/multi-app.c 自动获取哈希值: 81F03B4A99256458B688B8E0E8A50BE2DEBEBEC19CBDD597ECF219574195730F ? 文件验证成功: multi-app.c 下载成功: multi-app.c === 测试安全下载(指定哈希)=== 下载文件: https://github.com/curl/curl/releases/download/curl-8_15_0/curl-8.15.0.zip Invoke-SecureDownload : 下载失败: 文件哈希验证失败! 期望: B7B1409A8C05C6C464488F8CB38AF2090D39AC88B5DCC3FEF0E1EFE8103F7 7E2, 实际: C4244B2B6DB7239CD1DAB2629E9D609B15D876E0AA15A5F4AC59BA7DD37C5159 所在位置 行:28 字符: 15 + ... ownloaded = Invoke-SecureDownload -Url $fileUrl -ExpectedHash $expect ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Invoke-SecureDownload === 测试版本切换 === 已切换到 curl system 版本 系统版本: curl 8.15.0 (x86_64-w64-mingw32) libcurl/8.15.0 LibreSSL/4.1.0 zlib/1.3.1.zlib-ng brotli/1.1.0 zstd/1.5.7 WinIDN libpsl/0.21.5 libssh2/1.11.1 nghttp2/1.66.0 ngtcp2/1.14.0 nghttp3/1.11.0 已切换到 curl 8.15 版本 自定义版本: curl 8.15.0 (x86_64-w64-mingw32) libcurl/8.15.0 LibreSSL/4.1.0 zlib/1.3.1.zlib-ng brotli/1.1.0 zstd/1.5.7 WinIDN libpsl/0.21.5 libssh2/1.11.1 nghttp2/1.66.0 ngtcp2/1.14.0 nghttp3/1.11.0 PS C:\Users\Administrator>
08-13
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值