Skip to content

Commit

Permalink
Added directory support
Browse files Browse the repository at this point in the history
  • Loading branch information
Le0X8 committed Jul 13, 2024
1 parent b730a82 commit 5f74b30
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 17 deletions.
25 changes: 15 additions & 10 deletions src/formats/zip/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{
File,
};

pub fn metadata(file: &mut File) -> ZipArchiveMetadata {
pub fn metadata<'a>(file: &mut File) -> ZipArchiveMetadata<'a> {
let local_files = read_local_files(file);

let signature = local_files.1;
Expand All @@ -31,17 +31,21 @@ pub fn extract(
) {
for entry in entries {
let path = path_rewriter(&entry.file.path);
file.export(
entry.file.offset,
entry.file.size,
path.as_str(),
entry.file.modified,
buffer_size,
);
if !entry.file.is_directory {
file.export(
entry.file.offset,
entry.file.size,
path.as_str(),
entry.file.modified,
buffer_size,
);
} else {
std::fs::create_dir_all(path).unwrap();
};
}
}

fn read_local_files(file: &mut File) -> (Vec<ZipFileEntry>, u32) {
fn read_local_files<'a>(file: &mut File) -> (Vec<ZipFileEntry<'a>>, u32) {
let mut files: Vec<ZipFileEntry> = Vec::new();

let mut signature: u32 = file.read_u32le();
Expand Down Expand Up @@ -90,10 +94,11 @@ fn read_local_files(file: &mut File) -> (Vec<ZipFileEntry>, u32) {
let extra = file.read_u8array(extra_length as u64);
files.push(ZipFileEntry {
file: FileEntry {
path: name,
offset: file.get_position(),
size: size_compressed as u64,
modified: msdos::parse(lastmod_date, lastmod_time),
is_directory: name.ends_with('/'),
path: name,
},
version,
bit_flag,
Expand Down
15 changes: 8 additions & 7 deletions src/types.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use chrono::{DateTime, Utc};

#[derive(Debug)]
pub struct ArchiveMetadata {
pub format: &'static str,
pub struct ArchiveMetadata<'a> {
pub format: &'a str,
}

#[derive(Debug)]
pub struct ZipArchiveMetadata {
pub archive: ArchiveMetadata,
pub files: Vec<ZipFileEntry>,
pub struct ZipArchiveMetadata<'a> {
pub archive: ArchiveMetadata<'a>,
pub files: Vec<ZipFileEntry<'a>>,
}

#[derive(Debug)]
Expand All @@ -17,15 +17,16 @@ pub struct FileEntry {
pub offset: u64,
pub size: u64,
pub modified: DateTime<Utc>,
pub is_directory: bool,
}

#[derive(Debug)]
pub struct ZipFileEntry {
pub struct ZipFileEntry<'a> {
pub file: FileEntry,
pub uncompressed_size: u32,
pub checksum: u32,
pub extra_field: Vec<u8>,
pub version: u16,
pub bit_flag: u16,
pub compression: &'static str,
pub compression: &'a str,
}

0 comments on commit 5f74b30

Please sign in to comment.