Skip to content

Commit

Permalink
Fixes rusty-ferris-club#17: Upgrade to [email protected] to upgrade num-big…
Browse files Browse the repository at this point in the history
…int etc.
  • Loading branch information
rcook committed Sep 3, 2024
1 parent 8d9ef73 commit bc4c5ec
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 178 deletions.
164 changes: 12 additions & 152 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion decompress/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ bzip2 = { version = "0.4.3", optional = true }
flate2 = { version = "1.0.25", optional = true }
xz = { version = "0.1.0", optional = true }
zstd = { version = "0.12.0", optional = true }
unrar = { version = "0.4.4", optional = true }
unrar = { version = "0.5.6", optional = true }
infer = "0.12.0"

[dev-dependencies]
Expand Down
53 changes: 28 additions & 25 deletions decompress/src/decompressors/unrar.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
use lazy_static::lazy_static;
use regex::Regex;
use std::path::Path;
use unrar::FileHeader;

use crate::{DecompressError, Decompression, Decompressor, ExtractOpts, Listing};

macro_rules! check {
($e : expr) => {
$e.map_err(|e| crate::DecompressError::Error(e.to_string()))?
};
}

lazy_static! {
static ref RE: Regex = Regex::new(r"(?i)\.rar$").unwrap();
}
Expand All @@ -21,6 +28,14 @@ impl Unrar {
pub fn build(re: Option<Regex>) -> Box<Self> {
Box::new(Self::new(re))
}

fn get_entry_path(entry: &FileHeader) -> String {
let mut s = entry.filename.to_string_lossy().into_owned();
if entry.is_directory() && !s.ends_with('/') {
s.push('/');
}
s
}
}

impl Decompressor for Unrar {
Expand All @@ -36,19 +51,12 @@ impl Decompressor for Unrar {
}

fn list(&self, archive: &Path) -> Result<Listing, DecompressError> {
let res = unrar::Archive::new(archive.to_string_lossy().to_string())
.list()
.map_err(|e| DecompressError::Error(e.to_string()))?
.process()
.map_err(|e| DecompressError::Error(e.to_string()))?;

Ok(Listing {
id: "rar",
entries: res
.iter()
.map(std::string::ToString::to_string)
.collect::<Vec<_>>(),
})
let rar = check!(unrar::Archive::new(archive).open_for_listing());
let entries = rar
.into_iter()
.map(|e| Ok(Self::get_entry_path(&check!(e))))
.collect::<Result<Vec<_>, DecompressError>>()?;
Ok(Listing { id: "rar", entries })
}

fn decompress(
Expand All @@ -62,18 +70,13 @@ impl Decompressor for Unrar {
fs::create_dir_all(to)?;
}

let res = unrar::Archive::new(archive.to_string_lossy().to_string())
.extract_to(to.to_string_lossy().to_string())
.map_err(|e| DecompressError::Error(e.to_string()))?
.process()
.map_err(|e| DecompressError::Error(e.to_string()))?;
let mut rar = check!(unrar::Archive::new(archive).open_for_processing());
let mut files = Vec::new();
while let Some(header) = check!(rar.read_header()) {
files.push(Self::get_entry_path(header.entry()));
rar = check!(header.extract_with_base(to));
}

Ok(Decompression {
id: "rar",
files: res
.iter()
.map(std::string::ToString::to_string)
.collect::<Vec<_>>(),
})
Ok(Decompression { id: "rar", files })
}
}

0 comments on commit bc4c5ec

Please sign in to comment.