UNPKG

3.08 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 if (data.length != Constants.ENDHDR || data.readUInt32LE(0) != Constants.ENDSIG)
35 throw Utils.Errors.INVALID_END;
36
37 // number of entries on this volume
38 _volumeEntries = data.readUInt16LE(Constants.ENDSUB);
39 // total number of entries
40 _totalEntries = data.readUInt16LE(Constants.ENDTOT);
41 // central directory size in bytes
42 _size = data.readUInt32LE(Constants.ENDSIZ);
43 // offset of first CEN header
44 _offset = data.readUInt32LE(Constants.ENDOFF);
45 // zip file comment length
46 _commentLength = data.readUInt16LE(Constants.ENDCOM);
47 },
48
49 toBinary : function() {
50 var b = new Buffer(Constants.ENDHDR + _commentLength);
51 // "PK 05 06" signature
52 b.writeUInt32LE(Constants.ENDSIG, 0);
53 b.writeUInt32LE(0, 4);
54 // number of entries on this volume
55 b.writeUInt16LE(_volumeEntries, Constants.ENDSUB);
56 // total number of entries
57 b.writeUInt16LE(_totalEntries, Constants.ENDTOT);
58 // central directory size in bytes
59 b.writeUInt32LE(_size, Constants.ENDSIZ);
60 // offset of first CEN header
61 b.writeUInt32LE(_offset, Constants.ENDOFF);
62 // zip file comment length
63 b.writeUInt16LE(_commentLength, Constants.ENDCOM);
64 // fill comment memory with spaces so no garbage is left there
65 b.fill(" ", Constants.ENDHDR);
66
67 return b;
68 },
69
70 toString : function() {
71 return '{\n' +
72 '\t"diskEntries" : ' + _volumeEntries + ",\n" +
73 '\t"totalEntries" : ' + _totalEntries + ",\n" +
74 '\t"size" : ' + _size + " bytes,\n" +
75 '\t"offset" : 0x' + _offset.toString(16).toUpperCase() + ",\n" +
76 '\t"commentLength" : 0x' + _commentLength + "\n" +
77 '}';
78 }
79 }
80};
\No newline at end of file