Utilizing zip.js offers the possibility to utilize the function below, which takes a ZipReader
instance as input and retrieves all entries recursively.
async function getAllEntries(zipReader, options) {
let entries = await zipReader.getEntries(options);
entries = await Promise.all(entries.map(async entry => {
if (entry.filename.toLowerCase().endsWith(".zip")) {
const innerZipReader = new zip.ZipReader(new zip.BlobReader(await entry.getData(new zip.BlobWriter())));
return getAllEntries(innerZipReader, options);
} else {
return entry;
}
}));
return entries.flat();
}
Below is a sample code demonstrating how to use this function.
/* global zip, Blob */
"use strict";
const TEXT_CONTENT = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod...";
const TEXT_CONTENT_BLOB = new Blob([TEXT_CONTENT], { type: "text/plain" });
test().catch(error => console.error(error));
async function test() {
// creates a zip file containing 3 text files
let zipWriter = new zip.ZipWriter(new zip.BlobWriter("application/zip"));
await zipWriter.add("lorem1.txt", new zip.BlobReader(TEXT_CONTENT_BLOB));
await zipWriter.add("lorem2.txt", new zip.BlobReader(TEXT_CONTENT_BLOB));
await zipWriter.add("lorem3.txt", new zip.BlobReader(TEXT_CONTENT_BLOB));
const loremZip1 = await zipWriter.close();
// creates a zip file containing 2 text files
zipWriter = new zip.ZipWriter(new zip.BlobWriter("application/zip"));
await zipWriter.add("another_lorem1.txt", new zip.BlobReader(TEXT_CONTENT_BLOB));
await zipWriter.add("another_lorem2.txt", new zip.BlobReader(TEXT_CONTENT_BLOB));
const loremZip2 = await zipWriter.close();
// creates a zip file containing the first zip file and a text file
zipWriter = new zip.ZipWriter(new zip.BlobWriter("application/zip"));
await zipWriter.add("lorem1.zip", new zip.BlobReader(loremZip1));
await zipWriter.add("another_lorem3.txt", new zip.BlobReader(TEXT_CONTENT_BLOB));
const loremZip3 = await zipWriter.close();
// creates a zip file containing the 2 previous files and a text file
zipWriter = new zip.ZipWriter(new zip.BlobWriter("application/zip"));
await zipWriter.add("lorem3.zip", new zip.BlobReader(loremZip3));
await zipWriter.add("lorem2.zip", new zip.BlobReader(loremZip2));
await zipWriter.add("yet_another_lorem.txt", new zip.BlobReader(TEXT_CONTENT_BLOB));
const compressedFile = await zipWriter.close();
// retrieve all entries recursively
const entries = await getAllEntries(new zip.ZipReader(new zip.BlobReader(compressedFile)));
document.body.innerHTML = entries.map(entry => entry.filename).join("<br>");
}
async function getAllEntries(zipReader, options) {
let entries = await zipReader.getEntries(options);
entries = await Promise.all(entries.map(async entry => {
if (entry.filename.toLowerCase().endsWith(".zip")) {
const innerZipReader = new zip.ZipReader(new zip.BlobReader(await entry.getData(new zip.BlobWriter())));
return getAllEntries(innerZipReader, options);
} else {
return entry;
}
}));
return entries.flat();
}
You can execute this code here: https://plnkr.co/edit/yhSnJFmsKyq6m4uH