-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunrar.ts
248 lines (223 loc) · 6.43 KB
/
unrar.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
import { EventEmitter } from "./deps.ts";
import Writer from "./writer.ts";
// const reg_password = /^\r\nEnter password \(will not be echoed\)/;
const password_incorrect = "The specified password is incorrect";
const decoder = new TextDecoder();
/**
* Represents information about a file in a RAR archive.
*/
interface FileInfo {
[key: string]: string;
}
/**
* Options for the Unrar constructor.
*/
interface ConstructorOptions {
/**
* Path to the UnRAR executable.
*/
bin?: string;
/**
* Password for the RAR archive.
*/
password?: string;
}
/**
* Options for the uncompress method.
*/
interface UncompressOptions {
/**
* New name for the extracted file.
*/
newName?: string;
}
/**
* Class for handling RAR file operations such as listing contents and uncompressing files.
*/
export class Unrar extends EventEmitter {
private bin: string;
private args: string[];
private decoder = new TextDecoder();
/**
* The path to the RAR file.
*/
filepath: string;
/**
* The password for the RAR file.
*/
password: string;
/**
* Constructs an Unrar instance.
* @param filepath - The path to the RAR file.
* @param options - Optional settings for the Unrar instance.
*/
constructor(filepath: string, options: ConstructorOptions = {}) {
super();
this.filepath = filepath;
this.password = options.password || "123";
this.bin = options.bin || "./bin/UnRAR.exe";
const switches = ["-idc", "-v"];
switches.push(`-p${this.password}`);
this.args = ["vt", ...switches, this.filepath];
}
/**
* Lists the contents of the RAR file.
* @returns A promise that resolves to an array of FileInfo objects.
*/
async list(): Promise<FileInfo[]> {
const data = await this.getList();
const list = this.parse(data);
return list;
}
/**
* Retrieves the raw list of files from the RAR file.
* @returns A promise that resolves to the raw list data.
*/
private async getList(): Promise<string> {
const unrar = new Deno.Command(this.bin, {
args: this.args,
stdout: "piped",
stderr: "piped",
stdin: "null",
});
const { stdout, stderr } = await unrar.output();
if (stderr.length !== 0) {
const msg = decoder.decode(stderr);
if (msg.includes(password_incorrect)) {
throw new Error("Password protected file");
}
throw new Error(msg);
}
const result = decoder.decode(stdout);
// should get: reg_password, but get "Program aborted"
// if (reg_password.test(result)) {
// throw new Error('Password protected file');
// }
return result;
}
/**
* Uncompresses a file from the RAR archive to a destination directory.
* @param fileInfo - Information about the file to uncompress.
* @param destDir - The destination directory for the extracted file.
* @param options - Optional settings for the uncompression process.
* @returns A promise that resolves when the file is successfully uncompressed.
*/
async uncompress(
fileInfo: FileInfo,
destDir: string,
options: UncompressOptions = {},
): Promise<void> {
const command = "p";
const { size, name, type } = fileInfo;
if (type !== "File") {
throw new Error("Currently only supports a single file");
}
if (!size) throw new Error("can't get size");
const fileSize = +size;
if (Number.isNaN(fileSize)) throw new Error("unexpected size");
const { password, filepath } = this;
const filename = options.newName || name;
const destpath = `${destDir}/${filename}`;
const writer = new Writer(destpath);
await writer.setup();
const switches = [
"-n" + name,
"-idq",
];
if (password) {
switches.push(`-p${password}`);
}
const cmd = new Deno.Command(this.bin, {
args: [command, ...switches, filepath],
stdout: "piped",
stderr: "piped",
stdin: "null",
});
const unrar = cmd.spawn();
const reader = unrar.stdout.getReader();
const errorReader = unrar.stderr.getReader();
try {
let writed = 0;
while (true) {
const { value, done } = await reader.read();
if (done) break;
await writer.write(value);
writed += value.length;
this.emit("progress", this.getPercent(writed / fileSize));
}
// Read from stderr
let errMsg = "";
while (true) {
const { value, done } = await errorReader.read();
if (done) break;
errMsg += this.decoder.decode(value, { stream: true });
}
if (errMsg.length > 0) {
throw new Error(errMsg);
}
const status = await unrar.status;
if (!status.success) {
throw new Error(`code: ${status.code}`);
}
} finally {
reader.releaseLock();
writer.close();
}
}
/**
* Calculates the percentage of completion.
* @param ratio - The ratio of completed bytes to total bytes.
* @returns The percentage of completion as a string.
*/
private getPercent(ratio: number): string {
return (100 * ratio).toFixed(2) + "%";
}
/**
* Parses the raw list data into an array of FileInfo objects.
* @param stdout - The raw list data.
* @returns An array of FileInfo objects.
*/
private parse(stdout: string): FileInfo[] {
const list = stdout
.split(/\r?\n\r?\n/)
.filter((item) => item)
.map((item) => {
const obj: FileInfo = {};
item
.split(/\r?\n/)
.filter((item) => item)
.forEach((item) => {
const arr = item.split(": ");
const key = this.normalizeKey(arr[0]);
const val = arr[1].trim();
if (key) obj[key] = val;
});
return obj;
})
.filter((item) => item.name);
return list;
}
/**
* Normalizes a key to a consistent format.
* @param key - The key to normalize.
* @returns The normalized key.
*/
private normalizeKey(key: string): string | undefined {
const normKey = key.toLowerCase().replace(/^\s+/, "");
const keyMap = new Map<string, string>([
["name", "name"],
["type", "type"],
["size", "size"],
["packed size", "packedSize"],
["ratio", "ratio"],
["mtime", "mtime"],
["attributes", "attributes"],
["crc32", "crc32"],
["crc32 mac", "crc32Mac"],
["host os", "hostOS"],
["compression", "compression"],
["flags", "flags"],
]);
return keyMap.has(normKey) ? keyMap.get(normKey) : normKey;
}
}