file-compression-and-archiving
Summary
Compress and archive multiple heterogeneous output artifacts (spectral images, annotated data files) into a single ZIP container for efficient delivery and client-side extraction. This skill is essential in spectral processing workflows where the analysis produces multiple file types (PNG images, modified JCAMP files) that must be bundled and transmitted together.
When to use
When a spectral processing operation produces multiple output artifacts in different formats (e.g., both PNG renderings and JCAMP peak-annotated files) that need to be returned to a client as a single downloadable unit, or when transmission efficiency or atomic delivery of interdependent outputs is required.
When NOT to use
- When output artifacts need to be retrieved independently or asynchronously (use streaming or individual endpoints instead)
- When the total uncompressed size exceeds available server memory or client storage capacity
- When downstream workflows require individual file access before decompression (prefer multi-part form responses or indexed storage)
Inputs
- Multiple PNG image files (rendered spectral plots with annotated peaks)
- One or more JCAMP files (peak-annotated spectral data files in .dx or .jdx format)
- Metadata or log files associated with the spectral processing operation
Outputs
- Single ZIP archive file containing all input artifacts
- HTTP response with application/zip MIME type and Content-Disposition header
How to apply
After generating all output artifacts from spectral peak detection and image rendering, collect all files (JCAMP files and PNG images) into a staging directory. Use Python's standard zipfile module or equivalent to recursively compress all outputs into a single ZIP archive. Set appropriate MIME type (application/zip) and HTTP headers (Content-Disposition with attachment filename) before returning the archive to the client. The rationale is to provide atomic, self-contained delivery of all analysis outputs in a single HTTP response, eliminating the need for multiple round-trips and simplifying client-side file management.
Related tools
- Python 3 zipfile module (Native archiving and compression of multiple output files into a single ZIP container)
- Flask (HTTP framework for setting MIME type, headers, and serving the ZIP archive to the client) — https://github.com/ComPlat/chem-spectra-app
- chem-spectra-app (Backend service that generates the PNG and JCAMP artifacts to be archived) — https://github.com/ComPlat/chem-spectra-app
Examples
import zipfile; z = zipfile.ZipFile('output.zip', 'w'); z.write('spectrum_plot.png'); z.write('peaks_annotated.jdx'); z.close()
Evaluation signals
- ZIP archive is valid and can be extracted without corruption (verify with
unzip -tor equivalent) - All expected output files (PNG images and JCAMP files) are present in the archive with correct counts and filenames
- HTTP response headers include Content-Type: application/zip and appropriate Content-Disposition attachment filename
- Compressed file size is smaller than the sum of uncompressed artifacts (compression ratio > 0 for lossless formats like text JCAMP)
- Client can successfully download, decompress, and access individual artifacts without data loss or format corruption
Limitations
- ZIP compression is lossless but provides limited compression for already-compressed formats (PNG images); JCAMP text files compress better
- Large archives may exceed client memory or disk constraints; no streaming or chunked delivery mechanism is described
- No versioning or incremental archiving strategy is documented; all outputs must be regenerated and re-archived for each request
- The skill assumes all outputs fit in server memory before archiving; no out-of-core or temporary file strategies are mentioned
Evidence
- [other] Create modified JCAMP output file(s) incorporating the peak table annotations. Compress all output artifacts (JCAMP files and PNG images) into a single ZIP file.: "Compress all output artifacts (JCAMP files and PNG images) into a single ZIP file."
- [other] Return the ZIP archive to the client with appropriate MIME type and headers.: "Return the ZIP archive to the client with appropriate MIME type and headers."
- [other] Receive POST request with spectral file attachment in .dx or .jdx format at the /zip_jcamp_n_img endpoint.: "Receive POST request with spectral file attachment in .dx or .jdx format at the /zip_jcamp_n_img endpoint."