Skip to content

Commit

Permalink
feat: 添加 解压 方法
Browse files Browse the repository at this point in the history
  • Loading branch information
1739616529 committed Jan 11, 2024
1 parent 0d42c67 commit fb30a26
Showing 1 changed file with 16 additions and 12 deletions.
28 changes: 16 additions & 12 deletions crates/root/src/util/download.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::path::Path;

use futures_util::StreamExt;
use tokio::{fs::{File, create_dir_all}, io::AsyncWriteExt};
use std::path::Path;
use tokio::{fs::create_dir_all, io::AsyncWriteExt};
use tokio::fs::File;

pub async fn download_file(url: &str, path: &Path) -> Result<(), Box<dyn std::error::Error>> {
let mut file = File::create(path).await?;
Expand All @@ -14,26 +14,30 @@ pub async fn download_file(url: &str, path: &Path) -> Result<(), Box<dyn std::er
Ok(())
}


pub async fn unzip_file(zip_file_dir: &Path, output_dir: &Path) -> Result<(), Box<dyn std::error::Error>> {

pub async fn unzip_file(
zip_file_dir: &Path,
output_dir: &Path,
) -> Result<(), Box<dyn std::error::Error>> {
create_dir_all(output_dir.parent().unwrap()).await.unwrap();

#[cfg(target_os = "windows")]
{
sevenz_rust::decompress_file(zip_file_dir, output_dir).unwrap();
}

// #[cfg(any(target_os = "linux", target_os = "macos"))]
// {

// }
#[cfg(any(target_os = "linux", target_os = "macos"))]
{
let mut unzip_file = File::open(zip_file_dir).await.unwrap();
let mut unzip_file_buf = Vec::new();
unzip_file.read_to_end(&mut unzip_file_buf).await.unwrap();
let xz = XzDecoder::new(&unzip_file_buf[..]);
let mut archive = Archive::new(xz);
archive.unpack(output_dir).unwrap();
}

Ok(())
}



#[cfg(test)]
mod test {

Expand Down

0 comments on commit fb30a26

Please sign in to comment.