diff options
author | GeckoEidechse <40122905+GeckoEidechse@users.noreply.github.com> | 2023-05-13 13:25:05 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-05-13 13:25:05 +0200 |
commit | 726b7466b34a936205a32098c56b7c723db34862 (patch) | |
tree | 0d0e5a725a605dd59ea450300ef28c34be98583c /src-tauri/src/util.rs | |
parent | ec0f0a92f8387f4c6d8b314536bc3acdcae56c66 (diff) | |
download | FlightCore-726b7466b34a936205a32098c56b7c723db34862.tar.gz FlightCore-726b7466b34a936205a32098c56b7c723db34862.zip |
refactor: Move `extract` function to util submod (#354)
Diffstat (limited to 'src-tauri/src/util.rs')
-rw-r--r-- | src-tauri/src/util.rs | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/src-tauri/src/util.rs b/src-tauri/src/util.rs index 0802d68c..93844af5 100644 --- a/src-tauri/src/util.rs +++ b/src-tauri/src/util.rs @@ -1,6 +1,8 @@ //! This module contains various utility/helper functions that do not fit into any other module +use anyhow::{Context, Result}; use serde::{Deserialize, Serialize}; +use zip::ZipArchive; use crate::constants::{APP_USER_AGENT, MASTER_SERVER_URL, SERVER_BROWSER_ENDPOINT}; @@ -60,3 +62,44 @@ pub async fn get_server_player_count() -> Result<(i32, usize), String> { Ok((total_player_count, server_count)) } + +/// Copied from `papa` source code and modified +///Extract N* zip file to target game path +// fn extract(ctx: &Ctx, zip_file: File, target: &Path) -> Result<()> { +pub fn extract(zip_file: std::fs::File, target: &std::path::Path) -> Result<()> { + let mut archive = ZipArchive::new(&zip_file).context("Unable to open zip archive")?; + for i in 0..archive.len() { + let mut f = archive.by_index(i).unwrap(); + + //This should work fine for N* because the dir structure *should* always be the same + if f.enclosed_name().unwrap().starts_with("Northstar") { + let out = target.join( + f.enclosed_name() + .unwrap() + .strip_prefix("Northstar") + .unwrap(), + ); + + if (*f.name()).ends_with('/') { + log::info!("Create directory {}", f.name()); + std::fs::create_dir_all(target.join(f.name())) + .context("Unable to create directory")?; + continue; + } else if let Some(p) = out.parent() { + std::fs::create_dir_all(p).context("Unable to create directory")?; + } + + let mut outfile = std::fs::OpenOptions::new() + .create(true) + .write(true) + .truncate(true) + .open(&out)?; + + log::info!("Write file {}", out.display()); + + std::io::copy(&mut f, &mut outfile).context("Unable to write to file")?; + } + } + + Ok(()) +} |