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, pluggable compression engines, archives larger than 4GB
with Zip64, split zip files, data encryption, incremental writing, and
Deflate64 decompression.
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 "@zip.js/zip.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/tm9fhvab/
Hello world with the filesystem API
The filesystem API stores the entries in a tree instead of a flat list. It needs
a single import, it infers the Reader and the Writer from the type of the data,
and it reaches an entry from its name instead of searching it in an array.
import { ZipFS } from "@zip.js/zip.js";
const zipFs = new ZipFS();
zipFs.addText("hello.txt", "Hello world!");
zipFs.addText("folder/hello.txt", "Hello world from a directory!");
const zipFileBlob = await zipFs.exportBlob();
const importedZipFs = new ZipFS();
await importedZipFs.importBlob(zipFileBlob);
const helloWorldText = await importedZipFs.find("hello.txt").getText();
const nestedText = await importedZipFs.find("folder/hello.txt").getText();
console.log(helloWorldText, nestedText);
Run the code on JSFiddle: https://jsfiddle.net/gcnret0x/
Hello world with Streams
import {
BlobReader,
ZipReader,
ZipWriter
} from "@zip-js/zip-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/aw3d6f4o/
Adding concurrently multiple entries in a zip file
import {
BlobWriter,
HttpReader,
TextReader,
ZipWriter,
} from "@zip-js/zip-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
Custom web workers and compression engines
zip.js lets you create its web workers yourself by using the
new Worker(new URL("./zip-worker.js", import.meta.url)) form. Bundlers like
Vite, webpack and Rollup detect it and bundle the worker automatically. You can
also delegate compression and decompression to a custom engine, e.g.
fflate:
import { configure } from "@zip.js/zip.js/lib/zip-core-custom.js";
configure({
createWorker: () => new Worker(
new URL("./zip-worker.js", import.meta.url),
{ type: "module" }
)
});
import { initWorker } from "@zip.js/zip.js/worker";
import { CompressionStreamFallback, DecompressionStreamFallback } from "./fflate-streams.js";
initWorker({ CompressionStreamFallback, DecompressionStreamFallback });
See https://gildas-lormeau.github.io/zip.js/#custom-workers for a complete
guide.
Tests
See https://github.com/gildas-lormeau/zip.js/tree/master/tests/all
Reading untrusted zip files
The sizes declared in a zip file are not verified before the entry is read. A crafted entry can declare a large uncompressedSize, and valid deflate data can expand up to 1032 times its compressed size. Before extracting an entry from an untrusted source, check entry.uncompressedSize against the limit your application accepts, and avoid extracting all the entries concurrently. Uint8ArrayWriter allocates its buffer from the declared size, up to 1032 times entry.compressedSize, so this check matters most when the content is read into memory. Writers which deliver the content progressively, e.g. BlobWriter or a WritableStream, grow with the data actually produced.
评论