|
20 | 20 | */ |
21 | 21 |
|
22 | 22 | import fs from "fs-extra"; |
23 | | - |
| 23 | +import path from "path"; |
24 | 24 | import * as compressing from "compressing"; |
25 | 25 | import child_process from "child_process"; |
| 26 | +import os from "os"; |
26 | 27 |
|
27 | 28 | // 跨平台的高效率/低效率结合的解压缩方案 |
| 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 | +} |
28 | 38 |
|
29 | 39 | async function nodeCompress(zipPath: string, files: string[], fileCode: string = "utf-8") { |
30 | 40 | const stream = new compressing.zip.Stream(); |
@@ -52,11 +62,11 @@ export async function compress(sourceZip: string, files: string[], fileCode?: st |
52 | 62 | } |
53 | 63 |
|
54 | 64 | 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 | + } |
60 | 70 | return await nodeDecompress(zipPath, dest, fileCode); |
61 | 71 | } |
62 | 72 |
|
@@ -92,4 +102,20 @@ async function _7zipDecompress(sourceZip: string, destDir: string) { |
92 | 102 | }); |
93 | 103 | } |
94 | 104 |
|
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