| 1 | // Copyright (c) 2023 Harry [Majored] [[email protected]] |
| 2 | // Copyright (c) 2023 Cognite AS |
| 3 | // MIT License (https://github.com/Majored/rs-async-zip/blob/main/LICENSE) |
| 4 | |
| 5 | use crate::spec::header::{EndOfCentralDirectoryHeader, Zip64EndOfCentralDirectoryRecord}; |
| 6 | |
| 7 | /// Combines all the fields in EOCDR and Zip64EOCDR into one struct. |
| 8 | #[derive(Debug)] |
| 9 | pub struct CombinedCentralDirectoryRecord { |
| 10 | pub version_made_by: Option<u16>, |
| 11 | pub version_needed_to_extract: Option<u16>, |
| 12 | pub disk_number: u32, |
| 13 | pub disk_number_start_of_cd: u32, |
| 14 | pub num_entries_in_directory_on_disk: u64, |
| 15 | pub num_entries_in_directory: u64, |
| 16 | pub directory_size: u64, |
| 17 | pub offset_of_start_of_directory: u64, |
| 18 | pub file_comment_length: u16, |
| 19 | } |
| 20 | |
| 21 | impl CombinedCentralDirectoryRecord { |
| 22 | /// Combine an EOCDR with an optional Zip64EOCDR. |
| 23 | /// |
| 24 | /// Fields that are set to their max value in the EOCDR will be overwritten by the contents of |
| 25 | /// the corresponding Zip64EOCDR field. |
| 26 | pub fn combine(eocdr: EndOfCentralDirectoryHeader, zip64eocdr: Zip64EndOfCentralDirectoryRecord) -> Self { |
| 27 | let mut combined = Self::from(&eocdr); |
| 28 | if eocdr.disk_num == u16::MAX { |
| 29 | combined.disk_number = zip64eocdr.disk_number; |
| 30 | } |
| 31 | if eocdr.start_cent_dir_disk == u16::MAX { |
| 32 | combined.disk_number_start_of_cd = zip64eocdr.disk_number_start_of_cd; |
| 33 | } |
| 34 | if eocdr.num_of_entries_disk == u16::MAX { |
| 35 | combined.num_entries_in_directory_on_disk = zip64eocdr.num_entries_in_directory_on_disk; |
| 36 | } |
| 37 | if eocdr.num_of_entries == u16::MAX { |
| 38 | combined.num_entries_in_directory = zip64eocdr.num_entries_in_directory; |
| 39 | } |
| 40 | if eocdr.size_cent_dir == u32::MAX { |
| 41 | combined.directory_size = zip64eocdr.directory_size; |
| 42 | } |
| 43 | if eocdr.cent_dir_offset == u32::MAX { |
| 44 | combined.offset_of_start_of_directory = zip64eocdr.offset_of_start_of_directory; |
| 45 | } |
| 46 | combined.version_made_by = Some(zip64eocdr.version_made_by); |
| 47 | combined.version_needed_to_extract = Some(zip64eocdr.version_needed_to_extract); |
| 48 | |
| 49 | combined |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | // An implementation for the case of no zip64EOCDR. |
| 54 | impl From<&EndOfCentralDirectoryHeader> for CombinedCentralDirectoryRecord { |
| 55 | fn from(header: &EndOfCentralDirectoryHeader) -> Self { |
| 56 | Self { |
| 57 | version_made_by: None, |
| 58 | version_needed_to_extract: None, |
| 59 | disk_number: header.disk_num as u32, |
| 60 | disk_number_start_of_cd: header.start_cent_dir_disk as u32, |
| 61 | num_entries_in_directory_on_disk: header.num_of_entries_disk as u64, |
| 62 | num_entries_in_directory: header.num_of_entries as u64, |
| 63 | directory_size: header.size_cent_dir as u64, |
| 64 | offset_of_start_of_directory: header.cent_dir_offset as u64, |
| 65 | file_comment_length: header.file_comm_length, |
| 66 | } |
| 67 | } |
| 68 | } |