-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
109 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
mod archived; | ||
mod downloader; | ||
|
||
pub use downloader::Downloader; |