import fs from "fs-extra" ; import path from "path" ; import unzipper from "unzipper" ; function getFiles ( directory ) { return fs . readdirSync ( path . join ( directory ) , { withFileTypes : true , } ) ; } description export param { string } directory param { boolean } removeFiles returns { Promise < string > } export function extractFile ( file , removeFiles = true ) { return new Promise ( ( resolve , reject ) => { const filepath = path . join ( path . dirname ( file ) , path . basename ( file , ".zip" ) ) ; fs . createReadStream ( file ) . pipe ( unzipper . Extract ( { path : filepath , } ) . on ( "error" , ( ) => reject ( `Failed to extract ${ file }, probably not a zip file` ) ) . on ( "close" , ( ) => { if ( removeFiles ) { fs . removeSync ( file ) ; } resolve ( filepath ) ; } ) ) ; } ) ; } description export param { string } directory param { boolean } removeFiles returns { Promise < string [ ] > } export function extractFiles ( directory , removeFiles = true ) { const files = getFiles ( directory ) ; const promises = [ ] ; files . filter ( ( file ) => file . isFile ( ) && path . extname ( file . name ) === ".zip" ) . forEach ( ( file ) => { promises . push ( extractFile ( path . join ( path . resolve ( directory ) , file . name ) , removeFiles ) ) ; } ) ; return Promise . all ( promises ) ; }