UNPKG

4.06 kBJavaScriptView Raw
1var Utils = require("../util"),
2 Constants = Utils.Constants;
3
4/* The entries in the end of central directory */
5module.exports = function () {
6 var _volumeEntries = 0,
7 _totalEntries = 0,
8 _size = 0,
9 _offset = 0,
10 _commentLength = 0;
11
12 return {
13 get diskEntries () { return _volumeEntries },
14 set diskEntries (/*Number*/val) { _volumeEntries = _totalEntries = val; },
15
16 get totalEntries () { return _totalEntries },
17 set totalEntries (/*Number*/val) { _totalEntries = _volumeEntries = val; },
18
19 get size () { return _size },
20 set size (/*Number*/val) { _size = val; },
21
22 get offset () { return _offset },
23 set offset (/*Number*/val) { _offset = val; },
24
25 get commentLength () { return _commentLength },
26 set commentLength (/*Number*/val) { _commentLength = val; },
27
28 get mainHeaderSize () {
29 return Constants.ENDHDR + _commentLength;
30 },
31
32 loadFromBinary : function(/*Buffer*/data) {
33 // data should be 22 bytes and start with "PK 05 06"
34 // or be 56+ bytes and start with "PK 06 06" for Zip64
35 if ((data.length !== Constants.ENDHDR || data.readUInt32LE(0) !== Constants.ENDSIG) &&
36 (data.length < Constants.ZIP64HDR || data.readUInt32LE(0) !== Constants.ZIP64SIG)) {
37
38 throw new Error(Utils.Errors.INVALID_END);
39 }
40
41 if (data.readUInt32LE(0) === Constants.ENDSIG) {
42 // number of entries on this volume
43 _volumeEntries = data.readUInt16LE(Constants.ENDSUB);
44 // total number of entries
45 _totalEntries = data.readUInt16LE(Constants.ENDTOT);
46 // central directory size in bytes
47 _size = data.readUInt32LE(Constants.ENDSIZ);
48 // offset of first CEN header
49 _offset = data.readUInt32LE(Constants.ENDOFF);
50 // zip file comment length
51 _commentLength = data.readUInt16LE(Constants.ENDCOM);
52 } else {
53 // number of entries on this volume
54 _volumeEntries = Utils.readBigUInt64LE(data, Constants.ZIP64SUB);
55 // total number of entries
56 _totalEntries = Utils.readBigUInt64LE(data, Constants.ZIP64TOT);
57 // central directory size in bytes
58 _size = Utils.readBigUInt64LE(data, Constants.ZIP64SIZ);
59 // offset of first CEN header
60 _offset = Utils.readBigUInt64LE(data, Constants.ZIP64OFF);
61
62 _commentLength = 0;
63 }
64
65 },
66
67 toBinary : function() {
68 var b = Buffer.alloc(Constants.ENDHDR + _commentLength);
69 // "PK 05 06" signature
70 b.writeUInt32LE(Constants.ENDSIG, 0);
71 b.writeUInt32LE(0, 4);
72 // number of entries on this volume
73 b.writeUInt16LE(_volumeEntries, Constants.ENDSUB);
74 // total number of entries
75 b.writeUInt16LE(_totalEntries, Constants.ENDTOT);
76 // central directory size in bytes
77 b.writeUInt32LE(_size, Constants.ENDSIZ);
78 // offset of first CEN header
79 b.writeUInt32LE(_offset, Constants.ENDOFF);
80 // zip file comment length
81 b.writeUInt16LE(_commentLength, Constants.ENDCOM);
82 // fill comment memory with spaces so no garbage is left there
83 b.fill(" ", Constants.ENDHDR);
84
85 return b;
86 },
87
88 toString : function() {
89 return '{\n' +
90 '\t"diskEntries" : ' + _volumeEntries + ",\n" +
91 '\t"totalEntries" : ' + _totalEntries + ",\n" +
92 '\t"size" : ' + _size + " bytes,\n" +
93 '\t"offset" : 0x' + _offset.toString(16).toUpperCase() + ",\n" +
94 '\t"commentLength" : 0x' + _commentLength + "\n" +
95 '}';
96 }
97 }
98};
\No newline at end of file