新增 zip 压缩功能

This commit is contained in:
Suwings 2022-05-11 19:20:29 +08:00
parent a0423a1964
commit dc55d69a7e

View File

@ -52,12 +52,9 @@ async function nodeDecompress(sourceZip: string, destDir: string, fileCode: stri
}
export async function compress(sourceZip: string, files: string[], fileCode?: string) {
// TODO 与系统集成的解压缩功能
// if (system === "win32") {
// await _7zipCompress(sourceZip, files);
// } else {
// }
if (system === "linux") {
return await linuxZip(sourceZip, files);
}
return await nodeCompress(sourceZip, files, fileCode);
}
@ -119,3 +116,19 @@ async function linuxUnzip(sourceZip: string, destDir: string) {
});
});
}
// zip -r a.zip css css_v1 js
async function linuxZip(sourceZip: string, files: string[]) {
return new Promise((resolve, reject) => {
const p = ["-r", sourceZip];
p.concat(files);
const process = child_process.spawn("zip", p, {
cwd: path.normalize(path.dirname(sourceZip))
});
if (!process || !process.pid) return reject(false);
process.on("exit", (code) => {
if (code) return reject(false);
return resolve(true);
});
});
}