1+ # 设置错误处理
2+ $ErrorActionPreference = " Stop"
3+ $ProgressPreference = ' SilentlyContinue'
4+
5+ $RESTIC_VERSION = " 0.18.1"
6+
7+ # --- 1. 确保 7-Zip 已安装 ---
8+ function Ensure-7Zip {
9+ if (Get-Command " 7z" - ErrorAction SilentlyContinue) {
10+ return " 7z"
11+ }
12+
13+ $path7z = " ${env: ProgramFiles} \7-Zip\7z.exe"
14+ if (Test-Path $path7z ) {
15+ return $path7z
16+ }
17+
18+ Write-Host " [-] 未找到 7-Zip,正在尝试通过 winget 安装..." - ForegroundColor Yellow
19+ try {
20+ # 使用 winget 静默安装 7-Zip
21+ Start-Process winget - ArgumentList " install --id 7zip.7zip --silent --accept-source-agreements --accept-package-agreements" - Wait
22+ if (Test-Path $path7z ) { return $path7z }
23+ } catch {
24+ Write-Error " 自动安装 7-Zip 失败,请手动安装: https://www.7-zip.org/"
25+ }
26+
27+ throw " 无法定位 7z 命令行工具。"
28+ }
29+
30+ $Global :ZipExe = Ensure- 7Zip
31+
32+ # --- 2. 下载与解压逻辑 ---
33+ function Download-ResticBinary {
34+ param (
35+ [string ]$resticArch ,
36+ [string ]$androidArch
37+ )
38+
39+ $currentDir = Get-Location
40+ $targetPath = Join-Path $currentDir " source/app/src/main/jniLibs/$androidArch "
41+
42+ if (-not (Test-Path $targetPath )) {
43+ New-Item - ItemType Directory - Path $targetPath - Force | Out-Null
44+ }
45+
46+ $resticFile = " restic_${RESTIC_VERSION} _linux_${resticArch} .bz2"
47+ $url = " https://github.com/restic/restic/releases/download/v${RESTIC_VERSION} /$resticFile "
48+ $tempFile = Join-Path $targetPath $resticFile
49+ $finalFile = Join-Path $targetPath " librestic.so"
50+
51+ Write-Host " `n [+] Processing: $androidArch " - ForegroundColor Cyan
52+
53+ # 下载
54+ Write-Host " Downloading..." - NoNewline
55+ Invoke-WebRequest - Uri $url - OutFile $tempFile
56+ Write-Host " Done." - ForegroundColor Green
57+
58+ # 解压
59+ Write-Host " Decompressing via 7-Zip..." - NoNewline
60+
61+ # 7-Zip 解压 bz2 会产生一个去掉 .bz2 后缀的文件
62+ # -y: 自动确认, -so: 输出到标准输出 (类似 bzip2 -dc)
63+ try {
64+ if (Test-Path $finalFile ) { Remove-Item $finalFile - Force }
65+
66+ # 使用 & 调用变量存储的路径,并重定向输出流
67+ & $Global :ZipExe x " $tempFile " - so > " $finalFile "
68+
69+ if ((Get-Item " $finalFile " ).Length -gt 1 MB ) {
70+ Write-Host " Success." - ForegroundColor Green
71+ Remove-Item $tempFile - Force
72+ } else {
73+ throw " 解压文件损坏或体积异常。"
74+ }
75+ } catch {
76+ Write-Host " Failed." - ForegroundColor Red
77+ Write-Error $_
78+ }
79+ }
80+
81+ # --- 3. 执行任务 ---
82+ Download- ResticBinary " arm64" " arm64-v8a"
83+ Download- ResticBinary " arm" " armeabi-v7a"
84+ Download- ResticBinary " amd64" " x86_64"
85+ Download- ResticBinary " 386" " x86"
86+
87+ $ProgressPreference = ' Continue'
88+ Write-Host " `n [!] 所有架构处理完毕。" - ForegroundColor Green
0 commit comments