zip.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /**
  2. * ZIP Format Plugin
  3. *
  4. * @module plugins/zip
  5. * @license [MIT]{@link https://github.com/archiverjs/node-archiver/blob/master/LICENSE}
  6. * @copyright (c) 2012-2014 Chris Talkington, contributors.
  7. */
  8. var engine = require('zip-stream');
  9. var util = require('archiver-utils');
  10. /**
  11. * @constructor
  12. * @param {ZipOptions} [options]
  13. * @param {String} [options.comment] Sets the zip archive comment.
  14. * @param {Boolean} [options.forceLocalTime=false] Forces the archive to contain local file times instead of UTC.
  15. * @param {Boolean} [options.forceZip64=false] Forces the archive to contain ZIP64 headers.
  16. * @param {Boolean} [options.store=false] Sets the compression method to STORE.
  17. * @param {Object} [options.zlib] Passed to [zlib]{@link https://nodejs.org/api/zlib.html#zlib_class_options}
  18. */
  19. var Zip = function(options) {
  20. if (!(this instanceof Zip)) {
  21. return new Zip(options);
  22. }
  23. options = this.options = util.defaults(options, {
  24. comment: '',
  25. forceUTC: false,
  26. store: false
  27. });
  28. this.supports = {
  29. directory: true,
  30. symlink: true
  31. };
  32. this.engine = new engine(options);
  33. };
  34. /**
  35. * @param {(Buffer|Stream)} source
  36. * @param {ZipEntryData} data
  37. * @param {String} data.name Sets the entry name including internal path.
  38. * @param {(String|Date)} [data.date=NOW()] Sets the entry date.
  39. * @param {Number} [data.mode=D:0755/F:0644] Sets the entry permissions.
  40. * @param {String} [data.prefix] Sets a path prefix for the entry name. Useful
  41. * when working with methods like `directory` or `glob`.
  42. * @param {fs.Stats} [data.stats] Sets the fs stat data for this entry allowing
  43. * for reduction of fs stat calls when stat data is already known.
  44. * @param {Boolean} [data.store=ZipOptions.store] Sets the compression method to STORE.
  45. * @param {Function} callback
  46. * @return void
  47. */
  48. Zip.prototype.append = function(source, data, callback) {
  49. this.engine.entry(source, data, callback);
  50. };
  51. /**
  52. * @return void
  53. */
  54. Zip.prototype.finalize = function() {
  55. this.engine.finalize();
  56. };
  57. /**
  58. * @return this.engine
  59. */
  60. Zip.prototype.on = function() {
  61. return this.engine.on.apply(this.engine, arguments);
  62. };
  63. /**
  64. * @return this.engine
  65. */
  66. Zip.prototype.pipe = function() {
  67. return this.engine.pipe.apply(this.engine, arguments);
  68. };
  69. /**
  70. * @return this.engine
  71. */
  72. Zip.prototype.unpipe = function() {
  73. return this.engine.unpipe.apply(this.engine, arguments);
  74. };
  75. module.exports = Zip;
  76. /**
  77. * @typedef {Object} ZipOptions
  78. * @global
  79. * @property {String} [comment] Sets the zip archive comment.
  80. * @property {Boolean} [forceLocalTime=false] Forces the archive to contain local file times instead of UTC.
  81. * @property {Boolean} [forceZip64=false] Forces the archive to contain ZIP64 headers.
  82. * @property {Boolean} [store=false] Sets the compression method to STORE.
  83. * @property {Object} [zlib] Passed to [zlib]{@link https://nodejs.org/api/zlib.html#zlib_class_options}
  84. * to control compression.
  85. * @property {*} [*] See [zip-stream]{@link https://archiverjs.com/zip-stream/ZipStream.html} documentation for current list of properties.
  86. */
  87. /**
  88. * @typedef {Object} ZipEntryData
  89. * @global
  90. * @property {String} name Sets the entry name including internal path.
  91. * @property {(String|Date)} [date=NOW()] Sets the entry date.
  92. * @property {Number} [mode=D:0755/F:0644] Sets the entry permissions.
  93. * @property {String} [prefix] Sets a path prefix for the entry name. Useful
  94. * when working with methods like `directory` or `glob`.
  95. * @property {fs.Stats} [stats] Sets the fs stat data for this entry allowing
  96. * for reduction of fs stat calls when stat data is already known.
  97. * @property {Boolean} [store=ZipOptions.store] Sets the compression method to STORE.
  98. */
  99. /**
  100. * ZipStream Module
  101. * @external ZipStream
  102. * @see {@link https://www.archiverjs.com/zip-stream/ZipStream.html}
  103. */