forked from moncefplastin07/deno-zip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
decompress.ts
60 lines (58 loc) · 1.76 KB
/
decompress.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
import { exists, join } from "./deps.ts";
import { getFileNameFromPath } from "./utils.ts";
interface DecompressOptions {
overwrite?: boolean;
includeFileName?: boolean;
}
export const decompress = async (
filePath: string,
destinationPath: string | null = "./",
options?: DecompressOptions,
): Promise<string | false> => {
// check if the zip file is exist
if (!await exists(filePath)) {
throw "this file does not found";
}
// check destinationPath is not null and set './' as destinationPath
if (!destinationPath) {
destinationPath = "./";
}
// the file name with aut extension
const fileNameWithOutExt = getFileNameFromPath(filePath);
// get the extract file and add fileNameWithOutExt whene options.includeFileName is true
const fullDestinationPath = options?.includeFileName
? join(destinationPath, fileNameWithOutExt)
: destinationPath;
// return the unzipped file path or false whene the unzipping Process failed
return await decompressProcess(filePath, fullDestinationPath, options)
? fullDestinationPath
: false;
};
const decompressProcess = async (
zipSourcePath: string,
destinationPath: string,
options?: DecompressOptions,
): Promise<boolean> => {
const unzipCommandProcess = Deno.run({
cmd: Deno.build.os === "windows"
? [
"PowerShell",
"Expand-Archive",
"-Path",
`"${zipSourcePath}"`,
"-DestinationPath",
`"${destinationPath}"`,
options?.overwrite ? "-Force" : "",
]
: [
"unzip",
options?.overwrite ? "-o" : "",
zipSourcePath,
"-d",
destinationPath,
],
});
const processStatus = (await unzipCommandProcess.status()).success;
Deno.close(unzipCommandProcess.rid);
return processStatus;
};