antelle / node-stream-zip

node.js library for fast reading of large ZIPs
Other
447 stars 63 forks source link

recursive unpack of zip files? #101

Open pcace opened 1 year ago

pcace commented 1 year ago

Hi, i have the situation that i have zip in zip ... an cannot figure out how to unpack all of them recursively. maybe someone can point me to the problem here:

const recursiveUnZip = async (folder: string, zipfiles: string[]) => {
    return await Promise.allSettled(
        zipfiles.map(
            zipFile =>
                new Promise<string>((resolve, reject) => {
                    const resolveFunc = resolve
                    const rejectFunc = reject
                    const zip = new StreamZip({
                        storeEntries: true,
                        file: `${folder}/${zipFile.replace(/ /g, ' ')}`,
                    })
                    zip.on('error', function(err) {
                        console.error('[ERROR]', err)
                    })
                    zip.on('ready', function() {
                        console.log('All entries read: ' + zip.entriesCount)
                    })
                    zip.on('entry', function(entry) {
                        const pathname = path.resolve(`./${folder}`, entry.name)
                        if (/\.\./.test(path.relative(`./${folder}`, pathname))) {
                            console.warn(
                                '[zip warn]: ignoring maliciously crafted paths in zip file:',
                                entry.name
                            )
                            return
                        }
                        if ('/' === entry.name[entry.name.length - 1]) {
                            console.log('[DIR]', entry.name)
                            return
                        }

                        zip.extract(
                            entry,
                            `${folder}/${entry.name}`,
                            async (err?: string, res?: number | undefined) => {
                                console.log('DEBUG zip file entry name', entry.name)
                                if (path.parse(entry.name).ext.includes('zip')) {
                                    console.log('DEBUUG', path.parse(entry.name).base)
                                    await recursiveUnZip(folder, [path.parse(entry.name).base])
                                }
                                if (err) {
                                    rejectFunc(`error: ${err}`)
                                } else {
                                }
                            }
                        )
                    })
                    zip.close(() => resolveFunc('done'))
                })
        )
    )
}

any help would be great!