node_stream_zip.js 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210
  1. /**
  2. * @license node-stream-zip | (c) 2020 Antelle | https://github.com/antelle/node-stream-zip/blob/master/LICENSE
  3. * Portions copyright https://github.com/cthackers/adm-zip | https://raw.githubusercontent.com/cthackers/adm-zip/master/LICENSE
  4. */
  5. let fs = require('fs');
  6. const util = require('util');
  7. const path = require('path');
  8. const events = require('events');
  9. const zlib = require('zlib');
  10. const stream = require('stream');
  11. const consts = {
  12. /* The local file header */
  13. LOCHDR: 30, // LOC header size
  14. LOCSIG: 0x04034b50, // "PK\003\004"
  15. LOCVER: 4, // version needed to extract
  16. LOCFLG: 6, // general purpose bit flag
  17. LOCHOW: 8, // compression method
  18. LOCTIM: 10, // modification time (2 bytes time, 2 bytes date)
  19. LOCCRC: 14, // uncompressed file crc-32 value
  20. LOCSIZ: 18, // compressed size
  21. LOCLEN: 22, // uncompressed size
  22. LOCNAM: 26, // filename length
  23. LOCEXT: 28, // extra field length
  24. /* The Data descriptor */
  25. EXTSIG: 0x08074b50, // "PK\007\008"
  26. EXTHDR: 16, // EXT header size
  27. EXTCRC: 4, // uncompressed file crc-32 value
  28. EXTSIZ: 8, // compressed size
  29. EXTLEN: 12, // uncompressed size
  30. /* The central directory file header */
  31. CENHDR: 46, // CEN header size
  32. CENSIG: 0x02014b50, // "PK\001\002"
  33. CENVEM: 4, // version made by
  34. CENVER: 6, // version needed to extract
  35. CENFLG: 8, // encrypt, decrypt flags
  36. CENHOW: 10, // compression method
  37. CENTIM: 12, // modification time (2 bytes time, 2 bytes date)
  38. CENCRC: 16, // uncompressed file crc-32 value
  39. CENSIZ: 20, // compressed size
  40. CENLEN: 24, // uncompressed size
  41. CENNAM: 28, // filename length
  42. CENEXT: 30, // extra field length
  43. CENCOM: 32, // file comment length
  44. CENDSK: 34, // volume number start
  45. CENATT: 36, // internal file attributes
  46. CENATX: 38, // external file attributes (host system dependent)
  47. CENOFF: 42, // LOC header offset
  48. /* The entries in the end of central directory */
  49. ENDHDR: 22, // END header size
  50. ENDSIG: 0x06054b50, // "PK\005\006"
  51. ENDSIGFIRST: 0x50,
  52. ENDSUB: 8, // number of entries on this disk
  53. ENDTOT: 10, // total number of entries
  54. ENDSIZ: 12, // central directory size in bytes
  55. ENDOFF: 16, // offset of first CEN header
  56. ENDCOM: 20, // zip file comment length
  57. MAXFILECOMMENT: 0xffff,
  58. /* The entries in the end of ZIP64 central directory locator */
  59. ENDL64HDR: 20, // ZIP64 end of central directory locator header size
  60. ENDL64SIG: 0x07064b50, // ZIP64 end of central directory locator signature
  61. ENDL64SIGFIRST: 0x50,
  62. ENDL64OFS: 8, // ZIP64 end of central directory offset
  63. /* The entries in the end of ZIP64 central directory */
  64. END64HDR: 56, // ZIP64 end of central directory header size
  65. END64SIG: 0x06064b50, // ZIP64 end of central directory signature
  66. END64SIGFIRST: 0x50,
  67. END64SUB: 24, // number of entries on this disk
  68. END64TOT: 32, // total number of entries
  69. END64SIZ: 40,
  70. END64OFF: 48,
  71. /* Compression methods */
  72. STORED: 0, // no compression
  73. SHRUNK: 1, // shrunk
  74. REDUCED1: 2, // reduced with compression factor 1
  75. REDUCED2: 3, // reduced with compression factor 2
  76. REDUCED3: 4, // reduced with compression factor 3
  77. REDUCED4: 5, // reduced with compression factor 4
  78. IMPLODED: 6, // imploded
  79. // 7 reserved
  80. DEFLATED: 8, // deflated
  81. ENHANCED_DEFLATED: 9, // deflate64
  82. PKWARE: 10, // PKWare DCL imploded
  83. // 11 reserved
  84. BZIP2: 12, // compressed using BZIP2
  85. // 13 reserved
  86. LZMA: 14, // LZMA
  87. // 15-17 reserved
  88. IBM_TERSE: 18, // compressed using IBM TERSE
  89. IBM_LZ77: 19, //IBM LZ77 z
  90. /* General purpose bit flag */
  91. FLG_ENC: 0, // encrypted file
  92. FLG_COMP1: 1, // compression option
  93. FLG_COMP2: 2, // compression option
  94. FLG_DESC: 4, // data descriptor
  95. FLG_ENH: 8, // enhanced deflation
  96. FLG_STR: 16, // strong encryption
  97. FLG_LNG: 1024, // language encoding
  98. FLG_MSK: 4096, // mask header values
  99. FLG_ENTRY_ENC: 1,
  100. /* 4.5 Extensible data fields */
  101. EF_ID: 0,
  102. EF_SIZE: 2,
  103. /* Header IDs */
  104. ID_ZIP64: 0x0001,
  105. ID_AVINFO: 0x0007,
  106. ID_PFS: 0x0008,
  107. ID_OS2: 0x0009,
  108. ID_NTFS: 0x000a,
  109. ID_OPENVMS: 0x000c,
  110. ID_UNIX: 0x000d,
  111. ID_FORK: 0x000e,
  112. ID_PATCH: 0x000f,
  113. ID_X509_PKCS7: 0x0014,
  114. ID_X509_CERTID_F: 0x0015,
  115. ID_X509_CERTID_C: 0x0016,
  116. ID_STRONGENC: 0x0017,
  117. ID_RECORD_MGT: 0x0018,
  118. ID_X509_PKCS7_RL: 0x0019,
  119. ID_IBM1: 0x0065,
  120. ID_IBM2: 0x0066,
  121. ID_POSZIP: 0x4690,
  122. EF_ZIP64_OR_32: 0xffffffff,
  123. EF_ZIP64_OR_16: 0xffff,
  124. };
  125. const StreamZip = function (config) {
  126. let fd, fileSize, chunkSize, op, centralDirectory, closed;
  127. const ready = false,
  128. that = this,
  129. entries = config.storeEntries !== false ? {} : null,
  130. fileName = config.file,
  131. textDecoder = config.nameEncoding ? new TextDecoder(config.nameEncoding) : null;
  132. open();
  133. function open() {
  134. if (config.fd) {
  135. fd = config.fd;
  136. readFile();
  137. } else {
  138. fs.open(fileName, 'r', (err, f) => {
  139. if (err) {
  140. return that.emit('error', err);
  141. }
  142. fd = f;
  143. readFile();
  144. });
  145. }
  146. }
  147. function readFile() {
  148. fs.fstat(fd, (err, stat) => {
  149. if (err) {
  150. return that.emit('error', err);
  151. }
  152. fileSize = stat.size;
  153. chunkSize = config.chunkSize || Math.round(fileSize / 1000);
  154. chunkSize = Math.max(
  155. Math.min(chunkSize, Math.min(128 * 1024, fileSize)),
  156. Math.min(1024, fileSize)
  157. );
  158. readCentralDirectory();
  159. });
  160. }
  161. function readUntilFoundCallback(err, bytesRead) {
  162. if (err || !bytesRead) {
  163. return that.emit('error', err || new Error('Archive read error'));
  164. }
  165. let pos = op.lastPos;
  166. let bufferPosition = pos - op.win.position;
  167. const buffer = op.win.buffer;
  168. const minPos = op.minPos;
  169. while (--pos >= minPos && --bufferPosition >= 0) {
  170. if (buffer.length - bufferPosition >= 4 && buffer[bufferPosition] === op.firstByte) {
  171. // quick check first signature byte
  172. if (buffer.readUInt32LE(bufferPosition) === op.sig) {
  173. op.lastBufferPosition = bufferPosition;
  174. op.lastBytesRead = bytesRead;
  175. op.complete();
  176. return;
  177. }
  178. }
  179. }
  180. if (pos === minPos) {
  181. return that.emit('error', new Error('Bad archive'));
  182. }
  183. op.lastPos = pos + 1;
  184. op.chunkSize *= 2;
  185. if (pos <= minPos) {
  186. return that.emit('error', new Error('Bad archive'));
  187. }
  188. const expandLength = Math.min(op.chunkSize, pos - minPos);
  189. op.win.expandLeft(expandLength, readUntilFoundCallback);
  190. }
  191. function readCentralDirectory() {
  192. const totalReadLength = Math.min(consts.ENDHDR + consts.MAXFILECOMMENT, fileSize);
  193. op = {
  194. win: new FileWindowBuffer(fd),
  195. totalReadLength,
  196. minPos: fileSize - totalReadLength,
  197. lastPos: fileSize,
  198. chunkSize: Math.min(1024, chunkSize),
  199. firstByte: consts.ENDSIGFIRST,
  200. sig: consts.ENDSIG,
  201. complete: readCentralDirectoryComplete,
  202. };
  203. op.win.read(fileSize - op.chunkSize, op.chunkSize, readUntilFoundCallback);
  204. }
  205. function readCentralDirectoryComplete() {
  206. const buffer = op.win.buffer;
  207. const pos = op.lastBufferPosition;
  208. try {
  209. centralDirectory = new CentralDirectoryHeader();
  210. centralDirectory.read(buffer.slice(pos, pos + consts.ENDHDR));
  211. centralDirectory.headerOffset = op.win.position + pos;
  212. if (centralDirectory.commentLength) {
  213. that.comment = buffer
  214. .slice(
  215. pos + consts.ENDHDR,
  216. pos + consts.ENDHDR + centralDirectory.commentLength
  217. )
  218. .toString();
  219. } else {
  220. that.comment = null;
  221. }
  222. that.entriesCount = centralDirectory.volumeEntries;
  223. that.centralDirectory = centralDirectory;
  224. if (
  225. (centralDirectory.volumeEntries === consts.EF_ZIP64_OR_16 &&
  226. centralDirectory.totalEntries === consts.EF_ZIP64_OR_16) ||
  227. centralDirectory.size === consts.EF_ZIP64_OR_32 ||
  228. centralDirectory.offset === consts.EF_ZIP64_OR_32
  229. ) {
  230. readZip64CentralDirectoryLocator();
  231. } else {
  232. op = {};
  233. readEntries();
  234. }
  235. } catch (err) {
  236. that.emit('error', err);
  237. }
  238. }
  239. function readZip64CentralDirectoryLocator() {
  240. const length = consts.ENDL64HDR;
  241. if (op.lastBufferPosition > length) {
  242. op.lastBufferPosition -= length;
  243. readZip64CentralDirectoryLocatorComplete();
  244. } else {
  245. op = {
  246. win: op.win,
  247. totalReadLength: length,
  248. minPos: op.win.position - length,
  249. lastPos: op.win.position,
  250. chunkSize: op.chunkSize,
  251. firstByte: consts.ENDL64SIGFIRST,
  252. sig: consts.ENDL64SIG,
  253. complete: readZip64CentralDirectoryLocatorComplete,
  254. };
  255. op.win.read(op.lastPos - op.chunkSize, op.chunkSize, readUntilFoundCallback);
  256. }
  257. }
  258. function readZip64CentralDirectoryLocatorComplete() {
  259. const buffer = op.win.buffer;
  260. const locHeader = new CentralDirectoryLoc64Header();
  261. locHeader.read(
  262. buffer.slice(op.lastBufferPosition, op.lastBufferPosition + consts.ENDL64HDR)
  263. );
  264. const readLength = fileSize - locHeader.headerOffset;
  265. op = {
  266. win: op.win,
  267. totalReadLength: readLength,
  268. minPos: locHeader.headerOffset,
  269. lastPos: op.lastPos,
  270. chunkSize: op.chunkSize,
  271. firstByte: consts.END64SIGFIRST,
  272. sig: consts.END64SIG,
  273. complete: readZip64CentralDirectoryComplete,
  274. };
  275. op.win.read(fileSize - op.chunkSize, op.chunkSize, readUntilFoundCallback);
  276. }
  277. function readZip64CentralDirectoryComplete() {
  278. const buffer = op.win.buffer;
  279. const zip64cd = new CentralDirectoryZip64Header();
  280. zip64cd.read(buffer.slice(op.lastBufferPosition, op.lastBufferPosition + consts.END64HDR));
  281. that.centralDirectory.volumeEntries = zip64cd.volumeEntries;
  282. that.centralDirectory.totalEntries = zip64cd.totalEntries;
  283. that.centralDirectory.size = zip64cd.size;
  284. that.centralDirectory.offset = zip64cd.offset;
  285. that.entriesCount = zip64cd.volumeEntries;
  286. op = {};
  287. readEntries();
  288. }
  289. function readEntries() {
  290. op = {
  291. win: new FileWindowBuffer(fd),
  292. pos: centralDirectory.offset,
  293. chunkSize,
  294. entriesLeft: centralDirectory.volumeEntries,
  295. };
  296. op.win.read(op.pos, Math.min(chunkSize, fileSize - op.pos), readEntriesCallback);
  297. }
  298. function readEntriesCallback(err, bytesRead) {
  299. if (err || !bytesRead) {
  300. return that.emit('error', err || new Error('Entries read error'));
  301. }
  302. let bufferPos = op.pos - op.win.position;
  303. let entry = op.entry;
  304. const buffer = op.win.buffer;
  305. const bufferLength = buffer.length;
  306. try {
  307. while (op.entriesLeft > 0) {
  308. if (!entry) {
  309. entry = new ZipEntry();
  310. entry.readHeader(buffer, bufferPos);
  311. entry.headerOffset = op.win.position + bufferPos;
  312. op.entry = entry;
  313. op.pos += consts.CENHDR;
  314. bufferPos += consts.CENHDR;
  315. }
  316. const entryHeaderSize = entry.fnameLen + entry.extraLen + entry.comLen;
  317. const advanceBytes = entryHeaderSize + (op.entriesLeft > 1 ? consts.CENHDR : 0);
  318. if (bufferLength - bufferPos < advanceBytes) {
  319. op.win.moveRight(chunkSize, readEntriesCallback, bufferPos);
  320. op.move = true;
  321. return;
  322. }
  323. entry.read(buffer, bufferPos, textDecoder);
  324. if (!config.skipEntryNameValidation) {
  325. entry.validateName();
  326. }
  327. if (entries) {
  328. entries[entry.name] = entry;
  329. }
  330. that.emit('entry', entry);
  331. op.entry = entry = null;
  332. op.entriesLeft--;
  333. op.pos += entryHeaderSize;
  334. bufferPos += entryHeaderSize;
  335. }
  336. that.emit('ready');
  337. } catch (err) {
  338. that.emit('error', err);
  339. }
  340. }
  341. function checkEntriesExist() {
  342. if (!entries) {
  343. throw new Error('storeEntries disabled');
  344. }
  345. }
  346. Object.defineProperty(this, 'ready', {
  347. get() {
  348. return ready;
  349. },
  350. });
  351. this.entry = function (name) {
  352. checkEntriesExist();
  353. return entries[name];
  354. };
  355. this.entries = function () {
  356. checkEntriesExist();
  357. return entries;
  358. };
  359. this.stream = function (entry, callback) {
  360. return this.openEntry(
  361. entry,
  362. (err, entry) => {
  363. if (err) {
  364. return callback(err);
  365. }
  366. const offset = dataOffset(entry);
  367. let entryStream = new EntryDataReaderStream(fd, offset, entry.compressedSize);
  368. if (entry.method === consts.STORED) {
  369. // nothing to do
  370. } else if (entry.method === consts.DEFLATED) {
  371. entryStream = entryStream.pipe(zlib.createInflateRaw());
  372. } else {
  373. return callback(new Error('Unknown compression method: ' + entry.method));
  374. }
  375. if (canVerifyCrc(entry)) {
  376. entryStream = entryStream.pipe(
  377. new EntryVerifyStream(entryStream, entry.crc, entry.size)
  378. );
  379. }
  380. callback(null, entryStream);
  381. },
  382. false
  383. );
  384. };
  385. this.entryDataSync = function (entry) {
  386. let err = null;
  387. this.openEntry(
  388. entry,
  389. (e, en) => {
  390. err = e;
  391. entry = en;
  392. },
  393. true
  394. );
  395. if (err) {
  396. throw err;
  397. }
  398. let data = Buffer.alloc(entry.compressedSize);
  399. new FsRead(fd, data, 0, entry.compressedSize, dataOffset(entry), (e) => {
  400. err = e;
  401. }).read(true);
  402. if (err) {
  403. throw err;
  404. }
  405. if (entry.method === consts.STORED) {
  406. // nothing to do
  407. } else if (entry.method === consts.DEFLATED || entry.method === consts.ENHANCED_DEFLATED) {
  408. data = zlib.inflateRawSync(data);
  409. } else {
  410. throw new Error('Unknown compression method: ' + entry.method);
  411. }
  412. if (data.length !== entry.size) {
  413. throw new Error('Invalid size');
  414. }
  415. if (canVerifyCrc(entry)) {
  416. const verify = new CrcVerify(entry.crc, entry.size);
  417. verify.data(data);
  418. }
  419. return data;
  420. };
  421. this.openEntry = function (entry, callback, sync) {
  422. if (typeof entry === 'string') {
  423. checkEntriesExist();
  424. entry = entries[entry];
  425. if (!entry) {
  426. return callback(new Error('Entry not found'));
  427. }
  428. }
  429. if (!entry.isFile) {
  430. return callback(new Error('Entry is not file'));
  431. }
  432. if (!fd) {
  433. return callback(new Error('Archive closed'));
  434. }
  435. const buffer = Buffer.alloc(consts.LOCHDR);
  436. new FsRead(fd, buffer, 0, buffer.length, entry.offset, (err) => {
  437. if (err) {
  438. return callback(err);
  439. }
  440. let readEx;
  441. try {
  442. entry.readDataHeader(buffer);
  443. if (entry.encrypted) {
  444. readEx = new Error('Entry encrypted');
  445. }
  446. } catch (ex) {
  447. readEx = ex;
  448. }
  449. callback(readEx, entry);
  450. }).read(sync);
  451. };
  452. function dataOffset(entry) {
  453. return entry.offset + consts.LOCHDR + entry.fnameLen + entry.extraLen;
  454. }
  455. function canVerifyCrc(entry) {
  456. // if bit 3 (0x08) of the general-purpose flags field is set, then the CRC-32 and file sizes are not known when the header is written
  457. return (entry.flags & 0x8) !== 0x8;
  458. }
  459. function extract(entry, outPath, callback) {
  460. that.stream(entry, (err, stm) => {
  461. if (err) {
  462. callback(err);
  463. } else {
  464. let fsStm, errThrown;
  465. stm.on('error', (err) => {
  466. errThrown = err;
  467. if (fsStm) {
  468. stm.unpipe(fsStm);
  469. fsStm.close(() => {
  470. callback(err);
  471. });
  472. }
  473. });
  474. fs.open(outPath, 'w', (err, fdFile) => {
  475. if (err) {
  476. return callback(err);
  477. }
  478. if (errThrown) {
  479. fs.close(fd, () => {
  480. callback(errThrown);
  481. });
  482. return;
  483. }
  484. fsStm = fs.createWriteStream(outPath, { fd: fdFile });
  485. fsStm.on('finish', () => {
  486. that.emit('extract', entry, outPath);
  487. if (!errThrown) {
  488. callback();
  489. }
  490. });
  491. stm.pipe(fsStm);
  492. });
  493. }
  494. });
  495. }
  496. function createDirectories(baseDir, dirs, callback) {
  497. if (!dirs.length) {
  498. return callback();
  499. }
  500. let dir = dirs.shift();
  501. dir = path.join(baseDir, path.join(...dir));
  502. fs.mkdir(dir, { recursive: true }, (err) => {
  503. if (err && err.code !== 'EEXIST') {
  504. return callback(err);
  505. }
  506. createDirectories(baseDir, dirs, callback);
  507. });
  508. }
  509. function extractFiles(baseDir, baseRelPath, files, callback, extractedCount) {
  510. if (!files.length) {
  511. return callback(null, extractedCount);
  512. }
  513. const file = files.shift();
  514. const targetPath = path.join(baseDir, file.name.replace(baseRelPath, ''));
  515. extract(file, targetPath, (err) => {
  516. if (err) {
  517. return callback(err, extractedCount);
  518. }
  519. extractFiles(baseDir, baseRelPath, files, callback, extractedCount + 1);
  520. });
  521. }
  522. this.extract = function (entry, outPath, callback) {
  523. let entryName = entry || '';
  524. if (typeof entry === 'string') {
  525. entry = this.entry(entry);
  526. if (entry) {
  527. entryName = entry.name;
  528. } else {
  529. if (entryName.length && entryName[entryName.length - 1] !== '/') {
  530. entryName += '/';
  531. }
  532. }
  533. }
  534. if (!entry || entry.isDirectory) {
  535. const files = [],
  536. dirs = [],
  537. allDirs = {};
  538. for (const e in entries) {
  539. if (
  540. Object.prototype.hasOwnProperty.call(entries, e) &&
  541. e.lastIndexOf(entryName, 0) === 0
  542. ) {
  543. let relPath = e.replace(entryName, '');
  544. const childEntry = entries[e];
  545. if (childEntry.isFile) {
  546. files.push(childEntry);
  547. relPath = path.dirname(relPath);
  548. }
  549. if (relPath && !allDirs[relPath] && relPath !== '.') {
  550. allDirs[relPath] = true;
  551. let parts = relPath.split('/').filter((f) => {
  552. return f;
  553. });
  554. if (parts.length) {
  555. dirs.push(parts);
  556. }
  557. while (parts.length > 1) {
  558. parts = parts.slice(0, parts.length - 1);
  559. const partsPath = parts.join('/');
  560. if (allDirs[partsPath] || partsPath === '.') {
  561. break;
  562. }
  563. allDirs[partsPath] = true;
  564. dirs.push(parts);
  565. }
  566. }
  567. }
  568. }
  569. dirs.sort((x, y) => {
  570. return x.length - y.length;
  571. });
  572. if (dirs.length) {
  573. createDirectories(outPath, dirs, (err) => {
  574. if (err) {
  575. callback(err);
  576. } else {
  577. extractFiles(outPath, entryName, files, callback, 0);
  578. }
  579. });
  580. } else {
  581. extractFiles(outPath, entryName, files, callback, 0);
  582. }
  583. } else {
  584. fs.stat(outPath, (err, stat) => {
  585. if (stat && stat.isDirectory()) {
  586. extract(entry, path.join(outPath, path.basename(entry.name)), callback);
  587. } else {
  588. extract(entry, outPath, callback);
  589. }
  590. });
  591. }
  592. };
  593. this.close = function (callback) {
  594. if (closed || !fd) {
  595. closed = true;
  596. if (callback) {
  597. callback();
  598. }
  599. } else {
  600. closed = true;
  601. fs.close(fd, (err) => {
  602. fd = null;
  603. if (callback) {
  604. callback(err);
  605. }
  606. });
  607. }
  608. };
  609. const originalEmit = events.EventEmitter.prototype.emit;
  610. this.emit = function (...args) {
  611. if (!closed) {
  612. return originalEmit.call(this, ...args);
  613. }
  614. };
  615. };
  616. StreamZip.setFs = function (customFs) {
  617. fs = customFs;
  618. };
  619. StreamZip.debugLog = (...args) => {
  620. if (StreamZip.debug) {
  621. // eslint-disable-next-line no-console
  622. console.log(...args);
  623. }
  624. };
  625. util.inherits(StreamZip, events.EventEmitter);
  626. const propZip = Symbol('zip');
  627. StreamZip.async = class StreamZipAsync extends events.EventEmitter {
  628. constructor(config) {
  629. super();
  630. const zip = new StreamZip(config);
  631. zip.on('entry', (entry) => this.emit('entry', entry));
  632. zip.on('extract', (entry, outPath) => this.emit('extract', entry, outPath));
  633. this[propZip] = new Promise((resolve, reject) => {
  634. zip.on('ready', () => {
  635. zip.removeListener('error', reject);
  636. resolve(zip);
  637. });
  638. zip.on('error', reject);
  639. });
  640. }
  641. get entriesCount() {
  642. return this[propZip].then((zip) => zip.entriesCount);
  643. }
  644. get comment() {
  645. return this[propZip].then((zip) => zip.comment);
  646. }
  647. async entry(name) {
  648. const zip = await this[propZip];
  649. return zip.entry(name);
  650. }
  651. async entries() {
  652. const zip = await this[propZip];
  653. return zip.entries();
  654. }
  655. async stream(entry) {
  656. const zip = await this[propZip];
  657. return new Promise((resolve, reject) => {
  658. zip.stream(entry, (err, stm) => {
  659. if (err) {
  660. reject(err);
  661. } else {
  662. resolve(stm);
  663. }
  664. });
  665. });
  666. }
  667. async entryData(entry) {
  668. const stm = await this.stream(entry);
  669. return new Promise((resolve, reject) => {
  670. const data = [];
  671. stm.on('data', (chunk) => data.push(chunk));
  672. stm.on('end', () => {
  673. resolve(Buffer.concat(data));
  674. });
  675. stm.on('error', (err) => {
  676. stm.removeAllListeners('end');
  677. reject(err);
  678. });
  679. });
  680. }
  681. async extract(entry, outPath) {
  682. const zip = await this[propZip];
  683. return new Promise((resolve, reject) => {
  684. zip.extract(entry, outPath, (err, res) => {
  685. if (err) {
  686. reject(err);
  687. } else {
  688. resolve(res);
  689. }
  690. });
  691. });
  692. }
  693. async close() {
  694. const zip = await this[propZip];
  695. return new Promise((resolve, reject) => {
  696. zip.close((err) => {
  697. if (err) {
  698. reject(err);
  699. } else {
  700. resolve();
  701. }
  702. });
  703. });
  704. }
  705. };
  706. class CentralDirectoryHeader {
  707. read(data) {
  708. if (data.length !== consts.ENDHDR || data.readUInt32LE(0) !== consts.ENDSIG) {
  709. throw new Error('Invalid central directory');
  710. }
  711. // number of entries on this volume
  712. this.volumeEntries = data.readUInt16LE(consts.ENDSUB);
  713. // total number of entries
  714. this.totalEntries = data.readUInt16LE(consts.ENDTOT);
  715. // central directory size in bytes
  716. this.size = data.readUInt32LE(consts.ENDSIZ);
  717. // offset of first CEN header
  718. this.offset = data.readUInt32LE(consts.ENDOFF);
  719. // zip file comment length
  720. this.commentLength = data.readUInt16LE(consts.ENDCOM);
  721. }
  722. }
  723. class CentralDirectoryLoc64Header {
  724. read(data) {
  725. if (data.length !== consts.ENDL64HDR || data.readUInt32LE(0) !== consts.ENDL64SIG) {
  726. throw new Error('Invalid zip64 central directory locator');
  727. }
  728. // ZIP64 EOCD header offset
  729. this.headerOffset = readUInt64LE(data, consts.ENDSUB);
  730. }
  731. }
  732. class CentralDirectoryZip64Header {
  733. read(data) {
  734. if (data.length !== consts.END64HDR || data.readUInt32LE(0) !== consts.END64SIG) {
  735. throw new Error('Invalid central directory');
  736. }
  737. // number of entries on this volume
  738. this.volumeEntries = readUInt64LE(data, consts.END64SUB);
  739. // total number of entries
  740. this.totalEntries = readUInt64LE(data, consts.END64TOT);
  741. // central directory size in bytes
  742. this.size = readUInt64LE(data, consts.END64SIZ);
  743. // offset of first CEN header
  744. this.offset = readUInt64LE(data, consts.END64OFF);
  745. }
  746. }
  747. class ZipEntry {
  748. readHeader(data, offset) {
  749. // data should be 46 bytes and start with "PK 01 02"
  750. if (data.length < offset + consts.CENHDR || data.readUInt32LE(offset) !== consts.CENSIG) {
  751. throw new Error('Invalid entry header');
  752. }
  753. // version made by
  754. this.verMade = data.readUInt16LE(offset + consts.CENVEM);
  755. // version needed to extract
  756. this.version = data.readUInt16LE(offset + consts.CENVER);
  757. // encrypt, decrypt flags
  758. this.flags = data.readUInt16LE(offset + consts.CENFLG);
  759. // compression method
  760. this.method = data.readUInt16LE(offset + consts.CENHOW);
  761. // modification time (2 bytes time, 2 bytes date)
  762. const timebytes = data.readUInt16LE(offset + consts.CENTIM);
  763. const datebytes = data.readUInt16LE(offset + consts.CENTIM + 2);
  764. this.time = parseZipTime(timebytes, datebytes);
  765. // uncompressed file crc-32 value
  766. this.crc = data.readUInt32LE(offset + consts.CENCRC);
  767. // compressed size
  768. this.compressedSize = data.readUInt32LE(offset + consts.CENSIZ);
  769. // uncompressed size
  770. this.size = data.readUInt32LE(offset + consts.CENLEN);
  771. // filename length
  772. this.fnameLen = data.readUInt16LE(offset + consts.CENNAM);
  773. // extra field length
  774. this.extraLen = data.readUInt16LE(offset + consts.CENEXT);
  775. // file comment length
  776. this.comLen = data.readUInt16LE(offset + consts.CENCOM);
  777. // volume number start
  778. this.diskStart = data.readUInt16LE(offset + consts.CENDSK);
  779. // internal file attributes
  780. this.inattr = data.readUInt16LE(offset + consts.CENATT);
  781. // external file attributes
  782. this.attr = data.readUInt32LE(offset + consts.CENATX);
  783. // LOC header offset
  784. this.offset = data.readUInt32LE(offset + consts.CENOFF);
  785. }
  786. readDataHeader(data) {
  787. // 30 bytes and should start with "PK\003\004"
  788. if (data.readUInt32LE(0) !== consts.LOCSIG) {
  789. throw new Error('Invalid local header');
  790. }
  791. // version needed to extract
  792. this.version = data.readUInt16LE(consts.LOCVER);
  793. // general purpose bit flag
  794. this.flags = data.readUInt16LE(consts.LOCFLG);
  795. // compression method
  796. this.method = data.readUInt16LE(consts.LOCHOW);
  797. // modification time (2 bytes time ; 2 bytes date)
  798. const timebytes = data.readUInt16LE(consts.LOCTIM);
  799. const datebytes = data.readUInt16LE(consts.LOCTIM + 2);
  800. this.time = parseZipTime(timebytes, datebytes);
  801. // uncompressed file crc-32 value
  802. this.crc = data.readUInt32LE(consts.LOCCRC) || this.crc;
  803. // compressed size
  804. const compressedSize = data.readUInt32LE(consts.LOCSIZ);
  805. if (compressedSize && compressedSize !== consts.EF_ZIP64_OR_32) {
  806. this.compressedSize = compressedSize;
  807. }
  808. // uncompressed size
  809. const size = data.readUInt32LE(consts.LOCLEN);
  810. if (size && size !== consts.EF_ZIP64_OR_32) {
  811. this.size = size;
  812. }
  813. // filename length
  814. this.fnameLen = data.readUInt16LE(consts.LOCNAM);
  815. // extra field length
  816. this.extraLen = data.readUInt16LE(consts.LOCEXT);
  817. }
  818. read(data, offset, textDecoder) {
  819. const nameData = data.slice(offset, (offset += this.fnameLen));
  820. this.name = textDecoder
  821. ? textDecoder.decode(new Uint8Array(nameData))
  822. : nameData.toString('utf8');
  823. const lastChar = data[offset - 1];
  824. this.isDirectory = lastChar === 47 || lastChar === 92;
  825. if (this.extraLen) {
  826. this.readExtra(data, offset);
  827. offset += this.extraLen;
  828. }
  829. this.comment = this.comLen ? data.slice(offset, offset + this.comLen).toString() : null;
  830. }
  831. validateName() {
  832. if (/\\|^\w+:|^\/|(^|\/)\.\.(\/|$)/.test(this.name)) {
  833. throw new Error('Malicious entry: ' + this.name);
  834. }
  835. }
  836. readExtra(data, offset) {
  837. let signature, size;
  838. const maxPos = offset + this.extraLen;
  839. while (offset < maxPos) {
  840. signature = data.readUInt16LE(offset);
  841. offset += 2;
  842. size = data.readUInt16LE(offset);
  843. offset += 2;
  844. if (consts.ID_ZIP64 === signature) {
  845. this.parseZip64Extra(data, offset, size);
  846. }
  847. offset += size;
  848. }
  849. }
  850. parseZip64Extra(data, offset, length) {
  851. if (length >= 8 && this.size === consts.EF_ZIP64_OR_32) {
  852. this.size = readUInt64LE(data, offset);
  853. offset += 8;
  854. length -= 8;
  855. }
  856. if (length >= 8 && this.compressedSize === consts.EF_ZIP64_OR_32) {
  857. this.compressedSize = readUInt64LE(data, offset);
  858. offset += 8;
  859. length -= 8;
  860. }
  861. if (length >= 8 && this.offset === consts.EF_ZIP64_OR_32) {
  862. this.offset = readUInt64LE(data, offset);
  863. offset += 8;
  864. length -= 8;
  865. }
  866. if (length >= 4 && this.diskStart === consts.EF_ZIP64_OR_16) {
  867. this.diskStart = data.readUInt32LE(offset);
  868. // offset += 4; length -= 4;
  869. }
  870. }
  871. get encrypted() {
  872. return (this.flags & consts.FLG_ENTRY_ENC) === consts.FLG_ENTRY_ENC;
  873. }
  874. get isFile() {
  875. return !this.isDirectory;
  876. }
  877. }
  878. class FsRead {
  879. constructor(fd, buffer, offset, length, position, callback) {
  880. this.fd = fd;
  881. this.buffer = buffer;
  882. this.offset = offset;
  883. this.length = length;
  884. this.position = position;
  885. this.callback = callback;
  886. this.bytesRead = 0;
  887. this.waiting = false;
  888. }
  889. read(sync) {
  890. StreamZip.debugLog('read', this.position, this.bytesRead, this.length, this.offset);
  891. this.waiting = true;
  892. let err;
  893. if (sync) {
  894. let bytesRead = 0;
  895. try {
  896. bytesRead = fs.readSync(
  897. this.fd,
  898. this.buffer,
  899. this.offset + this.bytesRead,
  900. this.length - this.bytesRead,
  901. this.position + this.bytesRead
  902. );
  903. } catch (e) {
  904. err = e;
  905. }
  906. this.readCallback(sync, err, err ? bytesRead : null);
  907. } else {
  908. fs.read(
  909. this.fd,
  910. this.buffer,
  911. this.offset + this.bytesRead,
  912. this.length - this.bytesRead,
  913. this.position + this.bytesRead,
  914. this.readCallback.bind(this, sync)
  915. );
  916. }
  917. }
  918. readCallback(sync, err, bytesRead) {
  919. if (typeof bytesRead === 'number') {
  920. this.bytesRead += bytesRead;
  921. }
  922. if (err || !bytesRead || this.bytesRead === this.length) {
  923. this.waiting = false;
  924. return this.callback(err, this.bytesRead);
  925. } else {
  926. this.read(sync);
  927. }
  928. }
  929. }
  930. class FileWindowBuffer {
  931. constructor(fd) {
  932. this.position = 0;
  933. this.buffer = Buffer.alloc(0);
  934. this.fd = fd;
  935. this.fsOp = null;
  936. }
  937. checkOp() {
  938. if (this.fsOp && this.fsOp.waiting) {
  939. throw new Error('Operation in progress');
  940. }
  941. }
  942. read(pos, length, callback) {
  943. this.checkOp();
  944. if (this.buffer.length < length) {
  945. this.buffer = Buffer.alloc(length);
  946. }
  947. this.position = pos;
  948. this.fsOp = new FsRead(this.fd, this.buffer, 0, length, this.position, callback).read();
  949. }
  950. expandLeft(length, callback) {
  951. this.checkOp();
  952. this.buffer = Buffer.concat([Buffer.alloc(length), this.buffer]);
  953. this.position -= length;
  954. if (this.position < 0) {
  955. this.position = 0;
  956. }
  957. this.fsOp = new FsRead(this.fd, this.buffer, 0, length, this.position, callback).read();
  958. }
  959. expandRight(length, callback) {
  960. this.checkOp();
  961. const offset = this.buffer.length;
  962. this.buffer = Buffer.concat([this.buffer, Buffer.alloc(length)]);
  963. this.fsOp = new FsRead(
  964. this.fd,
  965. this.buffer,
  966. offset,
  967. length,
  968. this.position + offset,
  969. callback
  970. ).read();
  971. }
  972. moveRight(length, callback, shift) {
  973. this.checkOp();
  974. if (shift) {
  975. this.buffer.copy(this.buffer, 0, shift);
  976. } else {
  977. shift = 0;
  978. }
  979. this.position += shift;
  980. this.fsOp = new FsRead(
  981. this.fd,
  982. this.buffer,
  983. this.buffer.length - shift,
  984. shift,
  985. this.position + this.buffer.length - shift,
  986. callback
  987. ).read();
  988. }
  989. }
  990. class EntryDataReaderStream extends stream.Readable {
  991. constructor(fd, offset, length) {
  992. super();
  993. this.fd = fd;
  994. this.offset = offset;
  995. this.length = length;
  996. this.pos = 0;
  997. this.readCallback = this.readCallback.bind(this);
  998. }
  999. _read(n) {
  1000. const buffer = Buffer.alloc(Math.min(n, this.length - this.pos));
  1001. if (buffer.length) {
  1002. fs.read(this.fd, buffer, 0, buffer.length, this.offset + this.pos, this.readCallback);
  1003. } else {
  1004. this.push(null);
  1005. }
  1006. }
  1007. readCallback(err, bytesRead, buffer) {
  1008. this.pos += bytesRead;
  1009. if (err) {
  1010. this.emit('error', err);
  1011. this.push(null);
  1012. } else if (!bytesRead) {
  1013. this.push(null);
  1014. } else {
  1015. if (bytesRead !== buffer.length) {
  1016. buffer = buffer.slice(0, bytesRead);
  1017. }
  1018. this.push(buffer);
  1019. }
  1020. }
  1021. }
  1022. class EntryVerifyStream extends stream.Transform {
  1023. constructor(baseStm, crc, size) {
  1024. super();
  1025. this.verify = new CrcVerify(crc, size);
  1026. baseStm.on('error', (e) => {
  1027. this.emit('error', e);
  1028. });
  1029. }
  1030. _transform(data, encoding, callback) {
  1031. let err;
  1032. try {
  1033. this.verify.data(data);
  1034. } catch (e) {
  1035. err = e;
  1036. }
  1037. callback(err, data);
  1038. }
  1039. }
  1040. class CrcVerify {
  1041. constructor(crc, size) {
  1042. this.crc = crc;
  1043. this.size = size;
  1044. this.state = {
  1045. crc: ~0,
  1046. size: 0,
  1047. };
  1048. }
  1049. data(data) {
  1050. const crcTable = CrcVerify.getCrcTable();
  1051. let crc = this.state.crc;
  1052. let off = 0;
  1053. let len = data.length;
  1054. while (--len >= 0) {
  1055. crc = crcTable[(crc ^ data[off++]) & 0xff] ^ (crc >>> 8);
  1056. }
  1057. this.state.crc = crc;
  1058. this.state.size += data.length;
  1059. if (this.state.size >= this.size) {
  1060. const buf = Buffer.alloc(4);
  1061. buf.writeInt32LE(~this.state.crc & 0xffffffff, 0);
  1062. crc = buf.readUInt32LE(0);
  1063. if (crc !== this.crc) {
  1064. throw new Error('Invalid CRC');
  1065. }
  1066. if (this.state.size !== this.size) {
  1067. throw new Error('Invalid size');
  1068. }
  1069. }
  1070. }
  1071. static getCrcTable() {
  1072. let crcTable = CrcVerify.crcTable;
  1073. if (!crcTable) {
  1074. CrcVerify.crcTable = crcTable = [];
  1075. const b = Buffer.alloc(4);
  1076. for (let n = 0; n < 256; n++) {
  1077. let c = n;
  1078. for (let k = 8; --k >= 0; ) {
  1079. if ((c & 1) !== 0) {
  1080. c = 0xedb88320 ^ (c >>> 1);
  1081. } else {
  1082. c = c >>> 1;
  1083. }
  1084. }
  1085. if (c < 0) {
  1086. b.writeInt32LE(c, 0);
  1087. c = b.readUInt32LE(0);
  1088. }
  1089. crcTable[n] = c;
  1090. }
  1091. }
  1092. return crcTable;
  1093. }
  1094. }
  1095. function parseZipTime(timebytes, datebytes) {
  1096. const timebits = toBits(timebytes, 16);
  1097. const datebits = toBits(datebytes, 16);
  1098. const mt = {
  1099. h: parseInt(timebits.slice(0, 5).join(''), 2),
  1100. m: parseInt(timebits.slice(5, 11).join(''), 2),
  1101. s: parseInt(timebits.slice(11, 16).join(''), 2) * 2,
  1102. Y: parseInt(datebits.slice(0, 7).join(''), 2) + 1980,
  1103. M: parseInt(datebits.slice(7, 11).join(''), 2),
  1104. D: parseInt(datebits.slice(11, 16).join(''), 2),
  1105. };
  1106. const dt_str = [mt.Y, mt.M, mt.D].join('-') + ' ' + [mt.h, mt.m, mt.s].join(':') + ' GMT+0';
  1107. return new Date(dt_str).getTime();
  1108. }
  1109. function toBits(dec, size) {
  1110. let b = (dec >>> 0).toString(2);
  1111. while (b.length < size) {
  1112. b = '0' + b;
  1113. }
  1114. return b.split('');
  1115. }
  1116. function readUInt64LE(buffer, offset) {
  1117. return buffer.readUInt32LE(offset + 4) * 0x0000000100000000 + buffer.readUInt32LE(offset);
  1118. }
  1119. module.exports = StreamZip;