Skip to content
This repository was archived by the owner on Nov 13, 2024. It is now read-only.

Commit 0254820

Browse files
committed
新增 unzip 解压方案
1 parent 1d22c8b commit 0254820

File tree

1 file changed

+33
-7
lines changed

1 file changed

+33
-7
lines changed

src/common/compress.ts

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,21 @@
2020
*/
2121

2222
import fs from "fs-extra";
23-
23+
import path from "path";
2424
import * as compressing from "compressing";
2525
import child_process from "child_process";
26+
import os from "os";
2627

2728
// 跨平台的高效率/低效率结合的解压缩方案
29+
const system = os.platform();
30+
31+
function checkFileName(fileName: string) {
32+
const disableList = ['"', "/", "\\", "?", "|"];
33+
for (const iterator of disableList) {
34+
if (fileName.includes(iterator)) return false;
35+
}
36+
return true;
37+
}
2838

2939
async function nodeCompress(zipPath: string, files: string[], fileCode: string = "utf-8") {
3040
const stream = new compressing.zip.Stream();
@@ -52,11 +62,11 @@ export async function compress(sourceZip: string, files: string[], fileCode?: st
5262
}
5363

5464
export async function decompress(zipPath: string, dest: string, fileCode?: string) {
55-
// if (system === "win32") {
56-
// await _7zipDecompress(zipPath, dest);
57-
// } else {
58-
59-
// }
65+
if (system === "linux") {
66+
if (haveLinuxUnzip()) {
67+
return await linuxUnzip(zipPath, dest);
68+
}
69+
}
6070
return await nodeDecompress(zipPath, dest, fileCode);
6171
}
6272

@@ -92,4 +102,20 @@ async function _7zipDecompress(sourceZip: string, destDir: string) {
92102
});
93103
}
94104

95-
async function linuxUnzip(sourceZip: string, destDir: string) {}
105+
function haveLinuxUnzip() {
106+
const result = child_process.execSync("unzip -hh");
107+
return result?.toString("utf-8").toLowerCase().includes("extended help for unzip");
108+
}
109+
110+
async function linuxUnzip(sourceZip: string, destDir: string) {
111+
return new Promise((resolve, reject) => {
112+
const process = child_process.spawn("unzip", [sourceZip, "-d", destDir], {
113+
cwd: path.normalize(path.dirname(sourceZip))
114+
});
115+
if (!process || !process.pid) return reject(false);
116+
process.on("exit", (code) => {
117+
if (code) return reject(false);
118+
return resolve(true);
119+
});
120+
});
121+
}

0 commit comments

Comments
 (0)