Skip to content

Commit

Permalink
fix: spilt archived in a mod
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkgos committed Dec 14, 2023
1 parent a80b3e9 commit b9266c8
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 54 deletions.
36 changes: 36 additions & 0 deletions goup-downloader/src/archived/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
mod tgz;
mod zip;

use std::path::Path;

use anyhow::anyhow;

use tgz::Tgz;
use zip::Zip;

pub trait Unpacker {
fn unpack<P1, P2>(dest_dir: P1, archive_file: P2) -> Result<(), anyhow::Error>
where
P1: AsRef<Path>,
P2: AsRef<Path>;
}

/// Unpack unpacks the provided archive zip or tar.gz file to targetDir.
pub(crate) struct Unpack;

impl Unpacker for Unpack {
fn unpack<P1, P2>(dest_dir: P1, archive_file: P2) -> Result<(), anyhow::Error>
where
P1: AsRef<Path>,
P2: AsRef<Path>,
{
let p = archive_file.as_ref().to_string_lossy();
if p.ends_with(".zip") {
Zip::unpack(dest_dir, archive_file)
} else if p.ends_with(".tar.gz") {
Tgz::unpack(dest_dir, archive_file)
} else {
Err(anyhow!("unsupported archive file"))
}
}
}
33 changes: 33 additions & 0 deletions goup-downloader/src/archived/tgz.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use std::fs;
use std::{fs::File, path::Path};

use anyhow::anyhow;
use flate2::read::GzDecoder;
use tar::Archive;

use super::Unpacker;

/// archive *.tar.gz
pub(crate) struct Tgz;

impl Unpacker for Tgz {
fn unpack<P1, P2>(dest_dir: P1, archive_file: P2) -> Result<(), anyhow::Error>
where
P1: AsRef<Path>,
P2: AsRef<Path>,
{
let mut archive = Archive::new(GzDecoder::new(File::open(archive_file)?));
for entry in archive.entries()? {
let mut entry = entry?;
let path = entry.path()?;

let dest_file = dest_dir.as_ref().join(path);
let parent = dest_file.parent().ok_or(anyhow!("No parent path found"))?;
if !parent.exists() {
fs::create_dir_all(parent)?;
}
entry.unpack(dest_file)?;
}
Ok(())
}
}
34 changes: 34 additions & 0 deletions goup-downloader/src/archived/zip.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use std::fs;
use std::{fs::File, io, path::Path};

use anyhow::anyhow;
use zip::ZipArchive;

use super::Unpacker;

/// archive *.zip
pub(crate) struct Zip;

impl Unpacker for Zip {
fn unpack<P1, P2>(dest_dir: P1, archive_file: P2) -> Result<(), anyhow::Error>
where
P1: AsRef<Path>,
P2: AsRef<Path>,
{
let mut archive = ZipArchive::new(File::open(archive_file)?)?;
for i in 0..archive.len() {
let mut file = archive.by_index(i)?;
let path = file.mangled_name();

let dest_file = dest_dir.as_ref().join(path);
let parent = dest_file.parent().ok_or(anyhow!("No parent path found"))?;
if !parent.exists() {
fs::create_dir_all(parent)?;
}

let mut output_file = File::create(dest_file)?;
io::copy(&mut file, &mut output_file)?;
}
Ok(())
}
}
59 changes: 5 additions & 54 deletions goup-downloader/src/downloader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,20 @@ use std::{
ffi::OsStr,
fs,
fs::File,
io::{self, BufRead, BufReader, Read},
path::{Path, PathBuf},
io::{BufRead, BufReader, Read},
path::Path,
process::{Command, Stdio},
};

use anyhow::anyhow;
use flate2::read::GzDecoder;
use reqwest::{blocking, StatusCode};
use sha2::{Digest, Sha256};
use tar::Archive;
use zip::ZipArchive;

use goup_consts::consts;
use goup_version::Dir;

use crate::archived::{Unpack, Unpacker};

pub struct Downloader;

impl Downloader {
Expand Down Expand Up @@ -147,7 +146,7 @@ impl Downloader {
Self::verify_archive_file_sha256(&archive_file, &archive_url)?;
// 解压
println!("Unpacking {} ...", archive_file.display());
Self::unpack_archive(&version_dest_dir, &archive_file)?;
Unpack::unpack(&version_dest_dir, &archive_file)?;
Dir::create_dot_unpacked_success_file(&home, version)?;
// 设置解压成功
println!(
Expand Down Expand Up @@ -237,52 +236,4 @@ impl Downloader {
}
Ok(())
}

/// unpack_archive unpacks the provided archive zip or tar.gz file to targetDir,
/// removing the "go/" prefix from file entries.
fn unpack_archive(dest_dir: &Path, archive_file: &PathBuf) -> Result<(), anyhow::Error> {
let p = archive_file.to_string_lossy();
if p.ends_with(".zip") {
Self::unpack_zip(dest_dir, archive_file)
} else if p.ends_with(".tar.gz") {
Self::unpack_tgz(dest_dir, archive_file)
} else {
Err(anyhow!("unsupported archive file"))
}
}
/// unpack_zip unpack *.zip
fn unpack_zip(dest_dir: &Path, archive_file: &Path) -> Result<(), anyhow::Error> {
let mut archive = ZipArchive::new(File::open(archive_file)?)?;
for i in 0..archive.len() {
let mut file = archive.by_index(i)?;
let path = file.mangled_name();

let dest_file = dest_dir.join(path);
let parent = dest_file.parent().ok_or(anyhow!("No parent path found"))?;
if !parent.exists() {
fs::create_dir_all(parent)?;
}

let mut output_file = File::create(dest_file)?;
io::copy(&mut file, &mut output_file)?;
}
Ok(())
}

/// unpack_tgz unpack *.tar.gz
fn unpack_tgz(dest_dir: &Path, archive_file: &PathBuf) -> Result<(), anyhow::Error> {
let mut archive = Archive::new(GzDecoder::new(File::open(archive_file)?));
for entry in archive.entries()? {
let mut entry = entry?;
let path = entry.path()?;

let dest_file = dest_dir.join(path);
let parent = dest_file.parent().ok_or(anyhow!("No parent path found"))?;
if !parent.exists() {
fs::create_dir_all(parent)?;
}
entry.unpack(dest_file)?;
}
Ok(())
}
}
1 change: 1 addition & 0 deletions goup-downloader/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod archived;
mod downloader;

pub use downloader::Downloader;

0 comments on commit b9266c8

Please sign in to comment.