Skip to content

Commit

Permalink
Merge pull request #6 from acridotheres/rustfmt-e12e47585622af6746e96…
Browse files Browse the repository at this point in the history
…fe04b2fe3db22bde75f

Format code using rustfmt for e12e475
  • Loading branch information
Le0X8 authored Jul 12, 2024
2 parents e12e475 + db30bf7 commit b21ff91
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 12 deletions.
11 changes: 9 additions & 2 deletions src/file.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{
cmp::min,
fs::{OpenOptions, FileTimes},
fs::{FileTimes, OpenOptions},
io::{Read, Seek, Write},
};

Expand All @@ -20,7 +20,14 @@ impl<'a> File<'a> {
Self { path, file, pos: 0 }
}

pub fn part_to_file(&mut self, offset: u64, len: u64, path: &str, modified: DateTime<Utc>, buffer_size: u64) {
pub fn part_to_file(
&mut self,
offset: u64,
len: u64,
path: &str,
modified: DateTime<Utc>,
buffer_size: u64,
) {
let mut target = OpenOptions::new()
.write(true)
.create(true)
Expand Down
15 changes: 13 additions & 2 deletions src/formats/zip/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,21 @@ pub fn get_file(file: &mut File, entry: &ZipFileEntry) -> Vec<u8> {
file.read_u8array(entry.uncompressed_size as u64)
}

pub fn extract_to(file: &mut File, entries: Vec<ZipFileEntry>, buffer_size: u64, path_rewriter: &dyn Fn(&str) -> String) {
pub fn extract_to(
file: &mut File,
entries: Vec<ZipFileEntry>,
buffer_size: u64,
path_rewriter: &dyn Fn(&str) -> String,
) {
for entry in entries {
let path = path_rewriter(&entry.file.path);
file.part_to_file(entry.file.offset, entry.file.size, path.as_str(), entry.file.modified, buffer_size);
file.part_to_file(
entry.file.offset,
entry.file.size,
path.as_str(),
entry.file.modified,
buffer_size,
);
}
}

Expand Down
19 changes: 11 additions & 8 deletions tests/zip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,20 @@ fn metadata_001() {

std::fs::create_dir_all("tests/samples/zip/001").unwrap();

corelib::formats::zip::parser::extract_to(
&mut file,
metadata.files,
1024,
&|path| format!("tests/samples/zip/001/{}", path),
);
corelib::formats::zip::parser::extract_to(&mut file, metadata.files, 1024, &|path| {
format!("tests/samples/zip/001/{}", path)
});

let extracted_test_txt = std::fs::read("tests/samples/zip/001/test.txt").unwrap();
assert_eq!(String::from_utf8(extracted_test_txt).unwrap(), "Hello, world!\n");
assert_eq!(
String::from_utf8(extracted_test_txt).unwrap(),
"Hello, world!\n"
);
let extracted_test2_txt = std::fs::read("tests/samples/zip/001/test2.txt").unwrap();
assert_eq!(String::from_utf8(extracted_test2_txt).unwrap(), "Hello, world! 2\n");
assert_eq!(
String::from_utf8(extracted_test2_txt).unwrap(),
"Hello, world! 2\n"
);

std::fs::remove_dir_all("tests/samples/zip/001").unwrap();
}

0 comments on commit b21ff91

Please sign in to comment.