Pick files, check them, download them as one zip
Let users pick files, check them, and download them as one zip.
Apache-2.0Runs on this page
Package: @awacloud/fw
A file input lists the picked files with their CRC-32, a zip archive of them is built in the browser, and the archive is downloaded and can be read back with the same checksums.
Uses @awacloud/fw (Apache-2.0). This example's code is Apache-2.0.
When to use it
Bundling what a user selected or generated into one download without a server round trip: exports, reports, attachments. The checksums let the recipient (or your own test) prove the archive holds exactly the picked bytes.
Copy-paste steps
npm install @awacloud/fw- Serve
node_modules/@awacloud/fw/src/at/vendor/fw/(node serve.jsfrom the examples root does this; any static server works). - Copy
index.htmlandmain.jsnext to each other and openindex.htmlthrough that server. The../../_style/examples.csslink is optional: copy that file along, or delete the link. - Drop the fixed
mtimeif you want the real file dates in the archive.
What it shows
How it fits together
- The archive comes from
fw/io-zip:zip.zipSyncwith a fixedmtime(two archives of the same files are byte-identical) andzip.unzipSyncto read it back. - The checksums come from
fw/io-binary:crc32per file, recomputed after the round trip. - The download comes from
fw/dom-storage:download.bytes(archive, 'files.zip', 'application/zip'). - The file input is the platform:
input.filesandFile.arrayBuffer().
Code
main.js
// SPDX-License-Identifier: Apache-2.0
import fw from '@awacloud/fw';
import { zip } from '@awacloud/fw/io/compress/zip.js';
import { crc32 } from '@awacloud/fw/io/calc/crc32.js';
import { download } from '@awacloud/fw/dom/fs/download.js';
fw.runtime.registerAllDeep([zip, crc32, download]);
const ZIP = fw.runtime.resolve('zip');
const Crc32 = fw.runtime.resolve('crc32');
const DOWNLOAD = fw.runtime.resolve('download');
/** @typedef {{ name: string, bytes: Uint8Array, crc: string }} Entry */
/** @param {Uint8Array} bytes @returns {string} CRC-32 as 8 hex digits */
export function checksum(bytes) {
const crc = new Crc32();
crc.append(bytes);
return crc.get().toString(16).padStart(8, '0');
}
/**
* Read the picked files into memory with their checksums.
* @param {Iterable<File>} files
* @returns {Promise<Entry[]>}
*/
export async function readFiles(files) {
const entries = [];
for (const file of files) {
const bytes = new Uint8Array(await file.arrayBuffer());
entries.push({ name: file.name, bytes, crc: checksum(bytes) });
}
return entries;
}
/**
* One archive of every entry, deflated, with a fixed timestamp so two
* archives of the same files are byte-identical.
* @param {Entry[]} entries
* @returns {Uint8Array}
*/
export function buildArchive(entries) {
const files = Object.fromEntries(entries.map((e) => [e.name, e.bytes]));
return ZIP.zipSync(files, { level: 6, mtime: new Date('2026-01-01T00:00:00Z') });
}
/**
* Read an archive back the way a recipient would, checksums recomputed.
* @param {Uint8Array} archive
* @returns {Entry[]}
*/
export function listArchive(archive) {
return Object.entries(ZIP.unzipSync(archive)).map(([name, bytes]) => ({ name, bytes, crc: checksum(bytes) }));
}
const input = /** @type {HTMLInputElement} */ (document.getElementById('files'));
const rows = /** @type {HTMLElement} */ (document.getElementById('rows'));
const button = /** @type {HTMLButtonElement} */ (document.getElementById('download'));
const status = /** @type {HTMLElement} */ (document.getElementById('status'));
/** @type {Entry[]} */
let picked = [];
input.addEventListener('change', async () => {
picked = await readFiles(input.files ?? []);
rows.replaceChildren(...picked.map((e) => {
const tr = document.createElement('tr');
for (const cell of [e.name, String(e.bytes.length), e.crc]) {
tr.appendChild(document.createElement('td')).textContent = cell;
}
return tr;
}));
button.disabled = picked.length === 0;
status.textContent = `${picked.length} file(s) ready`;
});
button.addEventListener('click', () => {
const archive = buildArchive(picked);
DOWNLOAD.bytes(archive, 'files.zip', 'application/zip');
status.textContent = `files.zip: ${archive.length} bytes for ${picked.length} file(s)`;
});index.html
<!doctype html>
<!-- SPDX-License-Identifier: Apache-2.0 -->
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>fw · Pick files, check them, download them as one zip</title>
<link rel="stylesheet" href="../../_style/examples.css">
<script type="importmap">
{ "imports": {
"@awacloud/fw": "/vendor/fw/main.js", "@awacloud/fw/": "/vendor/fw/" } }
</script>
<script type="module" src="./main.js"></script>
</head>
<body>
<main>
<h1>Pick files, check them, download them as one zip</h1>
<label for="files">Files</label>
<input id="files" type="file" multiple>
<table>
<caption>Picked files</caption>
<thead><tr><th scope="col">Name</th><th scope="col">Bytes</th><th scope="col">CRC-32</th></tr></thead>
<tbody id="rows"></tbody>
</table>
<p><button id="download" type="button" disabled>Download files.zip</button>
<span id="status" aria-live="polite"></span></p>
</main>
</body>
</html>