1 | import yauzl from 'yauzl';
|
2 | import getStream from 'get-stream';
|
3 | export async function readFilesFromZip(filename) {
|
4 | const files = {};
|
5 | await new Promise((resolve, reject) => {
|
6 | const promises = [];
|
7 | yauzl.open(filename, {}, (err, zipfile) => {
|
8 | if (err || !zipfile)
|
9 | return reject(err);
|
10 | zipfile.on('entry', (entry) => {
|
11 | promises.push(new Promise((res, rej) => {
|
12 | if (entry.fileName.endsWith('/'))
|
13 | return;
|
14 | zipfile.openReadStream(entry, async (err, stream) => {
|
15 | if (err || !stream)
|
16 | return rej(err);
|
17 | files[`/${entry.fileName}`] = await getStream.buffer(stream);
|
18 | res();
|
19 | });
|
20 | }));
|
21 | });
|
22 | zipfile.once('end', () => Promise.all(promises).then(resolve, reject));
|
23 | });
|
24 | });
|
25 | return files;
|
26 | }
|
27 |
|
\ | No newline at end of file |