-
Notifications
You must be signed in to change notification settings - Fork 6
/
zip.ts
106 lines (81 loc) · 2.97 KB
/
zip.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
import { join } from "https://deno.land/std@0.92.0/path/mod.ts";
import { exists } from "https://deno.land/std@0.92.0/fs/mod.ts";
// Great code modified from: https://deno.land/x/zip@v1.1.1
// Modified because of an os.tmpDir error, version differences,
// and overwriting limitations in Deno.run unzip command
export async function unZipFromFile(
filePath: string,
destinationPath: string | null = "./",
options: any = {},
): Promise<string | false> {
if (!await exists(filePath)) {
console.error("this file does not found");
return false;
}
if (!destinationPath) {
destinationPath = "./";
}
const fullFileName = filePath.split("/");
const fileNameWithOutExt =
fullFileName[fullFileName.length - 1].split(".")[0];
const fullDestinationPath = options.includeFileName
? join(destinationPath, fileNameWithOutExt)
: destinationPath;
return await unzipProcess(filePath, fullDestinationPath)
? fullDestinationPath
: false;
}
async function unzipProcess(
zipSourcePath: string,
destinationPath: string,
): Promise<boolean> {
const process = Deno.run({
cmd: ["unzip", "-o", zipSourcePath, "-d", destinationPath],
stdout: "piped",
stderr: "piped",
});
const { success, code } = await process.status();
// Reading the outputs closes their pipes
const rawOutput = await process.output();
const rawError = await process.stderrOutput();
if (!success) {
const str = new TextDecoder().decode(rawError);
throw new Error(`$Command failed: code ${code}, message: ${str}`);
} else {
const str = new TextDecoder().decode(rawOutput);
}
Deno.close(process.rid) // close the process
return success
}
function getDirectory() {
if (Deno.build.os === "windows") {
return Deno.env.get("TMP") || Deno.env.get("TEMP") || Deno.env.get("USERPROFILE") || Deno.env.get("SystemRoot") || ""
}
return Deno.env.get("TMPDIR") || "/tmp"
}
export async function downloadFileToTemp(url: string): Promise<string> {
const response = await fetch(url)
const blob = await response.blob()
const arrayBufferFromBlobResponse = await blob.arrayBuffer()
const uint8ArrayEncodeFileData = new Uint8Array(arrayBufferFromBlobResponse)
const temporaryDirectory = Deno.realPathSync(getDirectory())
const tempFilePath = join(temporaryDirectory, "file.zip")
const file = await Deno.create(tempFilePath)
await Deno.writeAll(file, uint8ArrayEncodeFileData)
Deno.close(file.rid)
return tempFilePath
}
export async function unZipFromURL(
fileURL: string,
destinationPath: string | null = "./",
options: any = {},
): Promise<string | false>{
const downloadedFilePath = await downloadFileToTemp(fileURL)
const unZippingProcess = await unZipFromFile(
downloadedFilePath,
destinationPath,
options,
)
await Deno.remove(downloadedFilePath)
return unZippingProcess
}