Skip to content

Commit 9a11e5a

Browse files
committed
feat: 优化 zip 解包兼容性,增加 README。
1 parent 8a99c72 commit 9a11e5a

File tree

6 files changed

+60
-19
lines changed

6 files changed

+60
-19
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
[package]
22
name = "fetchbrowser"
3-
version = "0.1.0"
3+
version = "0.1.1"
44
edition = "2021"
55
authors = ["hamflx <[email protected]>"]
6+
license = "MIT"
7+
license-file = "LICENSE"
68

79
[[bin]]
810
name = "fb"

LICENSE

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
MIT License
2+
3+
Copyright 2023 hamflx
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6+
7+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8+
9+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Fetch Browser
2+
3+
浏览器下载器。支持下载指定版本的 `Chromium``Firefox`
4+
5+
## 安装(Windows)
6+
7+
`Powershell` 中输入如下命令:
8+
9+
```powershell
10+
irm https://raw.githubusercontent.com/hamflx/fetchbrowser/master/install.ps1 | iex
11+
```
12+
13+
## 使用
14+
15+
下载 `Chromium 98`
16+
17+
```powershell
18+
fb 98
19+
```
20+
21+
**注意:在特定平台第一次下载 `Chromium` 会比较慢,因为会联机查找版本信息,后续会使用缓存的数据。**
22+
23+
下载 `Firefox 98`
24+
25+
```powershell
26+
fb --firefox 98
27+
```

install.ps1

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,26 @@
1+
$ErrorActionPreference = 'Stop'
2+
13
$repo = "hamflx/fetchbrowser"
24
$file = "fb.exe"
35

4-
$releases = "https://api.github.com/repos/$repo/releases"
6+
$releases = "https://api.github.com/repos/${repo}/releases"
57

68
Write-Host Determining latest release
79
$tag = (Invoke-WebRequest $releases | ConvertFrom-Json)[0].tag_name
810

9-
$download = "https://github.com/$repo/releases/download/$tag/$file"
10-
$fb_dir = "$HOME/.fb"
11-
$fb_bin_dir = "$fb_dir/bin"
12-
$fb_bin_path = "$fb_bin_dir/$file"
11+
$download = "https://github.com/${repo}/releases/download/${tag}/${file}"
12+
$fb_dir = "${HOME}\.fb"
13+
$fb_bin_dir = "${fb_dir}\bin"
14+
$fb_bin_path = "${fb_bin_dir}\$file"
1315

14-
New-Item "$fb_bin_dir" -ItemType Directory -Force
16+
New-Item "${fb_bin_dir}" -ItemType Directory -Force
1517

1618
Write-Host Dowloading latest release
1719
Invoke-WebRequest $download -Out $fb_bin_path
1820

1921
$old_path = [System.Environment]::GetEnvironmentVariable("PATH", "User")
20-
if ($old_path -notcontains $fb_bin_path) {
22+
if (!(";${old_path};".ToLower() -like "*;${fb_bin_path};*".ToLower())) {
2123
$new_path = $old_path + [IO.Path]::PathSeparator + $fb_bin_path
2224
[Environment]::SetEnvironmentVariable("PATH", $new_path, "User")
25+
$Env:Path += ";${fb_bin_path}"
2326
}

src/main.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,6 @@ fn download_zip_file(
133133
println!("==> downloading {}", zip_file.media_link);
134134
let mut win_zip_response = reqwest::blocking::get(&zip_file.media_link)?;
135135

136-
// 执行 zip 解压过程,并去除压缩包的根目录。
137-
let mut prefix = String::new();
138136
loop {
139137
let mut zip = match read_zipfile_from_stream(&mut win_zip_response) {
140138
Ok(Some(zip)) => zip,
@@ -145,14 +143,13 @@ fn download_zip_file(
145143
let zip_name = zip.name();
146144
println!("==> unzip: {zip_name}");
147145

148-
if prefix.is_empty() {
149-
if zip.is_dir() {
150-
prefix = zip.name().to_owned();
151-
} else {
152-
return Err(anyhow!("压缩包内目录结构不正确。"));
153-
}
154-
} else if zip_name.starts_with(&prefix) {
155-
let file_path = base_path.join(&zip_name[prefix.len()..]);
146+
if zip_name.starts_with("chrome-win/")
147+
|| zip_name.starts_with("chrome-win32/")
148+
|| zip_name.starts_with("chrome-mac/")
149+
|| zip_name.starts_with("chrome-linux/")
150+
{
151+
let prefix_len = zip_name.find('/').unwrap() + 1;
152+
let file_path = base_path.join(&zip_name[prefix_len..]);
156153
if zip.is_dir() {
157154
std::fs::create_dir_all(&file_path).map_err(|err| {
158155
anyhow!(
@@ -162,6 +159,9 @@ fn download_zip_file(
162159
)
163160
})?;
164161
} else {
162+
if let Some(parent_dir) = file_path.parent() {
163+
let _ = std::fs::create_dir_all(parent_dir);
164+
}
165165
copy(
166166
&mut zip,
167167
&mut OpenOptions::new()

0 commit comments

Comments
 (0)