Introduction
zip.js is a JavaScript open-source library (BSD-3-Clause license) for
compressing and decompressing zip files. It has been designed to handle large amounts
of data. It supports notably multi-core compression, native compression with
compression streams, archives larger than 4GB with Zip64, split zip files and data
encryption.
Demo
See https://gildas-lormeau.github.io/zip-manager
Documentation
See here for more info: https://gildas-lormeau.github.io/zip.js/
Examples
Hello world
import {
BlobReader,
BlobWriter,
TextReader,
TextWriter,
ZipReader,
ZipWriter,
} from "https://deno.land/x/zipjs/index.js";
const zipFileWriter = new BlobWriter();
const helloWorldReader = new TextReader("Hello world!");
const zipWriter = new ZipWriter(zipFileWriter);
await zipWriter.add("hello.txt", helloWorldReader);
await zipWriter.close();
const zipFileBlob = await zipFileWriter.getData();
const zipFileReader = new BlobReader(zipFileBlob);
const helloWorldWriter = new TextWriter();
const zipReader = new ZipReader(zipFileReader);
const firstEntry = (await zipReader.getEntries()).shift();
const helloWorldText = await firstEntry.getData(helloWorldWriter);
await zipReader.close();
console.log(helloWorldText);
Run the code on JSFiddle: https://jsfiddle.net/dns7pkxt/
Hello world with Streams
import {
BlobReader,
ZipReader,
ZipWriter,
} from "https://deno.land/x/zipjs/index.js";
const zipFileStream = new TransformStream();
const zipFileBlobPromise = new Response(zipFileStream.readable).blob();
const helloWorldReadable = new Blob(["Hello world!"]).stream();
const zipWriter = new ZipWriter(zipFileStream.writable);
await zipWriter.add("hello.txt", helloWorldReadable);
await zipWriter.close();
const zipFileBlob = await zipFileBlobPromise;
const zipFileReader = new BlobReader(zipFileBlob);
const helloWorldStream = new TransformStream();
const helloWorldTextPromise = new Response(helloWorldStream.readable).text();
const zipReader = new ZipReader(zipFileReader);
const firstEntry = (await zipReader.getEntries()).shift();
await firstEntry.getData(helloWorldStream.writable);
await zipReader.close();
const helloWorldText = await helloWorldTextPromise;
console.log(helloWorldText);
Run the code on JSFiddle: https://jsfiddle.net/exnyq1ft/
Adding concurrently multiple entries in a zip file
import {
BlobWriter,
HttpReader,
TextReader,
ZipWriter,
} from "https://unpkg.com/@zip.js/zip.js/index.js";
const README_URL = "https://unpkg.com/@zip.js/zip.js/README.md";
getZipFileBlob()
.then(downloadFile);
async function getZipFileBlob() {
const zipWriter = new ZipWriter(new BlobWriter("application/zip"));
await Promise.all([
zipWriter.add("hello.txt", new TextReader("Hello world!")),
zipWriter.add("README.md", new HttpReader(README_URL)),
]);
return zipWriter.close();
}
function downloadFile(blob) {
document.body.appendChild(Object.assign(document.createElement("a"), {
download: "hello.zip",
href: URL.createObjectURL(blob),
textContent: "Download zip file",
}));
}
Run the code on Plunker: https://plnkr.co/edit/4sVljNIpqSUE9HCA?preview
Tests
See https://github.com/gildas-lormeau/zip.js/tree/master/tests/all