import yauzl from "yauzl" ; import path from "path" ; import fs from "fs" ; param zipPath param unzipToDir export const unzip = ( zipPath , unzipToDir ) => { return new Promise < void > ( ( resolve , reject ) => { try { fs . mkdirSync ( unzipToDir , { recursive : true } ) ; yauzl . open ( zipPath , { lazyEntries : true } , ( err , zipFile ) => { if ( ! zipFile ) return ; if ( err ) { zipFile . close ( ) ; reject ( err ) ; return ; } zipFile . readEntry ( ) ; zipFile . on ( "entry" , entry => { try { if ( /\/$/ . test ( entry . fileName ) ) { fs . mkdirSync ( path . join ( unzipToDir , entry . fileName ) , { recursive : true , } ) ; zipFile . readEntry ( ) ; } else { fs . mkdirSync ( path . dirname ( path . join ( unzipToDir , entry . fileName ) ) , { recursive : true , } ) ; zipFile . openReadStream ( entry , ( readErr , readStream ) => { if ( readErr || ! readStream ) { zipFile . close ( ) ; reject ( readErr || "Readstream didn't exist" ) ; return ; } const file = fs . createWriteStream ( path . join ( unzipToDir , entry . fileName ) ) ; readStream . pipe ( file ) ; file . on ( "finish" , ( ) => { file . close ( ( ) => { zipFile . readEntry ( ) ; } ) ; file . on ( "error" , err => { zipFile . close ( ) ; reject ( err ) ; } ) ; } ) ; } ) ; } } catch ( e ) { zipFile . close ( ) ; reject ( e ) ; } } ) ; zipFile . on ( "end" , err => { resolve ( ) ; } ) ; zipFile . on ( "error" , err => { zipFile . close ( ) ; reject ( err ) ; } ) ; } ) ; } catch ( e ) { reject ( e ) ; } } ) ; } ;