Feat: test unzip program

This commit is contained in:
unitwk 2022-10-21 19:40:53 +08:00
parent ec80dc63ba
commit 9a56fb80d5
5 changed files with 25 additions and 11 deletions

View File

@ -53,7 +53,7 @@ function archiveZip(zipPath: string, files: string[], fileCode: string = "utf-8"
});
}
function archiveUnZip(sourceZip: string, destDir: string, fileCode: string = "utf-8") {
function archiveUnZip(sourceZip: string, destDir: string, fileCode: string = "utf-8"): Promise<boolean> {
return new Promise(async (resolve, reject) => {
const zip = new StreamZip.async({ file: sourceZip, nameEncoding: fileCode });
if (!fs.existsSync(destDir)) fs.mkdirsSync(destDir);
@ -76,7 +76,7 @@ export async function compress(sourceZip: string, files: string[], fileCode?: st
return await archiveZip(sourceZip, files, fileCode);
}
export async function decompress(zipPath: string, dest: string, fileCode?: string) {
export async function decompress(zipPath: string, dest: string, fileCode?: string): Promise<boolean> {
// if (system === "linux" && haveLinuxUnzip()) return await linuxUnzip(zipPath, dest);
// return await nodeDecompress(zipPath, dest, fileCode);
if (canGolangProcess()) return await golangProcessUnzip(zipPath, dest, fileCode);

View File

@ -240,5 +240,9 @@
"version": {
"versionDetectErr": "版本检查失败"
},
"quick_install": {
"unzipError": "解压文件失败"
}
}

View File

@ -1,4 +1,5 @@
import EventEmitter from "events";
import logger from "../log";
export interface IAsyncTaskJSON {
[key: string]: any;
@ -23,11 +24,11 @@ export class TaskCenter {
}
public static onTaskStopped(t: IAsyncTask) {
console.log("Task:", t.taskId, "Stopped");
logger.info("Async Task:", t.taskId, "Stopped.");
}
public static onTaskFailure(t: IAsyncTask) {
console.log("Task:", t.taskId, "Failed");
logger.info("Async Task:", t.taskId, "Failed.");
}
public static getTask(taskId: string) {

View File

@ -5,7 +5,7 @@ import fs from "fs-extra";
import Instance from "../../entity/instance/instance";
import InstanceSubsystem from "../system_instance";
import InstanceConfig from "../../entity/instance/Instance_config";
import { $t } from "../../i18n";
import { $t, i18next } from "../../i18n";
import path from "path";
import { getFileManager } from "../file_router_service";
import EventEmitter from "events";
@ -15,7 +15,7 @@ export class QuickInstallTask extends EventEmitter implements IAsyncTask {
private _status = 0; // 0=stop 1=running -1=error 2=downloading
public taskId: string;
private instance: Instance;
private readonly TMP_ZIP_NAME = "tmp.zip";
private readonly TMP_ZIP_NAME = "__tmp__.zip";
private zipPath = "";
private downloadStream: fs.WriteStream = null;
@ -30,7 +30,7 @@ export class QuickInstallTask extends EventEmitter implements IAsyncTask {
this.taskId = `QuickInstallTask-${this.instance.instanceUuid}-${v4()}`;
}
private download() {
private download(): Promise<boolean> {
return new Promise(async (resolve, reject) => {
this.zipPath = path.normalize(path.join(this.instance.config.cwd, this.TMP_ZIP_NAME));
const writeStream = fs.createWriteStream(this.zipPath);
@ -51,14 +51,18 @@ export class QuickInstallTask extends EventEmitter implements IAsyncTask {
async start() {
this._status = 1;
this.emit("started");
const fileManager = getFileManager(this.instance.instanceUuid);
try {
await this.download();
const fileManager = getFileManager(this.instance.instanceUuid);
let result = await this.download();
result = await fileManager.promiseUnzip(this.TMP_ZIP_NAME, ".", "UTF-8");
if (!result) throw new Error($t("quick_install.unzipError"));
console.log("OK!!!!");
this.stop();
} catch (error) {
console.log("Task error:", error);
this.emit("failure");
} finally {
fs.remove(fileManager.toAbsolutePath(this.TMP_ZIP_NAME), () => {});
}
}
@ -97,5 +101,3 @@ export function createQuickInstallTask(targetLink: string, instanceName: string)
TaskCenter.addTask(task);
return task;
}
createQuickInstallTask("http://oss.duzuii.com/d/MCSManager/MCSManager_v9.6.0_win_x64.zip", "23333");

View File

@ -156,6 +156,13 @@ export default class FileManager {
.catch(() => {});
}
async promiseUnzip(sourceZip: string, destDir: string, code?: string): Promise<boolean> {
if (!code) code = this.fileCode;
if (!this.check(sourceZip) || !this.checkPath(destDir)) throw new Error(ERROR_MSG_01);
this.zipFileCheck(this.toAbsolutePath(sourceZip));
return await decompress(this.toAbsolutePath(sourceZip), this.toAbsolutePath(destDir), code);
}
zip(sourceZip: string, files: string[], code?: string) {
if (!code) code = this.fileCode;
if (!this.checkPath(sourceZip)) throw new Error(ERROR_MSG_01);