Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

recursive unpack of zip files? #101

Open
pcace opened this issue Mar 22, 2023 · 0 comments
Open

recursive unpack of zip files? #101

pcace opened this issue Mar 22, 2023 · 0 comments

Comments

@pcace
Copy link

pcace commented Mar 22, 2023

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant