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-entry.js 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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 normalizePath = require('normalize-path');
  10. var ArchiveEntry = require('../archive-entry');
  11. var GeneralPurposeBit = require('./general-purpose-bit');
  12. var UnixStat = require('./unix-stat');
  13. var constants = require('./constants');
  14. var zipUtil = require('./util');
  15. var ZipArchiveEntry = module.exports = function(name) {
  16. if (!(this instanceof ZipArchiveEntry)) {
  17. return new ZipArchiveEntry(name);
  18. }
  19. ArchiveEntry.call(this);
  20. this.platform = constants.PLATFORM_FAT;
  21. this.method = -1;
  22. this.name = null;
  23. this.size = 0;
  24. this.csize = 0;
  25. this.gpb = new GeneralPurposeBit();
  26. this.crc = 0;
  27. this.time = -1;
  28. this.minver = constants.MIN_VERSION_INITIAL;
  29. this.mode = -1;
  30. this.extra = null;
  31. this.exattr = 0;
  32. this.inattr = 0;
  33. this.comment = null;
  34. if (name) {
  35. this.setName(name);
  36. }
  37. };
  38. inherits(ZipArchiveEntry, ArchiveEntry);
  39. /**
  40. * Returns the extra fields related to the entry.
  41. *
  42. * @returns {Buffer}
  43. */
  44. ZipArchiveEntry.prototype.getCentralDirectoryExtra = function() {
  45. return this.getExtra();
  46. };
  47. /**
  48. * Returns the comment set for the entry.
  49. *
  50. * @returns {string}
  51. */
  52. ZipArchiveEntry.prototype.getComment = function() {
  53. return this.comment !== null ? this.comment : '';
  54. };
  55. /**
  56. * Returns the compressed size of the entry.
  57. *
  58. * @returns {number}
  59. */
  60. ZipArchiveEntry.prototype.getCompressedSize = function() {
  61. return this.csize;
  62. };
  63. /**
  64. * Returns the CRC32 digest for the entry.
  65. *
  66. * @returns {number}
  67. */
  68. ZipArchiveEntry.prototype.getCrc = function() {
  69. return this.crc;
  70. };
  71. /**
  72. * Returns the external file attributes for the entry.
  73. *
  74. * @returns {number}
  75. */
  76. ZipArchiveEntry.prototype.getExternalAttributes = function() {
  77. return this.exattr;
  78. };
  79. /**
  80. * Returns the extra fields related to the entry.
  81. *
  82. * @returns {Buffer}
  83. */
  84. ZipArchiveEntry.prototype.getExtra = function() {
  85. return this.extra !== null ? this.extra : constants.EMPTY;
  86. };
  87. /**
  88. * Returns the general purpose bits related to the entry.
  89. *
  90. * @returns {GeneralPurposeBit}
  91. */
  92. ZipArchiveEntry.prototype.getGeneralPurposeBit = function() {
  93. return this.gpb;
  94. };
  95. /**
  96. * Returns the internal file attributes for the entry.
  97. *
  98. * @returns {number}
  99. */
  100. ZipArchiveEntry.prototype.getInternalAttributes = function() {
  101. return this.inattr;
  102. };
  103. /**
  104. * Returns the last modified date of the entry.
  105. *
  106. * @returns {number}
  107. */
  108. ZipArchiveEntry.prototype.getLastModifiedDate = function() {
  109. return this.getTime();
  110. };
  111. /**
  112. * Returns the extra fields related to the entry.
  113. *
  114. * @returns {Buffer}
  115. */
  116. ZipArchiveEntry.prototype.getLocalFileDataExtra = function() {
  117. return this.getExtra();
  118. };
  119. /**
  120. * Returns the compression method used on the entry.
  121. *
  122. * @returns {number}
  123. */
  124. ZipArchiveEntry.prototype.getMethod = function() {
  125. return this.method;
  126. };
  127. /**
  128. * Returns the filename of the entry.
  129. *
  130. * @returns {string}
  131. */
  132. ZipArchiveEntry.prototype.getName = function() {
  133. return this.name;
  134. };
  135. /**
  136. * Returns the platform on which the entry was made.
  137. *
  138. * @returns {number}
  139. */
  140. ZipArchiveEntry.prototype.getPlatform = function() {
  141. return this.platform;
  142. };
  143. /**
  144. * Returns the size of the entry.
  145. *
  146. * @returns {number}
  147. */
  148. ZipArchiveEntry.prototype.getSize = function() {
  149. return this.size;
  150. };
  151. /**
  152. * Returns a date object representing the last modified date of the entry.
  153. *
  154. * @returns {number|Date}
  155. */
  156. ZipArchiveEntry.prototype.getTime = function() {
  157. return this.time !== -1 ? zipUtil.dosToDate(this.time) : -1;
  158. };
  159. /**
  160. * Returns the DOS timestamp for the entry.
  161. *
  162. * @returns {number}
  163. */
  164. ZipArchiveEntry.prototype.getTimeDos = function() {
  165. return this.time !== -1 ? this.time : 0;
  166. };
  167. /**
  168. * Returns the UNIX file permissions for the entry.
  169. *
  170. * @returns {number}
  171. */
  172. ZipArchiveEntry.prototype.getUnixMode = function() {
  173. return this.platform !== constants.PLATFORM_UNIX ? 0 : ((this.getExternalAttributes() >> constants.SHORT_SHIFT) & constants.SHORT_MASK);
  174. };
  175. /**
  176. * Returns the version of ZIP needed to extract the entry.
  177. *
  178. * @returns {number}
  179. */
  180. ZipArchiveEntry.prototype.getVersionNeededToExtract = function() {
  181. return this.minver;
  182. };
  183. /**
  184. * Sets the comment of the entry.
  185. *
  186. * @param comment
  187. */
  188. ZipArchiveEntry.prototype.setComment = function(comment) {
  189. if (Buffer.byteLength(comment) !== comment.length) {
  190. this.getGeneralPurposeBit().useUTF8ForNames(true);
  191. }
  192. this.comment = comment;
  193. };
  194. /**
  195. * Sets the compressed size of the entry.
  196. *
  197. * @param size
  198. */
  199. ZipArchiveEntry.prototype.setCompressedSize = function(size) {
  200. if (size < 0) {
  201. throw new Error('invalid entry compressed size');
  202. }
  203. this.csize = size;
  204. };
  205. /**
  206. * Sets the checksum of the entry.
  207. *
  208. * @param crc
  209. */
  210. ZipArchiveEntry.prototype.setCrc = function(crc) {
  211. if (crc < 0) {
  212. throw new Error('invalid entry crc32');
  213. }
  214. this.crc = crc;
  215. };
  216. /**
  217. * Sets the external file attributes of the entry.
  218. *
  219. * @param attr
  220. */
  221. ZipArchiveEntry.prototype.setExternalAttributes = function(attr) {
  222. this.exattr = attr >>> 0;
  223. };
  224. /**
  225. * Sets the extra fields related to the entry.
  226. *
  227. * @param extra
  228. */
  229. ZipArchiveEntry.prototype.setExtra = function(extra) {
  230. this.extra = extra;
  231. };
  232. /**
  233. * Sets the general purpose bits related to the entry.
  234. *
  235. * @param gpb
  236. */
  237. ZipArchiveEntry.prototype.setGeneralPurposeBit = function(gpb) {
  238. if (!(gpb instanceof GeneralPurposeBit)) {
  239. throw new Error('invalid entry GeneralPurposeBit');
  240. }
  241. this.gpb = gpb;
  242. };
  243. /**
  244. * Sets the internal file attributes of the entry.
  245. *
  246. * @param attr
  247. */
  248. ZipArchiveEntry.prototype.setInternalAttributes = function(attr) {
  249. this.inattr = attr;
  250. };
  251. /**
  252. * Sets the compression method of the entry.
  253. *
  254. * @param method
  255. */
  256. ZipArchiveEntry.prototype.setMethod = function(method) {
  257. if (method < 0) {
  258. throw new Error('invalid entry compression method');
  259. }
  260. this.method = method;
  261. };
  262. /**
  263. * Sets the name of the entry.
  264. *
  265. * @param name
  266. * @param prependSlash
  267. */
  268. ZipArchiveEntry.prototype.setName = function(name, prependSlash = false) {
  269. name = normalizePath(name, false)
  270. .replace(/^\w+:/, '')
  271. .replace(/^(\.\.\/|\/)+/, '');
  272. if (prependSlash) {
  273. name = `/${name}`;
  274. }
  275. if (Buffer.byteLength(name) !== name.length) {
  276. this.getGeneralPurposeBit().useUTF8ForNames(true);
  277. }
  278. this.name = name;
  279. };
  280. /**
  281. * Sets the platform on which the entry was made.
  282. *
  283. * @param platform
  284. */
  285. ZipArchiveEntry.prototype.setPlatform = function(platform) {
  286. this.platform = platform;
  287. };
  288. /**
  289. * Sets the size of the entry.
  290. *
  291. * @param size
  292. */
  293. ZipArchiveEntry.prototype.setSize = function(size) {
  294. if (size < 0) {
  295. throw new Error('invalid entry size');
  296. }
  297. this.size = size;
  298. };
  299. /**
  300. * Sets the time of the entry.
  301. *
  302. * @param time
  303. * @param forceLocalTime
  304. */
  305. ZipArchiveEntry.prototype.setTime = function(time, forceLocalTime) {
  306. if (!(time instanceof Date)) {
  307. throw new Error('invalid entry time');
  308. }
  309. this.time = zipUtil.dateToDos(time, forceLocalTime);
  310. };
  311. /**
  312. * Sets the UNIX file permissions for the entry.
  313. *
  314. * @param mode
  315. */
  316. ZipArchiveEntry.prototype.setUnixMode = function(mode) {
  317. mode |= this.isDirectory() ? constants.S_IFDIR : constants.S_IFREG;
  318. var extattr = 0;
  319. extattr |= (mode << constants.SHORT_SHIFT) | (this.isDirectory() ? constants.S_DOS_D : constants.S_DOS_A);
  320. this.setExternalAttributes(extattr);
  321. this.mode = mode & constants.MODE_MASK;
  322. this.platform = constants.PLATFORM_UNIX;
  323. };
  324. /**
  325. * Sets the version of ZIP needed to extract this entry.
  326. *
  327. * @param minver
  328. */
  329. ZipArchiveEntry.prototype.setVersionNeededToExtract = function(minver) {
  330. this.minver = minver;
  331. };
  332. /**
  333. * Returns true if this entry represents a directory.
  334. *
  335. * @returns {boolean}
  336. */
  337. ZipArchiveEntry.prototype.isDirectory = function() {
  338. return this.getName().slice(-1) === '/';
  339. };
  340. /**
  341. * Returns true if this entry represents a unix symlink,
  342. * in which case the entry's content contains the target path
  343. * for the symlink.
  344. *
  345. * @returns {boolean}
  346. */
  347. ZipArchiveEntry.prototype.isUnixSymlink = function() {
  348. return (this.getUnixMode() & UnixStat.FILE_TYPE_FLAG) === UnixStat.LINK_FLAG;
  349. };
  350. /**
  351. * Returns true if this entry is using the ZIP64 extension of ZIP.
  352. *
  353. * @returns {boolean}
  354. */
  355. ZipArchiveEntry.prototype.isZip64 = function() {
  356. return this.csize > constants.ZIP64_MAGIC || this.size > constants.ZIP64_MAGIC;
  357. };