2018-04-02 21:46:55 +02:00
|
|
|
/**
|
|
|
|
* @author n1474335 [n1474335@gmail.com]
|
|
|
|
* @copyright Crown Copyright 2016
|
|
|
|
* @license Apache-2.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
import Operation from "../Operation";
|
|
|
|
import Utils from "../Utils";
|
|
|
|
import unzip from "zlibjs/bin/unzip.min";
|
|
|
|
|
|
|
|
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.";
|
2018-04-21 14:41:42 +02:00
|
|
|
this.inputType = "ArrayBuffer";
|
2018-04-06 20:11:13 +02:00
|
|
|
this.outputType = "List<File>";
|
|
|
|
this.presentType = "html";
|
2018-04-02 21:46:55 +02:00
|
|
|
this.args = [
|
|
|
|
{
|
|
|
|
name: "Password",
|
|
|
|
type: "binaryString",
|
|
|
|
value: ""
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Verify result",
|
|
|
|
type: "boolean",
|
|
|
|
value: false
|
|
|
|
}
|
|
|
|
];
|
2018-05-20 17:49:42 +02:00
|
|
|
this.patterns = [
|
|
|
|
{
|
|
|
|
match: "^\\x50\\x4b(?:\\x03|\\x05|\\x07)(?:\\x04|\\x06|\\x08)",
|
|
|
|
flags: "",
|
|
|
|
args: ["", false]
|
|
|
|
},
|
|
|
|
];
|
2018-04-02 21:46:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-04-21 14:41:42 +02:00
|
|
|
* @param {ArrayBuffer} input
|
2018-04-02 21:46:55 +02:00
|
|
|
* @param {Object[]} args
|
2018-04-06 20:11:13 +02:00
|
|
|
* @returns {File[]}
|
2018-04-02 21:46:55 +02:00
|
|
|
*/
|
|
|
|
run(input, args) {
|
|
|
|
const options = {
|
|
|
|
password: Utils.strToByteArray(args[0]),
|
|
|
|
verify: args[1]
|
|
|
|
},
|
2018-04-21 14:41:42 +02:00
|
|
|
unzip = new Zlib.Unzip(new Uint8Array(input), options),
|
2018-04-06 20:11:13 +02:00
|
|
|
filenames = unzip.getFilenames();
|
2018-04-02 21:46:55 +02:00
|
|
|
|
2018-04-06 20:11:13 +02:00
|
|
|
return filenames.map(fileName => {
|
2018-04-02 21:46:55 +02:00
|
|
|
const bytes = unzip.decompress(fileName);
|
2018-04-06 20:11:13 +02:00
|
|
|
return new File([bytes], fileName);
|
2018-04-02 21:46:55 +02:00
|
|
|
});
|
2018-04-06 20:11:13 +02:00
|
|
|
}
|
2018-04-02 21:46:55 +02:00
|
|
|
|
2018-04-06 20:11:13 +02:00
|
|
|
/**
|
|
|
|
* Displays the files in HTML for web apps.
|
|
|
|
*
|
|
|
|
* @param {File[]} files
|
|
|
|
* @returns {html}
|
|
|
|
*/
|
|
|
|
async present(files) {
|
|
|
|
return await Utils.displayFilesAsHTML(files);
|
2018-04-02 21:46:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
export default Unzip;
|