From e5f94912615e69c32c353fd6a63790e9b16685e4 Mon Sep 17 00:00:00 2001 From: 苏向夜 <46275354+fu050409@users.noreply.github.com> Date: Thu, 2 Apr 2026 11:27:09 +0800 Subject: refactor(modpack): split modpack module and extract curseforge api (#127) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: wsrsq Co-authored-by: 简律纯 Co-authored-by: HsiangNianian <44714368+HsiangNianian@users.noreply.github.com> --- src-tauri/src/core/modpack/archive.rs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 src-tauri/src/core/modpack/archive.rs (limited to 'src-tauri/src/core/modpack/archive.rs') diff --git a/src-tauri/src/core/modpack/archive.rs b/src-tauri/src/core/modpack/archive.rs new file mode 100644 index 0000000..89aaef0 --- /dev/null +++ b/src-tauri/src/core/modpack/archive.rs @@ -0,0 +1,25 @@ +use std::{fs, io::Read, path::Path}; + +pub(crate) type Archive = zip::ZipArchive; + +pub(crate) fn open(path: &Path) -> Result { + let file = fs::File::open(path).map_err(|e| format!("Failed to open: {e}"))?; + zip::ZipArchive::new(file).map_err(|e| format!("Invalid zip: {e}")) +} + +pub(crate) fn read_entry(archive: &mut Archive, name: &str) -> Option { + let mut buf = String::new(); + archive.by_name(name).ok()?.read_to_string(&mut buf).ok()?; + Some(buf) +} + +pub(crate) fn read_json(archive: &mut Archive, name: &str) -> Result { + let content = read_entry(archive, name).ok_or_else(|| format!("{name} not found"))?; + serde_json::from_str(&content).map_err(|e| e.to_string()) +} + +pub(crate) fn list_names(archive: &mut Archive) -> Vec { + (0..archive.len()) + .filter_map(|index| Some(archive.by_index_raw(index).ok()?.name().to_string())) + .collect() +} -- cgit v1.2.3-70-g09d2