2018-04-02 21:46:55 +02:00
|
|
|
/**
|
|
|
|
* @author n1474335 [n1474335@gmail.com]
|
|
|
|
* @copyright Crown Copyright 2016
|
|
|
|
* @license Apache-2.0
|
|
|
|
*/
|
|
|
|
|
2019-07-09 13:23:59 +02:00
|
|
|
import Operation from "../Operation.mjs";
|
|
|
|
import {COMPRESSION_TYPE, ZLIB_COMPRESSION_TYPE_LOOKUP} from "../lib/Zlib.mjs";
|
2019-12-16 18:05:06 +01:00
|
|
|
import gzip from "zlibjs/bin/gzip.min.js";
|
2018-04-02 21:46:55 +02:00
|
|
|
|
2019-12-16 18:05:06 +01:00
|
|
|
const Zlib = gzip.Zlib;
|
2018-04-02 21:46:55 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gzip operation
|
|
|
|
*/
|
|
|
|
class Gzip extends Operation {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gzip constructor
|
|
|
|
*/
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
|
|
|
|
this.name = "Gzip";
|
|
|
|
this.module = "Compression";
|
|
|
|
this.description = "Compresses data using the deflate algorithm with gzip headers.";
|
2018-08-21 20:07:13 +02:00
|
|
|
this.infoURL = "https://wikipedia.org/wiki/Gzip";
|
2018-04-21 14:41:42 +02:00
|
|
|
this.inputType = "ArrayBuffer";
|
|
|
|
this.outputType = "ArrayBuffer";
|
2018-04-02 21:46:55 +02:00
|
|
|
this.args = [
|
|
|
|
{
|
|
|
|
name: "Compression type",
|
|
|
|
type: "option",
|
|
|
|
value: COMPRESSION_TYPE
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Filename (optional)",
|
|
|
|
type: "string",
|
|
|
|
value: ""
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Comment (optional)",
|
|
|
|
type: "string",
|
|
|
|
value: ""
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Include file checksum",
|
|
|
|
type: "boolean",
|
|
|
|
value: false
|
|
|
|
}
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-04-21 14:41:42 +02:00
|
|
|
* @param {ArrayBuffer} input
|
2018-04-02 21:46:55 +02:00
|
|
|
* @param {Object[]} args
|
2018-04-21 14:41:42 +02:00
|
|
|
* @returns {ArrayBuffer}
|
2018-04-02 21:46:55 +02:00
|
|
|
*/
|
|
|
|
run(input, args) {
|
|
|
|
const filename = args[1],
|
|
|
|
comment = args[2],
|
|
|
|
options = {
|
|
|
|
deflateOptions: {
|
|
|
|
compressionType: ZLIB_COMPRESSION_TYPE_LOOKUP[args[0]]
|
|
|
|
},
|
|
|
|
flags: {
|
|
|
|
fhcrc: args[3]
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if (filename.length) {
|
|
|
|
options.flags.fname = true;
|
|
|
|
options.filename = filename;
|
|
|
|
}
|
|
|
|
if (comment.length) {
|
2019-12-16 18:05:06 +01:00
|
|
|
options.flags.comment = true;
|
2018-04-02 21:46:55 +02:00
|
|
|
options.comment = comment;
|
|
|
|
}
|
2019-12-16 18:05:06 +01:00
|
|
|
const gzipObj = new Zlib.Gzip(new Uint8Array(input), options);
|
|
|
|
const compressed = new Uint8Array(gzipObj.compress());
|
2019-12-17 13:15:11 +01:00
|
|
|
if (options.flags.comment && !(compressed[3] & 0x10)) {
|
2019-12-16 18:05:06 +01:00
|
|
|
compressed[3] |= 0x10;
|
|
|
|
}
|
|
|
|
return compressed.buffer;
|
2018-04-02 21:46:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
export default Gzip;
|