//! Streaming archive builders (zip / tar / tar.gz / tar.zst). //! //! Everything writes directly into an `impl std::io::Write` sink — no temp //! files, no full in-memory buffering. The sink is typically a channel that //! feeds the HTTP response body, so bytes reach the client while the tree is //! still being walked. use std::io::{self, Write}; use std::path::{Path, PathBuf}; use std::time::UNIX_EPOCH; /// The archive formats offered for folder downloads. #[derive(Clone, Copy, PartialEq, Eq, Debug)] pub enum ArchiveFormat { Zip, Tar, TarGz, TarZst, } impl ArchiveFormat { /// Parse the `format` query parameter. Unknown values are rejected. pub fn parse(s: &str) -> Option { match s { "zip" => Some(Self::Zip), "tar" => Some(Self::Tar), "tar.gz" | "tgz" => Some(Self::TarGz), "tar.zst" | "tzst" => Some(Self::TarZst), _ => None, } } /// The file-name suffix for the produced archive. pub fn extension(self) -> &'static str { match self { Self::Zip => "zip", Self::Tar => "tar", Self::TarGz => "tar.gz", Self::TarZst => "tar.zst", } } /// MIME type for the produced archive. pub fn mime(self) -> &'static str { match self { Self::Zip => "application/zip", Self::Tar => "application/x-tar", Self::TarGz => "application/gzip", Self::TarZst => "application/zstd", } } } /// Build `dir` (top-level entry named `top_name`) as `format`, streaming into /// `sink`. Blocking — call from `spawn_blocking`. pub fn build( format: ArchiveFormat, dir: &Path, top_name: &str, sink: impl Write, ) -> io::Result<()> { match format { ArchiveFormat::Tar => build_tar(dir, top_name, sink), ArchiveFormat::TarGz => build_tar_gz(dir, top_name, sink), ArchiveFormat::TarZst => build_tar_zst(dir, top_name, sink), ArchiveFormat::Zip => build_zip(dir, top_name, sink), } } fn mtime_secs(p: &Path) -> u64 { std::fs::metadata(p) .and_then(|m| m.modified()) .ok() .and_then(|t| t.duration_since(UNIX_EPOCH).ok()) .map(|d| d.as_secs()) .unwrap_or(0) } /// Depth-first walk. Invokes `f(entry_name, abs_path, is_dir)` for the /// directory itself and every descendant. Directory entry names carry no /// trailing slash; each format appends it as needed. Deterministic order /// (case-insensitive name) so archives are reproducible. fn walk io::Result<()>>( abs_dir: &Path, entry_prefix: &str, f: &mut F, ) -> io::Result<()> { f(entry_prefix, abs_dir, true)?; let rd = std::fs::read_dir(abs_dir)?; let mut children: Vec<(String, PathBuf, bool)> = Vec::new(); for e in rd.flatten() { let name = e.file_name().to_string_lossy().into_owned(); let p = e.path(); // Follows symlinks (a broken link shows up as an empty file). let is_dir = p.is_dir(); children.push((name, p, is_dir)); } children.sort_by(|a, b| a.0.to_lowercase().cmp(&b.0.to_lowercase())); for (name, p, is_dir) in children { let child = format!("{entry_prefix}/{name}"); if is_dir { walk(&p, &child, f)?; } else { f(&child, &p, false)?; } } Ok(()) } // --------------------------------------------------------------------------- // tar // --------------------------------------------------------------------------- fn tar_add( tar: &mut tar::Builder, entry: &str, abs: &Path, is_dir: bool, ) -> io::Result<()> { let mut header = tar::Header::new_gnu(); let meta = std::fs::metadata(abs)?; header.set_mode(if is_dir { 0o755 } else { 0o644 }); header.set_mtime(mtime_secs(abs)); let name = if is_dir { format!("{entry}/") } else { entry.to_string() }; if is_dir { header.set_entry_type(tar::EntryType::Directory); header.set_size(0); tar.append_data(&mut header, name, io::empty())?; } else { header.set_entry_type(tar::EntryType::Regular); header.set_size(meta.len()); let f = std::fs::File::open(abs)?; tar.append_data(&mut header, name, f)?; } Ok(()) } fn build_tar(dir: &Path, top: &str, sink: W) -> io::Result<()> { let mut tar = tar::Builder::new(sink); let mut add = |entry: &str, abs: &Path, is_dir: bool| -> io::Result<()> { tar_add(&mut tar, entry, abs, is_dir) }; walk(dir, top, &mut add)?; tar.finish()?; Ok(()) } fn build_tar_gz(dir: &Path, top: &str, sink: W) -> io::Result<()> { let enc = flate2::write::GzEncoder::new(sink, flate2::Compression::default()); let mut tar = tar::Builder::new(enc); let mut add = |entry: &str, abs: &Path, is_dir: bool| -> io::Result<()> { tar_add(&mut tar, entry, abs, is_dir) }; walk(dir, top, &mut add)?; let enc = tar.into_inner()?; enc.finish()?; Ok(()) } fn build_tar_zst(dir: &Path, top: &str, sink: W) -> io::Result<()> { let enc = zstd::stream::write::Encoder::new(sink, 3)?; let mut tar = tar::Builder::new(enc); let mut add = |entry: &str, abs: &Path, is_dir: bool| -> io::Result<()> { tar_add(&mut tar, entry, abs, is_dir) }; walk(dir, top, &mut add)?; let enc = tar.into_inner()?; enc.finish()?; Ok(()) } // --------------------------------------------------------------------------- // zip // --------------------------------------------------------------------------- fn build_zip(dir: &Path, top: &str, sink: W) -> io::Result<()> { // Streaming mode: no `Seek` needed, entries use data descriptors. let mut zip = zip::write::ZipWriter::new_stream(sink); let mut add = |entry: &str, abs: &Path, is_dir: bool| -> io::Result<()> { let opts = zip::write::SimpleFileOptions::default(); let name = if is_dir { format!("{entry}/") } else { entry.to_string() }; if is_dir { zip.add_directory(&name, opts)?; } else { zip.start_file(&name, opts)?; let mut f = std::fs::File::open(abs)?; io::copy(&mut f, &mut zip)?; } Ok(()) }; walk(dir, top, &mut add)?; zip.finish()?; Ok(()) }