-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
Copy pathGzip.mjs
86 lines (76 loc) · 2.13 KB
/
Gzip.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
84
85
86
/**
* @author n1474335 [[email protected]]
* @copyright Crown Copyright 2016
* @license Apache-2.0
*/
import Operation from "../Operation.mjs";
import {COMPRESSION_TYPE, ZLIB_COMPRESSION_TYPE_LOOKUP} from "../lib/Zlib.mjs";
import zlibAndGzip from "zlibjs/bin/zlib_and_gzip.min.js";
const Zlib = zlibAndGzip.Zlib;
/**
* 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.";
this.infoURL = "https://wikipedia.org/wiki/Gzip";
this.inputType = "ArrayBuffer";
this.outputType = "ArrayBuffer";
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
}
];
}
/**
* @param {ArrayBuffer} input
* @param {Object[]} args
* @returns {ArrayBuffer}
*/
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) {
options.flags.fcommenct = true;
options.comment = comment;
}
const gzip = new Zlib.Gzip(new Uint8Array(input), options);
return new Uint8Array(gzip.compress()).buffer;
}
}
export default Gzip;