Skip to content

Commit

Permalink
fix(unrar): extract_to should set parent path
Browse files Browse the repository at this point in the history
  • Loading branch information
deanrock committed Apr 22, 2024
1 parent 7fc7b31 commit e77883e
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ repository = "https://github.com/muja/unrar.rs"
regex = "1"
bitflags = "1"
widestring = "1"
tempfile = "3.10.1"

[dependencies.unrar_sys]
path = "unrar_sys"
Expand Down
6 changes: 5 additions & 1 deletion src/open_archive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -422,8 +422,12 @@ impl OpenArchive<Process, CursorBeforeFile> {
self,
dest: P,
) -> UnrarResult<OpenArchive<Process, CursorBeforeHeader>> {
let wpath = match dest.as_ref().parent() {
Some(parent) => Some(WideCString::from_os_str(parent).expect("Unexpected nul in destination")),
None => None,
};
let wdest = WideCString::from_os_str(dest.as_ref()).expect("Unexpected nul in destination");
self.process_file::<Extract>(None, Some(&wdest))
self.process_file::<Extract>(wpath.as_ref(), Some(&wdest))
}
}

Expand Down
34 changes: 33 additions & 1 deletion tests/simple.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use std::path::PathBuf;
use std::{env::set_current_dir, fs::read_to_string, path::{Path, PathBuf}};

use tempfile::tempdir;

#[test]
fn version_list() {
Expand All @@ -11,6 +13,36 @@ fn version_list() {
);
}

#[test]
fn version_extract_to_absolute_path() {
let archive = unrar::Archive::new("data/version.rar")
.open_for_processing()
.unwrap();

let tmp_dir = tempdir().unwrap();

let header = archive.read_header().unwrap().unwrap();
let filename = header.entry().filename.clone();
header.extract_to(tmp_dir.path().join(filename)).unwrap();

assert_eq!(read_to_string(tmp_dir.path().join("VERSION")).unwrap(), "unrar-0.4.0");
}

#[test]
fn version_extract_to_relative_path() {
let archive = unrar::Archive::new("data/version.rar")
.open_for_processing()
.unwrap();

let tmp_dir = tempdir().unwrap();
set_current_dir(&tmp_dir.path()).unwrap();

let header = archive.read_header().unwrap().unwrap();
header.extract_to("some-folder/VERSION").unwrap();

assert_eq!(read_to_string(tmp_dir.path().join("some-folder/VERSION")).unwrap(), "unrar-0.4.0");
}

#[test]
fn version_cat() {
let bytes = unrar::Archive::new("data/version.rar")
Expand Down

0 comments on commit e77883e

Please sign in to comment.