mirror of
https://github.com/refactorinqq/SLC-1.8.9.git
synced 2024-11-14 17:31:31 +01:00
38 lines
926 B
JavaScript
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,
|
|
};
|