extract.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. 'use strict';
  2. module.exports = Extract;
  3. var Parse = require("../unzip").Parse;
  4. var Writer = require("fstream").Writer;
  5. var Writable = require('readable-stream/writable');
  6. var path = require('path');
  7. var inherits = require('util').inherits;
  8. inherits(Extract, Writable);
  9. function Extract (opts) {
  10. var self = this;
  11. if (!(this instanceof Extract)) {
  12. return new Extract(opts);
  13. }
  14. Writable.apply(this);
  15. this._opts = opts || { verbose: false };
  16. this._parser = Parse(this._opts);
  17. this._parser.on('error', function(err) {
  18. self.emit('error', err);
  19. });
  20. this.on('finish', function() {
  21. self._parser.end();
  22. });
  23. var writer = Writer({
  24. type: 'Directory',
  25. path: opts.path
  26. });
  27. writer.on('error', function(err) {
  28. self.emit('error', err);
  29. });
  30. writer.on('close', function() {
  31. self.emit('close')
  32. });
  33. this.on('pipe', function(source) {
  34. if (opts.verbose && source.path) {
  35. console.log('Archive: ', source.path);
  36. }
  37. });
  38. this._parser.pipe(writer);
  39. }
  40. Extract.prototype._write = function (chunk, encoding, callback) {
  41. if (this._parser.write(chunk)) {
  42. return callback();
  43. }
  44. return this._parser.once('drain', callback);
  45. };