zipEntries.js 12 KB

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