Building a ZIP File Tool That Runs Directly in Your Browser
A while ago I had to zip a folder of invoices. I opened a free online ZIP site, and then stopped. Why would I upload private files to a stranger's server just to put them in an archive? Your browser can do this job by i
A while ago I had to zip a folder of invoices. I opened a free online ZIP site, and then stopped. Why would I upload private files to a stranger's server just to put them in an archive?
Your browser can do this job by itself. It can read files from your computer, compress them and give you a new file to download. Here is how that works, and what can go wrong.
Pick a library
A ZIP file is a container, and the files inside are usually compressed with an algorithm called DEFLATE. You don't need to write that yourself. fflate is a small JavaScript library that does it well and works fine in the browser.
npm install fflate
Read the files and make the ZIP
Start with a normal file input that accepts many files:
<input type="file" id="picker" multiple>
Every selected file has an arrayBuffer() method. Turn that into a Uint8Array, store it under the file's name, and give the whole object to fflate:
import { zipSync } from 'fflate';
const picker = document.getElementById('picker');
picker.addEventListener('change', async () => {
const entries = {};
for (const file of picker.files) {
entries[file.name] = new Uint8Array(await file.arrayBuffer());
}
const zipped = zipSync(entries, { level: 6 });
download(zipped, 'archive.zip');
});
function download(bytes, name) {
const blob = new Blob([bytes], { type: 'application/zip' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = name;
a.click();
setTimeout(() => URL.revokeObjectURL(url), 1000);
}
Notice what is missing: there is no fetch and no upload. The files stay inside the browser tab the whole time.
Problems you will run into
Two files with the same name. An object can only have one key of each name, so two files called photo.jpg become one. Rename the second one to photo (1).jpg before you add it.
Big files. zipSync keeps everything in memory. A few hundred megabytes can crash a phone browser. Show the total size before zipping, or use fflate's streaming Zip class for large inputs.
Compression level. Level 6 is a good default. Photos, videos and most PDFs are already compressed, so a higher level wastes time and saves almost nothing. Text files, CSV files and code shrink much more. A "no compression" option (level 0) is useful for media folders.
Folders. If you use webkitdirectory on the input, each file gets a webkitRelativePath. Use that as the key and the folder structure is kept inside the ZIP.
Passwords. fflate can't make password-protected ZIPs, and old ZIP encryption is weak anyway. If your tool has no password option, say so clearly.
Check your privacy claim
If your page says "files never leave your device", test it. Open DevTools, go to the Network tab, zip a file and watch the requests. If any request carries your file data, the claim is wrong. I repeat this check after every change, because one analytics or error-logging script can break it without you noticing.
Try it
I built a free version at Painkillers.io: ZIP file creator. You can add single files or a whole folder, pick a compression level and download the result. You don't need an account. If you make your own, tell me how you handled very large files.
Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.