Exercise: Bundling outputs into a .zip

Goal

Write a function that bundles several named blobs into a single .zip archive:

fn write_zip_bundle(output_path: &Path, entries: &[(&str, &[u8])]) -> Result<(), Box<dyn std::error::Error>>

The function should also support reading entries back, via a sibling function read_zip_bundle. This is how you would hand a collaborator a complete per-sample QC report: one TSV of stats, one histogram PNG, one summary text file, all in one downloadable file.

Gzip vs zip — the one rule

The two compressed formats biologists use look superficially similar but are not the same thing:

Format What it is Tool
.gz / .bgz / .bgzf one compressed byte stream flate2, noodles-bgzf
.zip archive of named entries, each compressed zip

.fa.gz, .fastq.gz, .vcf.gz are all the first kind: a single sequence file with one compressed body. A .zip is the second kind: a directory’s worth of separate files. This exercise is about the second kind.

The crate

[dependencies]
zip = "0.6"

You need:

For reading:

The exercise

cd day4/ex-zip-bundle

Implement both write_zip_bundle and read_zip_bundle. Make all three tests pass — they round-trip a few small entries through an in-memory Cursor, then through a real temp file on disk.

Hints

let mut zw = zip::ZipWriter::new(file_or_cursor);
let options = zip::write::FileOptions::default();
for (name, body) in entries {
    zw.start_file(*name, options)?;
    zw.write_all(body)?;
}
zw.finish()?;

start_file takes the entry name as &str and the options struct.

let mut archive = zip::ZipArchive::new(reader)?;
let mut out: Vec<(String, Vec<u8>)> = Vec::with_capacity(archive.len());
for i in 0..archive.len() {
    let mut entry = archive.by_index(i)?;
    let name = entry.name().to_string();
    let mut body = Vec::new();
    entry.read_to_end(&mut body)?;
    out.push((name, body));
}
Ok(out)

archive.len() is the number of entries. by_index returns one entry at a time (you cannot hold two at once because reading the archive moves an internal cursor).

Solution

See day4/ex-zip-bundle-solution/src/main.rs for the full reference.

Where this leads

  • Real production reports often use tar + gzip instead of zip — same idea, different file format. Pick whichever your downstream tooling expects.
  • This is the natural endpoint for a day-4 capstone: stream FASTQ records with noodles-fastq, compute QC stats, render a histogram with plotters, and bundle the TSV + PNG into one .zip for handover.