CentralDirectoryFileHeader

Struct CentralDirectoryFileHeader 

Source
pub struct CentralDirectoryFileHeader {
Show 16 fields pub version_made_by: u16, pub version_needed: u16, pub flag: u16, pub compression_method: Option<CompressionMethod>, pub mod_time: u16, pub mod_date: u16, pub crc32: u32, pub compressed_size: u64, pub uncompressed_size: u64, pub filename: String, pub extra_fields: Vec<FileHeaderExtraField>, pub disk_number: u32, pub internal_attributes: u16, pub external_attributes: u32, pub local_header_offset: u64, pub header_size: usize,
}
Expand description

Represents the result of reading a central directory file header (CDFH)

The layout of this object does not follow the original ZIP CDFH structure

Fields§

§version_made_by: u16§version_needed: u16§flag: u16§compression_method: Option<CompressionMethod>§mod_time: u16§mod_date: u16§crc32: u32§compressed_size: u64§uncompressed_size: u64§filename: String§extra_fields: Vec<FileHeaderExtraField>§disk_number: u32§internal_attributes: u16§external_attributes: u32§local_header_offset: u64§header_size: usize

Implementations§

Source§

impl CentralDirectoryFileHeader

Source

pub fn from_bytes(data: impl AsRef<[u8]>) -> Option<Self>

Attempts to read a central directory file header from the provided byte buffer. Returns None if there isn’t enought data

Source

pub fn is_directory(&self) -> bool

Examples found in repository?
examples/archive.rs (line 30)
5fn main() {
6    let output_dir = "unpack";
7    let archive = fs::read("archive.zip").unwrap();
8    let _ = fs::remove_dir_all(output_dir);
9
10    let central_directory = read_cd::from_provider(
11        vec![archive.len()],
12        false,
13        |pos, length| {
14            println!("Requested {length} bytes at {pos}");
15            Ok(archive[(pos.offset)..(pos.offset + length)].to_owned())
16        }
17    ).unwrap().sort();
18
19    let current_file: RefCell<Option<File>> = RefCell::new(None);
20
21    let mut unpacker = ZipUnpacker::new(central_directory, vec![archive.len()]);
22    unpacker.set_callback(|data| {
23        match data {
24            ZipDecodedData::FileHeader(cdfh, _) => {
25                println!();
26
27                let mut path = PathBuf::from(output_dir);
28                path.push(&cdfh.filename);
29
30                if !cdfh.is_directory() {
31                    print!("New file: {}", cdfh.filename);
32                    io::stdout().flush()?;
33
34                    fs::create_dir_all(path.parent().unwrap())?;
35
36                    *current_file.borrow_mut() = Some(
37                        OpenOptions::new()
38                        .create(true)
39                        .write(true)
40                        .open(path)?
41                    );
42                } else {
43                    print!("New directory: {}", cdfh.filename);
44                    io::stdout().flush()?;
45
46                    fs::create_dir_all(path)?;
47                }
48            },
49
50            ZipDecodedData::FileData(data) => {
51                print!(".");
52                io::stdout().flush()?;
53
54                current_file.borrow().as_ref().unwrap().write_all(data)?;
55            }
56        }
57
58        Ok(())
59    });
60
61    unpacker.update(archive).unwrap();
62
63    println!("\nDone!");
64}
More examples
Hide additional examples
examples/multipart.rs (line 42)
5fn main() {
6    let output_dir = "unpack";
7
8    let archives = [
9        "multipart/archive_m.z01",
10        "multipart/archive_m.z02",
11        "multipart/archive_m.z03",
12        "multipart/archive_m.z04",
13        "multipart/archive_m.z05",
14        "multipart/archive_m.z06",
15        "multipart/archive_m.z07",
16        "multipart/archive_m.zip"
17    ].map(fs::read).map(Result::unwrap);
18    let sizes = archives.iter().map(Vec::len).collect::<Vec<_>>();
19
20    let _ = fs::remove_dir_all(output_dir);
21
22    let central_directory = read_cd::from_provider(
23        &sizes,
24        false,
25        |pos, length| {
26            println!("Requested {length} bytes at {pos}");
27            Ok(archives[pos.disk][(pos.offset)..(pos.offset + length)].to_owned())
28        }
29    ).unwrap().sort();
30
31    let current_file: RefCell<Option<File>> = RefCell::new(None);
32
33    let mut unpacker = ZipUnpacker::new(central_directory, sizes);
34    unpacker.set_callback(|data| {
35        match data {
36            ZipDecodedData::FileHeader(cdfh, _) => {
37                println!();
38
39                let mut path = PathBuf::from(output_dir);
40                path.push(&cdfh.filename);
41
42                if !cdfh.is_directory() {
43                    print!("New file: {}", cdfh.filename);
44                    io::stdout().flush()?;
45
46                    fs::create_dir_all(path.parent().unwrap())?;
47
48                    *current_file.borrow_mut() = Some(
49                        OpenOptions::new()
50                        .create(true)
51                        .write(true)
52                        .open(path)?
53                    );
54                } else {
55                    print!("New directory: {}", cdfh.filename);
56                    io::stdout().flush()?;
57
58                    fs::create_dir_all(path)?;
59                }
60            },
61
62            ZipDecodedData::FileData(data) => {
63                print!(".");
64                io::stdout().flush()?;
65
66                current_file.borrow().as_ref().unwrap().write_all(data)?;
67            }
68        }
69
70        Ok(())
71    });
72
73    for archive in archives {
74        unpacker.update(archive).unwrap();
75    }
76
77    println!("\nDone!");
78}
Source

pub fn header_position(&self) -> ZipPosition

Returns the ZipPosition of the LFH corresponding to this CDFH

Trait Implementations§

Source§

impl Clone for CentralDirectoryFileHeader

Source§

fn clone(&self) -> CentralDirectoryFileHeader

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for CentralDirectoryFileHeader

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.