//! A streaming zip writer for the export. Files go out as they are added; until the //! central directory is written at the end it keeps 32 bytes per file, with names //! looked up again by index, so millions of files fit in little memory. use std::io::{self, Write}; use flate2::Compression; use flate2::write::DeflateEncoder; const MAX32: u64 = 0xFFFF_FFFF; /// File names are UTF-8. const UTF8: u16 = 1 << 11; const DEFLATE: u16 = 8; /// Appends each value's little-endian bytes to `buf`. macro_rules! le { ($buf:expr, $($v:expr),+ $(,)?) => { $( $buf.extend_from_slice(&$v.to_le_bytes()); )+ }; } /// A file's compressed bytes, ready to add. pub struct Deflated { data: Vec, crc: u32, size: u64, } impl Deflated { pub fn new(bytes: &[u8]) -> io::Result { Self::with_level(bytes, Compression::default()) } /// Without compression, for media, which is already compressed. The output is still /// a valid DEFLATE stream (stored blocks), so the zip format needs no change. pub fn fast(bytes: &[u8]) -> io::Result { Self::with_level(bytes, Compression::none()) } fn with_level(bytes: &[u8], level: Compression) -> io::Result { let mut enc = DeflateEncoder::new(Vec::with_capacity(bytes.len() / 4 + 64), level); enc.write_all(bytes)?; Ok(Self { data: enc.finish()?, crc: crc32fast::hash(bytes), size: bytes.len() as u64 }) } } struct Entry { name: u32, crc: u32, csize: u64, size: u64, offset: u64, } pub struct ZipOut { out: W, at: u64, entries: Vec, date: u16, } impl ZipOut { /// Files are dated `(year, month, day)`, at midnight. pub fn new(out: W, (year, month, day): (i64, i64, i64)) -> Self { let date = (((year - 1980).clamp(0, 127) as u16) << 9) | ((month as u16) << 5) | day as u16; Self { out, at: 0, entries: Vec::new(), date } } /// Writes one file; `name` is what `finish` is given to find `path` again. pub fn add(&mut self, name: u32, path: &str, file: Deflated) -> io::Result<()> { let csize = file.data.len() as u64; let big = file.size >= MAX32 || csize >= MAX32; let (c32, s32) = if big { (MAX32, MAX32) } else { (csize, file.size) }; let mut h = Vec::with_capacity(50 + path.len()); le!(h, 0x0403_4b50u32, if big { 45u16 } else { 20u16 }, UTF8, DEFLATE, 0u16, self.date, file.crc); le!(h, c32 as u32, s32 as u32, path.len() as u16, if big { 20u16 } else { 0u16 }); h.extend_from_slice(path.as_bytes()); if big { le!(h, 1u16, 16u16, file.size, csize); } self.out.write_all(&h)?; self.out.write_all(&file.data)?; self.entries.push(Entry { name, crc: file.crc, csize, size: file.size, offset: self.at }); self.at += h.len() as u64 + csize; Ok(()) } /// Writes the central directory, `path` giving each file's path by its name index. pub fn finish<'a>(mut self, path: impl Fn(u32) -> &'a str) -> io::Result { let start = self.at; let mut h = Vec::with_capacity(256); for e in &self.entries { let path = path(e.name); let big = e.size >= MAX32 || e.csize >= MAX32 || e.offset >= MAX32; let small = |v: u64| if big { MAX32 as u32 } else { v as u32 }; h.clear(); le!(h, 0x0201_4b50u32, 45u16, if big { 45u16 } else { 20u16 }, UTF8, DEFLATE, 0u16, self.date, e.crc); le!(h, small(e.csize), small(e.size), path.len() as u16, if big { 28u16 } else { 0u16 }); le!(h, 0u16, 0u16, 0u16, 0u32, small(e.offset)); h.extend_from_slice(path.as_bytes()); if big { le!(h, 1u16, 24u16, e.size, e.csize, e.offset); } self.out.write_all(&h)?; self.at += h.len() as u64; } let (count, size) = (self.entries.len() as u64, self.at - start); h.clear(); if count >= 0xFFFF || size >= MAX32 || start >= MAX32 { le!(h, 0x0606_4b50u32, 44u64, 45u16, 45u16, 0u32, 0u32, count, count, size, start); le!(h, 0x0706_4b50u32, 0u32, self.at, 1u32); } let count16 = count.min(0xFFFF) as u16; le!(h, 0x0605_4b50u32, 0u16, 0u16, count16, count16, size.min(MAX32) as u32, start.min(MAX32) as u32, 0u16); self.out.write_all(&h)?; self.out.flush()?; Ok(self.out) } }