mainHeader.js 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. var Utils = require("../util"),
  2. Constants = Utils.Constants;
  3. /* The entries in the end of central directory */
  4. module.exports = function () {
  5. var _volumeEntries = 0,
  6. _totalEntries = 0,
  7. _size = 0,
  8. _offset = 0,
  9. _commentLength = 0;
  10. return {
  11. get diskEntries () { return _volumeEntries },
  12. set diskEntries (/*Number*/val) { _volumeEntries = _totalEntries = val; },
  13. get totalEntries () { return _totalEntries },
  14. set totalEntries (/*Number*/val) { _totalEntries = _volumeEntries = val; },
  15. get size () { return _size },
  16. set size (/*Number*/val) { _size = val; },
  17. get offset () { return _offset },
  18. set offset (/*Number*/val) { _offset = val; },
  19. get commentLength () { return _commentLength },
  20. set commentLength (/*Number*/val) { _commentLength = val; },
  21. get mainHeaderSize () {
  22. return Constants.ENDHDR + _commentLength;
  23. },
  24. loadFromBinary : function(/*Buffer*/data) {
  25. // data should be 22 bytes and start with "PK 05 06"
  26. if (data.length !== Constants.ENDHDR || data.readUInt32LE(0) !== Constants.ENDSIG)
  27. throw Utils.Errors.INVALID_END;
  28. // number of entries on this volume
  29. _volumeEntries = data.readUInt16LE(Constants.ENDSUB);
  30. // total number of entries
  31. _totalEntries = data.readUInt16LE(Constants.ENDTOT);
  32. // central directory size in bytes
  33. _size = data.readUInt32LE(Constants.ENDSIZ);
  34. // offset of first CEN header
  35. _offset = data.readUInt32LE(Constants.ENDOFF);
  36. // zip file comment length
  37. _commentLength = data.readUInt16LE(Constants.ENDCOM);
  38. },
  39. toBinary : function() {
  40. var b = Buffer.alloc(Constants.ENDHDR + _commentLength);
  41. // "PK 05 06" signature
  42. b.writeUInt32LE(Constants.ENDSIG, 0);
  43. b.writeUInt32LE(0, 4);
  44. // number of entries on this volume
  45. b.writeUInt16LE(_volumeEntries, Constants.ENDSUB);
  46. // total number of entries
  47. b.writeUInt16LE(_totalEntries, Constants.ENDTOT);
  48. // central directory size in bytes
  49. b.writeUInt32LE(_size, Constants.ENDSIZ);
  50. // offset of first CEN header
  51. b.writeUInt32LE(_offset, Constants.ENDOFF);
  52. // zip file comment length
  53. b.writeUInt16LE(_commentLength, Constants.ENDCOM);
  54. // fill comment memory with spaces so no garbage is left there
  55. b.fill(" ", Constants.ENDHDR);
  56. return b;
  57. },
  58. toString : function() {
  59. return '{\n' +
  60. '\t"diskEntries" : ' + _volumeEntries + ",\n" +
  61. '\t"totalEntries" : ' + _totalEntries + ",\n" +
  62. '\t"size" : ' + _size + " bytes,\n" +
  63. '\t"offset" : 0x' + _offset.toString(16).toUpperCase() + ",\n" +
  64. '\t"commentLength" : 0x' + _commentLength + "\n" +
  65. '}';
  66. }
  67. }
  68. };