added zip and rar support for unpacking
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
use std::{fs, path::Path};
|
||||
|
||||
use anyhow::anyhow;
|
||||
use log::error;
|
||||
use zip::ZipArchive;
|
||||
|
||||
use crate::types::{ModConfig, RootConfig};
|
||||
|
||||
@@ -15,7 +17,22 @@ pub fn unpack(
|
||||
return Err(anyhow!("File already exists"));
|
||||
}
|
||||
|
||||
unpack_7z_file(path, &extract_to)?;
|
||||
match path.as_ref().extension().and_then(|e| e.to_str()) {
|
||||
Some("7z") => unpack_7z_file(path, &extract_to),
|
||||
Some("zip") => unpack_zip_file(path, &extract_to),
|
||||
Some("rar") => unpack_rar(path, &extract_to),
|
||||
Some(ext) => {
|
||||
error!("Unsupported archive format: {}", ext);
|
||||
Err(anyhow!("Unsupported archive format: {}", ext))
|
||||
}
|
||||
None => {
|
||||
error!(
|
||||
"Failed to determine the file extension for {}",
|
||||
&path.as_ref().to_string_lossy()
|
||||
);
|
||||
Err(anyhow!("Failed to determine file extension"))
|
||||
}
|
||||
}?;
|
||||
|
||||
let new_mod = ModConfig::new(id, id);
|
||||
|
||||
@@ -26,3 +43,20 @@ fn unpack_7z_file(path: impl AsRef<Path>, to: impl AsRef<Path>) -> anyhow::Resul
|
||||
sevenz_rust2::decompress_file(path, to)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn unpack_zip_file(path: impl AsRef<Path>, to: impl AsRef<Path>) -> anyhow::Result<()> {
|
||||
let file = fs::File::open(path)?;
|
||||
let mut archive = ZipArchive::new(file)?;
|
||||
archive.extract(to)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn unpack_rar(path: impl AsRef<Path>, to: impl AsRef<Path>) -> anyhow::Result<()> {
|
||||
let mut archive = unrar::Archive::new(path.as_ref()).open_for_processing()?;
|
||||
|
||||
while let Some(header) = archive.read_header()? {
|
||||
archive = header.extract_with_base(&to)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user