Build and read zip archives
Build a zip in the browser, and list the entries of a .zip the user picks.
Apache-2.0Runs on this page
Package: @awacloud/fw
Build a zip archive from in-memory text and list what it holds; open a
.zip the user picks and list its entries, or say why it is not one.
Uses @awacloud/fw (Apache-2.0). This example's code is Apache-2.0.
When to use it
You want to bundle generated files into one archive, or look inside a .zip
the user picked, entirely in the browser. A fixed mtime makes the output
reproducible (same files, same bytes), which matters for caching and for
tests.
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. - To offer the archive as a download, add
downloadas therecipes/zip-and-downloadrecipe does.
What it shows
zip.zipSync({ 'name': bytes, ... }, { level, mtime })for the archive; entry names with/become folders. The archive is built on a click, not on every keystroke.zip.unzipSync(bytes)giving{ name: Uint8Array }to list and read, on the bytes of a picked file (file.arrayBuffer()); nothing is uploaded.- A picked file that is not an archive throws: the page catches it and says so instead of listing nothing silently.
registerAllDeep([zip])bringsdeflateandcrc32along.
Code
main.js
// SPDX-License-Identifier: Apache-2.0
import fw from '@awacloud/fw';
import { zip } from '@awacloud/fw/io/compress/zip.js';
// zip brings deflate and crc32 along.
fw.runtime.registerAllDeep([zip]);
const ZIP = fw.runtime.resolve('zip');
const encoder = new TextEncoder();
/** A fixed timestamp makes two archives of the same files byte-identical. */
export const MTIME = new Date('2026-01-01T00:00:00Z');
/**
* @param {Record<string, string>} texts entry name → text content
* @returns {Uint8Array} the archive bytes
*/
export function buildArchive(texts) {
const files = Object.fromEntries(Object.entries(texts).map(([name, text]) => [name, encoder.encode(text)]));
return ZIP.zipSync(files, { level: 6, mtime: MTIME });
}
/**
* @param {Uint8Array} archive
* @returns {{ name: string, size: number }[]}
* @throws {Error} when the bytes are not a zip archive
*/
export function listEntries(archive) {
return Object.entries(ZIP.unzipSync(archive)).map(([name, data]) => ({ name, size: data.length }));
}
/** @param {string} id */
const byId = (id) => /** @type {HTMLElement} */ (document.getElementById(id));
/** @param {string} id @param {{ name: string, size: number }[]} entries */
function showEntries(id, entries) {
byId(id).replaceChildren(...entries.map(({ name, size }) => {
const tr = document.createElement('tr');
for (const cell of [name, String(size)]) tr.appendChild(document.createElement('td')).textContent = cell;
return tr;
}));
}
function build() {
const note = /** @type {HTMLTextAreaElement} */ (byId('note')).value;
const archive = buildArchive({
'README.txt': 'Two files, one archive, built in the browser.\n',
'notes/today.txt': note,
});
const entries = listEntries(archive);
byId('build-status').textContent = `${archive.length} bytes, ${entries.length} entries`;
showEntries('built', entries);
}
async function open() {
const file = /** @type {HTMLInputElement} */ (byId('pick')).files?.[0];
if (!file) return;
byId('pick-status').textContent = 'reading…';
try {
const entries = listEntries(new Uint8Array(await file.arrayBuffer()));
byId('pick-status').textContent = `${file.name}: ${entries.length} entries`;
showEntries('picked', entries);
} catch (err) {
byId('pick-status').textContent = `${file.name} is not a zip archive: ${/** @type {Error} */ (err).message}`;
showEntries('picked', []);
}
}
byId('build').addEventListener('click', build);
byId('pick').addEventListener('change', open);
build();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 · Build and read zip archives</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>Build and read zip archives</h1>
<h2>Build</h2>
<label for="note">Contents of <code>notes/today.txt</code></label>
<textarea id="note" rows="3">Ship the examples.</textarea>
<button id="build" type="button">Build the archive</button>
<p id="build-status" aria-live="polite"></p>
<table>
<caption>Entries read back from the archive</caption>
<thead><tr><th scope="col">Name</th><th scope="col">Bytes</th></tr></thead>
<tbody id="built"></tbody>
</table>
<h2>Read</h2>
<label for="pick">Open a <code>.zip</code> file (it stays in the page)</label>
<input id="pick" type="file" accept=".zip,application/zip">
<p id="pick-status" aria-live="polite"></p>
<table>
<caption>Entries of the picked archive</caption>
<thead><tr><th scope="col">Name</th><th scope="col">Bytes</th></tr></thead>
<tbody id="picked"></tbody>
</table>
</main>
</body>
</html>