const JSZip = require("jszip"); const fs = require("fs"); const path = require("path"); async function unzip(file, dir) { const sep = process.platform === "darwin" ? "/" : "\\"; const fileContent = fs.readFileSync(file); const jszip = new JSZip(); const result = await jszip.loadAsync(fileContent); const keys = Object.keys(result.files); for (let key of keys) { const item = result.files[key]; if (item.dir) { if (!fs.existsSync(dir + item.name.replace("/", sep))) { await fs.promises.mkdir(dir + item.name.replace("/", sep), { recursive: true, }); } } else { let dirname = path.dirname(dir + item.name.replace("/", sep)); if (!fs.existsSync(dirname)) { await fs.promises.mkdir(dirname, { recursive: true }); } await fs.promises.writeFile( dir + item.name.replace("/", sep), Buffer.from(await item.async("arraybuffer")) ); } } } module.exports = { unzip, };