parseExtraField.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. const parseBuffer = require('./parseBuffer');
  2. module.exports = function(extraField, vars) {
  3. let extra;
  4. // Find the ZIP64 header, if present.
  5. while(!extra && extraField && extraField.length) {
  6. const candidateExtra = parseBuffer.parse(extraField, [
  7. ['signature', 2],
  8. ['partSize', 2],
  9. ]);
  10. if(candidateExtra.signature === 0x0001) {
  11. // parse buffer based on data in ZIP64 central directory; order is important!
  12. const fieldsToExpect = [];
  13. if (vars.uncompressedSize === 0xffffffff) fieldsToExpect.push(['uncompressedSize', 8]);
  14. if (vars.compressedSize === 0xffffffff) fieldsToExpect.push(['compressedSize', 8]);
  15. if (vars.offsetToLocalFileHeader === 0xffffffff) fieldsToExpect.push(['offsetToLocalFileHeader', 8]);
  16. // slice off the 4 bytes for signature and partSize
  17. extra = parseBuffer.parse(extraField.slice(4), fieldsToExpect);
  18. } else {
  19. // Advance the buffer to the next part.
  20. // The total size of this part is the 4 byte header + partsize.
  21. extraField = extraField.slice(candidateExtra.partSize + 4);
  22. }
  23. }
  24. extra = extra || {};
  25. if (vars.compressedSize === 0xffffffff)
  26. vars.compressedSize = extra.compressedSize;
  27. if (vars.uncompressedSize === 0xffffffff)
  28. vars.uncompressedSize= extra.uncompressedSize;
  29. if (vars.offsetToLocalFileHeader === 0xffffffff)
  30. vars.offsetToLocalFileHeader = extra.offsetToLocalFileHeader;
  31. return extra;
  32. };