Files
craft-libs/crates/zip/tests/lenient_common/mod.rs
T
FredApps 712feab586 craft-zip: the lenient reader as craft_zip::lenient instead of a second ZIP crate
craft-unzip's raw-bytes reader moves unchanged into craft-zip as the
`lenient` module, behind the `lenient` feature (DEFLATE) and
`lenient-bzip2`, `lenient-lzma` and `lenient-zstd` for the other
methods, with `serde` for the listing types. craft-zip's default build
is unchanged; the module doesn't use the `zip` crate. Its unit and
integration tests move with it and run in craft-zip's test build.
2026-11-16 12:25:00 +01:00

42 lines
1.7 KiB
Rust

//! The stored-only ZIP writer the integration tests share, as in Craft Anything's testkit.
#![allow(dead_code, clippy::cast_possible_truncation)]
use craft_zip::lenient::crc32;
/// A stored-only ZIP of `files`.
pub fn zip_stored(files: &[(&str, &[u8])]) -> Vec<u8> {
let mut out = Vec::new();
let mut cd = Vec::new();
for (name, body) in files {
let off = out.len() as u32;
let crc = crc32(body);
let len = body.len() as u32;
out.extend_from_slice(b"PK\x03\x04\x14\0\0\0\0\0\0\0\0\0");
out.extend_from_slice(&crc.to_le_bytes());
out.extend_from_slice(&len.to_le_bytes());
out.extend_from_slice(&len.to_le_bytes());
out.extend_from_slice(&(name.len() as u16).to_le_bytes());
out.extend_from_slice(&0u16.to_le_bytes());
out.extend_from_slice(name.as_bytes());
out.extend_from_slice(body);
cd.extend_from_slice(b"PK\x01\x02\x14\0\x14\0\0\0\0\0\0\0\0\0");
cd.extend_from_slice(&crc.to_le_bytes());
cd.extend_from_slice(&len.to_le_bytes());
cd.extend_from_slice(&len.to_le_bytes());
cd.extend_from_slice(&(name.len() as u16).to_le_bytes());
cd.extend_from_slice(&[0; 12]);
cd.extend_from_slice(&off.to_le_bytes());
cd.extend_from_slice(name.as_bytes());
}
let cd_off = out.len() as u32;
out.extend_from_slice(&cd);
out.extend_from_slice(b"PK\x05\x06\0\0\0\0");
out.extend_from_slice(&(files.len() as u16).to_le_bytes());
out.extend_from_slice(&(files.len() as u16).to_le_bytes());
out.extend_from_slice(&(cd.len() as u32).to_le_bytes());
out.extend_from_slice(&cd_off.to_le_bytes());
out.extend_from_slice(&0u16.to_le_bytes());
out
}