Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

zip-archive-output-stream.js 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. /**
  2. * node-compress-commons
  3. *
  4. * Copyright (c) 2014 Chris Talkington, contributors.
  5. * Licensed under the MIT license.
  6. * https://github.com/archiverjs/node-compress-commons/blob/master/LICENSE-MIT
  7. */
  8. var inherits = require('util').inherits;
  9. var crc32 = require('buffer-crc32');
  10. var {CRC32Stream} = require('crc32-stream');
  11. var {DeflateCRC32Stream} = require('crc32-stream');
  12. var ArchiveOutputStream = require('../archive-output-stream');
  13. var ZipArchiveEntry = require('./zip-archive-entry');
  14. var GeneralPurposeBit = require('./general-purpose-bit');
  15. var constants = require('./constants');
  16. var util = require('../../util');
  17. var zipUtil = require('./util');
  18. var ZipArchiveOutputStream = module.exports = function(options) {
  19. if (!(this instanceof ZipArchiveOutputStream)) {
  20. return new ZipArchiveOutputStream(options);
  21. }
  22. options = this.options = this._defaults(options);
  23. ArchiveOutputStream.call(this, options);
  24. this._entry = null;
  25. this._entries = [];
  26. this._archive = {
  27. centralLength: 0,
  28. centralOffset: 0,
  29. comment: '',
  30. finish: false,
  31. finished: false,
  32. processing: false,
  33. forceZip64: options.forceZip64,
  34. forceLocalTime: options.forceLocalTime
  35. };
  36. };
  37. inherits(ZipArchiveOutputStream, ArchiveOutputStream);
  38. ZipArchiveOutputStream.prototype._afterAppend = function(ae) {
  39. this._entries.push(ae);
  40. if (ae.getGeneralPurposeBit().usesDataDescriptor()) {
  41. this._writeDataDescriptor(ae);
  42. }
  43. this._archive.processing = false;
  44. this._entry = null;
  45. if (this._archive.finish && !this._archive.finished) {
  46. this._finish();
  47. }
  48. };
  49. ZipArchiveOutputStream.prototype._appendBuffer = function(ae, source, callback) {
  50. if (source.length === 0) {
  51. ae.setMethod(constants.METHOD_STORED);
  52. }
  53. var method = ae.getMethod();
  54. if (method === constants.METHOD_STORED) {
  55. ae.setSize(source.length);
  56. ae.setCompressedSize(source.length);
  57. ae.setCrc(crc32.unsigned(source));
  58. }
  59. this._writeLocalFileHeader(ae);
  60. if (method === constants.METHOD_STORED) {
  61. this.write(source);
  62. this._afterAppend(ae);
  63. callback(null, ae);
  64. return;
  65. } else if (method === constants.METHOD_DEFLATED) {
  66. this._smartStream(ae, callback).end(source);
  67. return;
  68. } else {
  69. callback(new Error('compression method ' + method + ' not implemented'));
  70. return;
  71. }
  72. };
  73. ZipArchiveOutputStream.prototype._appendStream = function(ae, source, callback) {
  74. ae.getGeneralPurposeBit().useDataDescriptor(true);
  75. ae.setVersionNeededToExtract(constants.MIN_VERSION_DATA_DESCRIPTOR);
  76. this._writeLocalFileHeader(ae);
  77. var smart = this._smartStream(ae, callback);
  78. source.once('error', function(err) {
  79. smart.emit('error', err);
  80. smart.end();
  81. })
  82. source.pipe(smart);
  83. };
  84. ZipArchiveOutputStream.prototype._defaults = function(o) {
  85. if (typeof o !== 'object') {
  86. o = {};
  87. }
  88. if (typeof o.zlib !== 'object') {
  89. o.zlib = {};
  90. }
  91. if (typeof o.zlib.level !== 'number') {
  92. o.zlib.level = constants.ZLIB_BEST_SPEED;
  93. }
  94. o.forceZip64 = !!o.forceZip64;
  95. o.forceLocalTime = !!o.forceLocalTime;
  96. return o;
  97. };
  98. ZipArchiveOutputStream.prototype._finish = function() {
  99. this._archive.centralOffset = this.offset;
  100. this._entries.forEach(function(ae) {
  101. this._writeCentralFileHeader(ae);
  102. }.bind(this));
  103. this._archive.centralLength = this.offset - this._archive.centralOffset;
  104. if (this.isZip64()) {
  105. this._writeCentralDirectoryZip64();
  106. }
  107. this._writeCentralDirectoryEnd();
  108. this._archive.processing = false;
  109. this._archive.finish = true;
  110. this._archive.finished = true;
  111. this.end();
  112. };
  113. ZipArchiveOutputStream.prototype._normalizeEntry = function(ae) {
  114. if (ae.getMethod() === -1) {
  115. ae.setMethod(constants.METHOD_DEFLATED);
  116. }
  117. if (ae.getMethod() === constants.METHOD_DEFLATED) {
  118. ae.getGeneralPurposeBit().useDataDescriptor(true);
  119. ae.setVersionNeededToExtract(constants.MIN_VERSION_DATA_DESCRIPTOR);
  120. }
  121. if (ae.getTime() === -1) {
  122. ae.setTime(new Date(), this._archive.forceLocalTime);
  123. }
  124. ae._offsets = {
  125. file: 0,
  126. data: 0,
  127. contents: 0,
  128. };
  129. };
  130. ZipArchiveOutputStream.prototype._smartStream = function(ae, callback) {
  131. var deflate = ae.getMethod() === constants.METHOD_DEFLATED;
  132. var process = deflate ? new DeflateCRC32Stream(this.options.zlib) : new CRC32Stream();
  133. var error = null;
  134. function handleStuff() {
  135. var digest = process.digest().readUInt32BE(0);
  136. ae.setCrc(digest);
  137. ae.setSize(process.size());
  138. ae.setCompressedSize(process.size(true));
  139. this._afterAppend(ae);
  140. callback(error, ae);
  141. }
  142. process.once('end', handleStuff.bind(this));
  143. process.once('error', function(err) {
  144. error = err;
  145. });
  146. process.pipe(this, { end: false });
  147. return process;
  148. };
  149. ZipArchiveOutputStream.prototype._writeCentralDirectoryEnd = function() {
  150. var records = this._entries.length;
  151. var size = this._archive.centralLength;
  152. var offset = this._archive.centralOffset;
  153. if (this.isZip64()) {
  154. records = constants.ZIP64_MAGIC_SHORT;
  155. size = constants.ZIP64_MAGIC;
  156. offset = constants.ZIP64_MAGIC;
  157. }
  158. // signature
  159. this.write(zipUtil.getLongBytes(constants.SIG_EOCD));
  160. // disk numbers
  161. this.write(constants.SHORT_ZERO);
  162. this.write(constants.SHORT_ZERO);
  163. // number of entries
  164. this.write(zipUtil.getShortBytes(records));
  165. this.write(zipUtil.getShortBytes(records));
  166. // length and location of CD
  167. this.write(zipUtil.getLongBytes(size));
  168. this.write(zipUtil.getLongBytes(offset));
  169. // archive comment
  170. var comment = this.getComment();
  171. var commentLength = Buffer.byteLength(comment);
  172. this.write(zipUtil.getShortBytes(commentLength));
  173. this.write(comment);
  174. };
  175. ZipArchiveOutputStream.prototype._writeCentralDirectoryZip64 = function() {
  176. // signature
  177. this.write(zipUtil.getLongBytes(constants.SIG_ZIP64_EOCD));
  178. // size of the ZIP64 EOCD record
  179. this.write(zipUtil.getEightBytes(44));
  180. // version made by
  181. this.write(zipUtil.getShortBytes(constants.MIN_VERSION_ZIP64));
  182. // version to extract
  183. this.write(zipUtil.getShortBytes(constants.MIN_VERSION_ZIP64));
  184. // disk numbers
  185. this.write(constants.LONG_ZERO);
  186. this.write(constants.LONG_ZERO);
  187. // number of entries
  188. this.write(zipUtil.getEightBytes(this._entries.length));
  189. this.write(zipUtil.getEightBytes(this._entries.length));
  190. // length and location of CD
  191. this.write(zipUtil.getEightBytes(this._archive.centralLength));
  192. this.write(zipUtil.getEightBytes(this._archive.centralOffset));
  193. // extensible data sector
  194. // not implemented at this time
  195. // end of central directory locator
  196. this.write(zipUtil.getLongBytes(constants.SIG_ZIP64_EOCD_LOC));
  197. // disk number holding the ZIP64 EOCD record
  198. this.write(constants.LONG_ZERO);
  199. // relative offset of the ZIP64 EOCD record
  200. this.write(zipUtil.getEightBytes(this._archive.centralOffset + this._archive.centralLength));
  201. // total number of disks
  202. this.write(zipUtil.getLongBytes(1));
  203. };
  204. ZipArchiveOutputStream.prototype._writeCentralFileHeader = function(ae) {
  205. var gpb = ae.getGeneralPurposeBit();
  206. var method = ae.getMethod();
  207. var offsets = ae._offsets;
  208. var size = ae.getSize();
  209. var compressedSize = ae.getCompressedSize();
  210. if (ae.isZip64() || offsets.file > constants.ZIP64_MAGIC) {
  211. size = constants.ZIP64_MAGIC;
  212. compressedSize = constants.ZIP64_MAGIC;
  213. ae.setVersionNeededToExtract(constants.MIN_VERSION_ZIP64);
  214. var extraBuf = Buffer.concat([
  215. zipUtil.getShortBytes(constants.ZIP64_EXTRA_ID),
  216. zipUtil.getShortBytes(24),
  217. zipUtil.getEightBytes(ae.getSize()),
  218. zipUtil.getEightBytes(ae.getCompressedSize()),
  219. zipUtil.getEightBytes(offsets.file)
  220. ], 28);
  221. ae.setExtra(extraBuf);
  222. }
  223. // signature
  224. this.write(zipUtil.getLongBytes(constants.SIG_CFH));
  225. // version made by
  226. this.write(zipUtil.getShortBytes((ae.getPlatform() << 8) | constants.VERSION_MADEBY));
  227. // version to extract and general bit flag
  228. this.write(zipUtil.getShortBytes(ae.getVersionNeededToExtract()));
  229. this.write(gpb.encode());
  230. // compression method
  231. this.write(zipUtil.getShortBytes(method));
  232. // datetime
  233. this.write(zipUtil.getLongBytes(ae.getTimeDos()));
  234. // crc32 checksum
  235. this.write(zipUtil.getLongBytes(ae.getCrc()));
  236. // sizes
  237. this.write(zipUtil.getLongBytes(compressedSize));
  238. this.write(zipUtil.getLongBytes(size));
  239. var name = ae.getName();
  240. var comment = ae.getComment();
  241. var extra = ae.getCentralDirectoryExtra();
  242. if (gpb.usesUTF8ForNames()) {
  243. name = Buffer.from(name);
  244. comment = Buffer.from(comment);
  245. }
  246. // name length
  247. this.write(zipUtil.getShortBytes(name.length));
  248. // extra length
  249. this.write(zipUtil.getShortBytes(extra.length));
  250. // comments length
  251. this.write(zipUtil.getShortBytes(comment.length));
  252. // disk number start
  253. this.write(constants.SHORT_ZERO);
  254. // internal attributes
  255. this.write(zipUtil.getShortBytes(ae.getInternalAttributes()));
  256. // external attributes
  257. this.write(zipUtil.getLongBytes(ae.getExternalAttributes()));
  258. // relative offset of LFH
  259. if (offsets.file > constants.ZIP64_MAGIC) {
  260. this.write(zipUtil.getLongBytes(constants.ZIP64_MAGIC));
  261. } else {
  262. this.write(zipUtil.getLongBytes(offsets.file));
  263. }
  264. // name
  265. this.write(name);
  266. // extra
  267. this.write(extra);
  268. // comment
  269. this.write(comment);
  270. };
  271. ZipArchiveOutputStream.prototype._writeDataDescriptor = function(ae) {
  272. // signature
  273. this.write(zipUtil.getLongBytes(constants.SIG_DD));
  274. // crc32 checksum
  275. this.write(zipUtil.getLongBytes(ae.getCrc()));
  276. // sizes
  277. if (ae.isZip64()) {
  278. this.write(zipUtil.getEightBytes(ae.getCompressedSize()));
  279. this.write(zipUtil.getEightBytes(ae.getSize()));
  280. } else {
  281. this.write(zipUtil.getLongBytes(ae.getCompressedSize()));
  282. this.write(zipUtil.getLongBytes(ae.getSize()));
  283. }
  284. };
  285. ZipArchiveOutputStream.prototype._writeLocalFileHeader = function(ae) {
  286. var gpb = ae.getGeneralPurposeBit();
  287. var method = ae.getMethod();
  288. var name = ae.getName();
  289. var extra = ae.getLocalFileDataExtra();
  290. if (ae.isZip64()) {
  291. gpb.useDataDescriptor(true);
  292. ae.setVersionNeededToExtract(constants.MIN_VERSION_ZIP64);
  293. }
  294. if (gpb.usesUTF8ForNames()) {
  295. name = Buffer.from(name);
  296. }
  297. ae._offsets.file = this.offset;
  298. // signature
  299. this.write(zipUtil.getLongBytes(constants.SIG_LFH));
  300. // version to extract and general bit flag
  301. this.write(zipUtil.getShortBytes(ae.getVersionNeededToExtract()));
  302. this.write(gpb.encode());
  303. // compression method
  304. this.write(zipUtil.getShortBytes(method));
  305. // datetime
  306. this.write(zipUtil.getLongBytes(ae.getTimeDos()));
  307. ae._offsets.data = this.offset;
  308. // crc32 checksum and sizes
  309. if (gpb.usesDataDescriptor()) {
  310. this.write(constants.LONG_ZERO);
  311. this.write(constants.LONG_ZERO);
  312. this.write(constants.LONG_ZERO);
  313. } else {
  314. this.write(zipUtil.getLongBytes(ae.getCrc()));
  315. this.write(zipUtil.getLongBytes(ae.getCompressedSize()));
  316. this.write(zipUtil.getLongBytes(ae.getSize()));
  317. }
  318. // name length
  319. this.write(zipUtil.getShortBytes(name.length));
  320. // extra length
  321. this.write(zipUtil.getShortBytes(extra.length));
  322. // name
  323. this.write(name);
  324. // extra
  325. this.write(extra);
  326. ae._offsets.contents = this.offset;
  327. };
  328. ZipArchiveOutputStream.prototype.getComment = function(comment) {
  329. return this._archive.comment !== null ? this._archive.comment : '';
  330. };
  331. ZipArchiveOutputStream.prototype.isZip64 = function() {
  332. return this._archive.forceZip64 || this._entries.length > constants.ZIP64_MAGIC_SHORT || this._archive.centralLength > constants.ZIP64_MAGIC || this._archive.centralOffset > constants.ZIP64_MAGIC;
  333. };
  334. ZipArchiveOutputStream.prototype.setComment = function(comment) {
  335. this._archive.comment = comment;
  336. };