No Description

zipEntries.js 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. 'use strict';
  2. var readerFor = require('./reader/readerFor');
  3. var utils = require('./utils');
  4. var sig = require('./signature');
  5. var ZipEntry = require('./zipEntry');
  6. var utf8 = require('./utf8');
  7. var support = require('./support');
  8. // class ZipEntries {{{
  9. /**
  10. * All the entries in the zip file.
  11. * @constructor
  12. * @param {Object} loadOptions Options for loading the stream.
  13. */
  14. function ZipEntries(loadOptions) {
  15. this.files = [];
  16. this.loadOptions = loadOptions;
  17. }
  18. ZipEntries.prototype = {
  19. /**
  20. * Check that the reader is on the specified signature.
  21. * @param {string} expectedSignature the expected signature.
  22. * @throws {Error} if it is an other signature.
  23. */
  24. checkSignature: function(expectedSignature) {
  25. if (!this.reader.readAndCheckSignature(expectedSignature)) {
  26. this.reader.index -= 4;
  27. var signature = this.reader.readString(4);
  28. throw new Error("Corrupted zip or bug: unexpected signature " + "(" + utils.pretty(signature) + ", expected " + utils.pretty(expectedSignature) + ")");
  29. }
  30. },
  31. /**
  32. * Check if the given signature is at the given index.
  33. * @param {number} askedIndex the index to check.
  34. * @param {string} expectedSignature the signature to expect.
  35. * @return {boolean} true if the signature is here, false otherwise.
  36. */
  37. isSignature: function(askedIndex, expectedSignature) {
  38. var currentIndex = this.reader.index;
  39. this.reader.setIndex(askedIndex);
  40. var signature = this.reader.readString(4);
  41. var result = signature === expectedSignature;
  42. this.reader.setIndex(currentIndex);
  43. return result;
  44. },
  45. /**
  46. * Read the end of the central directory.
  47. */
  48. readBlockEndOfCentral: function() {
  49. this.diskNumber = this.reader.readInt(2);
  50. this.diskWithCentralDirStart = this.reader.readInt(2);
  51. this.centralDirRecordsOnThisDisk = this.reader.readInt(2);
  52. this.centralDirRecords = this.reader.readInt(2);
  53. this.centralDirSize = this.reader.readInt(4);
  54. this.centralDirOffset = this.reader.readInt(4);
  55. this.zipCommentLength = this.reader.readInt(2);
  56. // warning : the encoding depends of the system locale
  57. // On a linux machine with LANG=en_US.utf8, this field is utf8 encoded.
  58. // On a windows machine, this field is encoded with the localized windows code page.
  59. var zipComment = this.reader.readData(this.zipCommentLength);
  60. var decodeParamType = support.uint8array ? "uint8array" : "array";
  61. // To get consistent behavior with the generation part, we will assume that
  62. // this is utf8 encoded unless specified otherwise.
  63. var decodeContent = utils.transformTo(decodeParamType, zipComment);
  64. this.zipComment = this.loadOptions.decodeFileName(decodeContent);
  65. },
  66. /**
  67. * Read the end of the Zip 64 central directory.
  68. * Not merged with the method readEndOfCentral :
  69. * The end of central can coexist with its Zip64 brother,
  70. * I don't want to read the wrong number of bytes !
  71. */
  72. readBlockZip64EndOfCentral: function() {
  73. this.zip64EndOfCentralSize = this.reader.readInt(8);
  74. this.reader.skip(4);
  75. // this.versionMadeBy = this.reader.readString(2);
  76. // this.versionNeeded = this.reader.readInt(2);
  77. this.diskNumber = this.reader.readInt(4);
  78. this.diskWithCentralDirStart = this.reader.readInt(4);
  79. this.centralDirRecordsOnThisDisk = this.reader.readInt(8);
  80. this.centralDirRecords = this.reader.readInt(8);
  81. this.centralDirSize = this.reader.readInt(8);
  82. this.centralDirOffset = this.reader.readInt(8);
  83. this.zip64ExtensibleData = {};
  84. var extraDataSize = this.zip64EndOfCentralSize - 44,
  85. index = 0,
  86. extraFieldId,
  87. extraFieldLength,
  88. extraFieldValue;
  89. while (index < extraDataSize) {
  90. extraFieldId = this.reader.readInt(2);
  91. extraFieldLength = this.reader.readInt(4);
  92. extraFieldValue = this.reader.readData(extraFieldLength);
  93. this.zip64ExtensibleData[extraFieldId] = {
  94. id: extraFieldId,
  95. length: extraFieldLength,
  96. value: extraFieldValue
  97. };
  98. }
  99. },
  100. /**
  101. * Read the end of the Zip 64 central directory locator.
  102. */
  103. readBlockZip64EndOfCentralLocator: function() {
  104. this.diskWithZip64CentralDirStart = this.reader.readInt(4);
  105. this.relativeOffsetEndOfZip64CentralDir = this.reader.readInt(8);
  106. this.disksCount = this.reader.readInt(4);
  107. if (this.disksCount > 1) {
  108. throw new Error("Multi-volumes zip are not supported");
  109. }
  110. },
  111. /**
  112. * Read the local files, based on the offset read in the central part.
  113. */
  114. readLocalFiles: function() {
  115. var i, file;
  116. for (i = 0; i < this.files.length; i++) {
  117. file = this.files[i];
  118. this.reader.setIndex(file.localHeaderOffset);
  119. this.checkSignature(sig.LOCAL_FILE_HEADER);
  120. file.readLocalPart(this.reader);
  121. file.handleUTF8();
  122. file.processAttributes();
  123. }
  124. },
  125. /**
  126. * Read the central directory.
  127. */
  128. readCentralDir: function() {
  129. var file;
  130. this.reader.setIndex(this.centralDirOffset);
  131. while (this.reader.readAndCheckSignature(sig.CENTRAL_FILE_HEADER)) {
  132. file = new ZipEntry({
  133. zip64: this.zip64
  134. }, this.loadOptions);
  135. file.readCentralPart(this.reader);
  136. this.files.push(file);
  137. }
  138. if (this.centralDirRecords !== this.files.length) {
  139. if (this.centralDirRecords !== 0 && this.files.length === 0) {
  140. // We expected some records but couldn't find ANY.
  141. // This is really suspicious, as if something went wrong.
  142. throw new Error("Corrupted zip or bug: expected " + this.centralDirRecords + " records in central dir, got " + this.files.length);
  143. } else {
  144. // We found some records but not all.
  145. // Something is wrong but we got something for the user: no error here.
  146. // console.warn("expected", this.centralDirRecords, "records in central dir, got", this.files.length);
  147. }
  148. }
  149. },
  150. /**
  151. * Read the end of central directory.
  152. */
  153. readEndOfCentral: function() {
  154. var offset = this.reader.lastIndexOfSignature(sig.CENTRAL_DIRECTORY_END);
  155. if (offset < 0) {
  156. // Check if the content is a truncated zip or complete garbage.
  157. // A "LOCAL_FILE_HEADER" is not required at the beginning (auto
  158. // extractible zip for example) but it can give a good hint.
  159. // If an ajax request was used without responseType, we will also
  160. // get unreadable data.
  161. var isGarbage = !this.isSignature(0, sig.LOCAL_FILE_HEADER);
  162. if (isGarbage) {
  163. throw new Error("Can't find end of central directory : is this a zip file ? " +
  164. "If it is, see https://stuk.github.io/jszip/documentation/howto/read_zip.html");
  165. } else {
  166. throw new Error("Corrupted zip: can't find end of central directory");
  167. }
  168. }
  169. this.reader.setIndex(offset);
  170. var endOfCentralDirOffset = offset;
  171. this.checkSignature(sig.CENTRAL_DIRECTORY_END);
  172. this.readBlockEndOfCentral();
  173. /* extract from the zip spec :
  174. 4) If one of the fields in the end of central directory
  175. record is too small to hold required data, the field
  176. should be set to -1 (0xFFFF or 0xFFFFFFFF) and the
  177. ZIP64 format record should be created.
  178. 5) The end of central directory record and the
  179. Zip64 end of central directory locator record must
  180. reside on the same disk when splitting or spanning
  181. an archive.
  182. */
  183. if (this.diskNumber === utils.MAX_VALUE_16BITS || this.diskWithCentralDirStart === utils.MAX_VALUE_16BITS || this.centralDirRecordsOnThisDisk === utils.MAX_VALUE_16BITS || this.centralDirRecords === utils.MAX_VALUE_16BITS || this.centralDirSize === utils.MAX_VALUE_32BITS || this.centralDirOffset === utils.MAX_VALUE_32BITS) {
  184. this.zip64 = true;
  185. /*
  186. Warning : the zip64 extension is supported, but ONLY if the 64bits integer read from
  187. the zip file can fit into a 32bits integer. This cannot be solved : JavaScript represents
  188. all numbers as 64-bit double precision IEEE 754 floating point numbers.
  189. So, we have 53bits for integers and bitwise operations treat everything as 32bits.
  190. see https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Operators/Bitwise_Operators
  191. and http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-262.pdf section 8.5
  192. */
  193. // should look for a zip64 EOCD locator
  194. offset = this.reader.lastIndexOfSignature(sig.ZIP64_CENTRAL_DIRECTORY_LOCATOR);
  195. if (offset < 0) {
  196. throw new Error("Corrupted zip: can't find the ZIP64 end of central directory locator");
  197. }
  198. this.reader.setIndex(offset);
  199. this.checkSignature(sig.ZIP64_CENTRAL_DIRECTORY_LOCATOR);
  200. this.readBlockZip64EndOfCentralLocator();
  201. // now the zip64 EOCD record
  202. if (!this.isSignature(this.relativeOffsetEndOfZip64CentralDir, sig.ZIP64_CENTRAL_DIRECTORY_END)) {
  203. // console.warn("ZIP64 end of central directory not where expected.");
  204. this.relativeOffsetEndOfZip64CentralDir = this.reader.lastIndexOfSignature(sig.ZIP64_CENTRAL_DIRECTORY_END);
  205. if (this.relativeOffsetEndOfZip64CentralDir < 0) {
  206. throw new Error("Corrupted zip: can't find the ZIP64 end of central directory");
  207. }
  208. }
  209. this.reader.setIndex(this.relativeOffsetEndOfZip64CentralDir);
  210. this.checkSignature(sig.ZIP64_CENTRAL_DIRECTORY_END);
  211. this.readBlockZip64EndOfCentral();
  212. }
  213. var expectedEndOfCentralDirOffset = this.centralDirOffset + this.centralDirSize;
  214. if (this.zip64) {
  215. expectedEndOfCentralDirOffset += 20; // end of central dir 64 locator
  216. expectedEndOfCentralDirOffset += 12 /* should not include the leading 12 bytes */ + this.zip64EndOfCentralSize;
  217. }
  218. var extraBytes = endOfCentralDirOffset - expectedEndOfCentralDirOffset;
  219. if (extraBytes > 0) {
  220. // console.warn(extraBytes, "extra bytes at beginning or within zipfile");
  221. if (this.isSignature(endOfCentralDirOffset, sig.CENTRAL_FILE_HEADER)) {
  222. // The offsets seem wrong, but we have something at the specified offset.
  223. // So… we keep it.
  224. } else {
  225. // the offset is wrong, update the "zero" of the reader
  226. // this happens if data has been prepended (crx files for example)
  227. this.reader.zero = extraBytes;
  228. }
  229. } else if (extraBytes < 0) {
  230. throw new Error("Corrupted zip: missing " + Math.abs(extraBytes) + " bytes.");
  231. }
  232. },
  233. prepareReader: function(data) {
  234. this.reader = readerFor(data);
  235. },
  236. /**
  237. * Read a zip file and create ZipEntries.
  238. * @param {String|ArrayBuffer|Uint8Array|Buffer} data the binary string representing a zip file.
  239. */
  240. load: function(data) {
  241. this.prepareReader(data);
  242. this.readEndOfCentral();
  243. this.readCentralDir();
  244. this.readLocalFiles();
  245. }
  246. };
  247. // }}} end of ZipEntries
  248. module.exports = ZipEntries;