SLC-1.8.9/autobuilder/utils.js
2023-06-28 23:06:07 +06:00

38 lines
926 B
JavaScript

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,
};