Read zip files recursively with zip.js #265
Answered
by
gildas-lormeau
Venkatesh0625
asked this question in
Q&A
-
I have some zip inside the zip file which i want to read recursively |
Beta Was this translation helpful? Give feedback.
Answered by
gildas-lormeau
Jul 6, 2021
Replies: 1 comment 1 reply
-
You could use the function below which accepts a 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();
} Here is below an example of code showing how to use it. /* 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();
// get all the 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 run this code here: https://plnkr.co/edit/yhSnJFmsKyq6m4uH |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
Venkatesh0625
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You could use the function below which accepts a
ZipReader
instance as parameter and returns all the entries recursively.Here is below an example of code showing how to use it.