-
Notifications
You must be signed in to change notification settings - Fork 3.3k
/
Unzip.mjs
83 lines (73 loc) · 2 KB
/
Unzip.mjs
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
/**
* @author n1474335 [n1474335@gmail.com]
* @copyright Crown Copyright 2016
* @license Apache-2.0
*/
import Operation from "../Operation.mjs";
import Utils from "../Utils.mjs";
import unzip from "zlibjs/bin/unzip.min.js";
const Zlib = unzip.Zlib;
/**
* Unzip operation
*/
class Unzip extends Operation {
/**
* Unzip constructor
*/
constructor() {
super();
this.name = "Unzip";
this.module = "Compression";
this.description = "Decompresses data using the PKZIP algorithm and displays it per file, with support for passwords.";
this.infoURL = "https://wikipedia.org/wiki/Zip_(file_format)";
this.inputType = "ArrayBuffer";
this.outputType = "List<File>";
this.presentType = "html";
this.args = [
{
name: "Password",
type: "binaryString",
value: ""
},
{
name: "Verify result",
type: "boolean",
value: false
}
];
this.checks = [
{
pattern: "^\\x50\\x4b(?:\\x03|\\x05|\\x07)(?:\\x04|\\x06|\\x08)",
flags: "",
args: ["", false]
}
];
}
/**
* @param {ArrayBuffer} input
* @param {Object[]} args
* @returns {File[]}
*/
run(input, args) {
const options = {
password: Utils.strToByteArray(args[0]),
verify: args[1]
},
unzip = new Zlib.Unzip(new Uint8Array(input), options),
filenames = unzip.getFilenames();
return filenames.map(fileName => {
const bytes = unzip.decompress(fileName);
return new File([bytes], fileName);
});
}
/**
* Displays the files in HTML for web apps.
*
* @param {File[]} files
* @returns {html}
*/
async present(files) {
return await Utils.displayFilesAsHTML(files);
}
}
export default Unzip;