| 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 futures_lite::io::AsyncReadExt; |
| 6 | |
| 7 | use crate::tests::init_logger; |
| 8 | |
| 9 | const ZIP64_ZIP_CONTENTS: &str = "Hello World!\n"; |
| 10 | |
| 11 | /// Tests opening and reading a zip64 archive. |
| 12 | /// It contains one file named "-" with a zip 64 extended field header. |
| 13 | #[tokio::test] |
| 14 | async fn test_read_zip64_archive_mem() { |
| 15 | use crate::base::read::mem::ZipFileReader; |
| 16 | init_logger(); |
| 17 | |
| 18 | let data = include_bytes!("zip64.zip").to_vec(); |
| 19 | |
| 20 | let reader = ZipFileReader::new(data).await.unwrap(); |
| 21 | let mut entry_reader = reader.reader_without_entry(0).await.unwrap(); |
| 22 | |
| 23 | let mut read_data = String::new(); |
| 24 | entry_reader.read_to_string(&mut read_data).await.expect("read failed"); |
| 25 | |
| 26 | assert_eq!( |
| 27 | read_data.chars().count(), |
| 28 | ZIP64_ZIP_CONTENTS.chars().count(), |
| 29 | "{read_data:?} != {ZIP64_ZIP_CONTENTS:?}" |
| 30 | ); |
| 31 | assert_eq!(read_data, ZIP64_ZIP_CONTENTS); |
| 32 | } |
| 33 | |
| 34 | /// Like test_read_zip64_archive_mem() but for the streaming version |
| 35 | #[tokio::test] |
| 36 | async fn test_read_zip64_archive_stream() { |
| 37 | use crate::base::read::stream::ZipFileReader; |
| 38 | init_logger(); |
| 39 | |
| 40 | let data = include_bytes!("zip64.zip").to_vec(); |
| 41 | |
| 42 | let reader = ZipFileReader::new(data.as_slice()); |
| 43 | let mut entry_reader = reader.next_without_entry().await.unwrap().unwrap(); |
| 44 | |
| 45 | let mut read_data = String::new(); |
| 46 | entry_reader.reader_mut().read_to_string(&mut read_data).await.expect("read failed"); |
| 47 | |
| 48 | assert_eq!( |
| 49 | read_data.chars().count(), |
| 50 | ZIP64_ZIP_CONTENTS.chars().count(), |
| 51 | "{read_data:?} != {ZIP64_ZIP_CONTENTS:?}" |
| 52 | ); |
| 53 | assert_eq!(read_data, ZIP64_ZIP_CONTENTS); |
| 54 | } |
| 55 | |
| 56 | /// Generate an example file only if it doesn't exist already. |
| 57 | /// The file is placed adjacent to this rs file. |
| 58 | #[cfg(feature = "tokio")] |
| 59 | fn generate_zip64many_zip() -> std::path::PathBuf { |
| 60 | use std::io::Write; |
| 61 | use zip::write::{ExtendedFileOptions, FileOptions}; |
| 62 | |
| 63 | let mut path = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")); |
| 64 | path.push("src/tests/read/zip64/zip64many.zip"); |
| 65 | |
| 66 | // Only recreate the zip if it doesnt already exist. |
| 67 | if path.exists() { |
| 68 | return path; |
| 69 | } |
| 70 | |
| 71 | let zip_file = std::fs::File::create(&path).unwrap(); |
| 72 | let mut zip = zip::ZipWriter::new(zip_file); |
| 73 | let options: FileOptions<'_, ExtendedFileOptions> = |
| 74 | FileOptions::default().compression_method(zip::CompressionMethod::Stored); |
| 75 | |
| 76 | for i in 0..2_u32.pow(16) + 1 { |
| 77 | zip.start_file(format!("{i}.txt"), options.clone()).unwrap(); |
| 78 | zip.write_all(b"\n").unwrap(); |
| 79 | } |
| 80 | |
| 81 | zip.finish().unwrap(); |
| 82 | |
| 83 | path |
| 84 | } |
| 85 | |
| 86 | /// Test reading a generated zip64 archive that contains more than 2^16 entries. |
| 87 | #[cfg(feature = "tokio-fs")] |
| 88 | #[tokio::test] |
| 89 | async fn test_read_zip64_archive_many_entries() { |
| 90 | use crate::tokio::read::fs::ZipFileReader; |
| 91 | |
| 92 | init_logger(); |
| 93 | |
| 94 | let path = generate_zip64many_zip(); |
| 95 | |
| 96 | let reader = ZipFileReader::new(path).await.unwrap(); |
| 97 | |
| 98 | // Verify that each entry exists and is has the contents "\n" |
| 99 | for i in 0..2_u32.pow(16) + 1 { |
| 100 | let entry = reader.file().entries().get(i as usize).unwrap(); |
| 101 | eprintln!("{:?}", entry.filename().as_bytes()); |
| 102 | assert_eq!(entry.filename.as_str().unwrap(), format!("{i}.txt")); |
| 103 | let mut entry = reader.reader_without_entry(i as usize).await.unwrap(); |
| 104 | let mut contents = String::new(); |
| 105 | entry.read_to_string(&mut contents).await.unwrap(); |
| 106 | assert_eq!(contents, "\n"); |
| 107 | } |
| 108 | } |