Sin descripción

zipObject.js 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. 'use strict';
  2. var StreamHelper = require('./stream/StreamHelper');
  3. var DataWorker = require('./stream/DataWorker');
  4. var utf8 = require('./utf8');
  5. var CompressedObject = require('./compressedObject');
  6. var GenericWorker = require('./stream/GenericWorker');
  7. /**
  8. * A simple object representing a file in the zip file.
  9. * @constructor
  10. * @param {string} name the name of the file
  11. * @param {String|ArrayBuffer|Uint8Array|Buffer} data the data
  12. * @param {Object} options the options of the file
  13. */
  14. var ZipObject = function(name, data, options) {
  15. this.name = name;
  16. this.dir = options.dir;
  17. this.date = options.date;
  18. this.comment = options.comment;
  19. this.unixPermissions = options.unixPermissions;
  20. this.dosPermissions = options.dosPermissions;
  21. this._data = data;
  22. this._dataBinary = options.binary;
  23. // keep only the compression
  24. this.options = {
  25. compression : options.compression,
  26. compressionOptions : options.compressionOptions
  27. };
  28. };
  29. ZipObject.prototype = {
  30. /**
  31. * Create an internal stream for the content of this object.
  32. * @param {String} type the type of each chunk.
  33. * @return StreamHelper the stream.
  34. */
  35. internalStream: function (type) {
  36. var result = null, outputType = "string";
  37. try {
  38. if (!type) {
  39. throw new Error("No output type specified.");
  40. }
  41. outputType = type.toLowerCase();
  42. var askUnicodeString = outputType === "string" || outputType === "text";
  43. if (outputType === "binarystring" || outputType === "text") {
  44. outputType = "string";
  45. }
  46. result = this._decompressWorker();
  47. var isUnicodeString = !this._dataBinary;
  48. if (isUnicodeString && !askUnicodeString) {
  49. result = result.pipe(new utf8.Utf8EncodeWorker());
  50. }
  51. if (!isUnicodeString && askUnicodeString) {
  52. result = result.pipe(new utf8.Utf8DecodeWorker());
  53. }
  54. } catch (e) {
  55. result = new GenericWorker("error");
  56. result.error(e);
  57. }
  58. return new StreamHelper(result, outputType, "");
  59. },
  60. /**
  61. * Prepare the content in the asked type.
  62. * @param {String} type the type of the result.
  63. * @param {Function} onUpdate a function to call on each internal update.
  64. * @return Promise the promise of the result.
  65. */
  66. async: function (type, onUpdate) {
  67. return this.internalStream(type).accumulate(onUpdate);
  68. },
  69. /**
  70. * Prepare the content as a nodejs stream.
  71. * @param {String} type the type of each chunk.
  72. * @param {Function} onUpdate a function to call on each internal update.
  73. * @return Stream the stream.
  74. */
  75. nodeStream: function (type, onUpdate) {
  76. return this.internalStream(type || "nodebuffer").toNodejsStream(onUpdate);
  77. },
  78. /**
  79. * Return a worker for the compressed content.
  80. * @private
  81. * @param {Object} compression the compression object to use.
  82. * @param {Object} compressionOptions the options to use when compressing.
  83. * @return Worker the worker.
  84. */
  85. _compressWorker: function (compression, compressionOptions) {
  86. if (
  87. this._data instanceof CompressedObject &&
  88. this._data.compression.magic === compression.magic
  89. ) {
  90. return this._data.getCompressedWorker();
  91. } else {
  92. var result = this._decompressWorker();
  93. if(!this._dataBinary) {
  94. result = result.pipe(new utf8.Utf8EncodeWorker());
  95. }
  96. return CompressedObject.createWorkerFrom(result, compression, compressionOptions);
  97. }
  98. },
  99. /**
  100. * Return a worker for the decompressed content.
  101. * @private
  102. * @return Worker the worker.
  103. */
  104. _decompressWorker : function () {
  105. if (this._data instanceof CompressedObject) {
  106. return this._data.getContentWorker();
  107. } else if (this._data instanceof GenericWorker) {
  108. return this._data;
  109. } else {
  110. return new DataWorker(this._data);
  111. }
  112. }
  113. };
  114. var removedMethods = ["asText", "asBinary", "asNodeBuffer", "asUint8Array", "asArrayBuffer"];
  115. var removedFn = function () {
  116. throw new Error("This method has been removed in JSZip 3.0, please check the upgrade guide.");
  117. };
  118. for(var i = 0; i < removedMethods.length; i++) {
  119. ZipObject.prototype[removedMethods[i]] = removedFn;
  120. }
  121. module.exports = ZipObject;