stream.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. 'use strict';
  2. const path = require('path');
  3. const yazl = require('yazl');
  4. const TarStream = require('../tar/stream');
  5. class ZipStream extends TarStream {
  6. _init() {
  7. const zipfile = this._zipfile = new yazl.ZipFile();
  8. const stream = zipfile.outputStream;
  9. stream.on('end', () => this.push(null));
  10. stream.on('data', chunk => this.push(chunk));
  11. stream.on('error', err => this.emit('error', err));
  12. }
  13. _addFileEntry(entry, opts) {
  14. this._zipfile.addFile(entry, opts.relativePath || path.basename(entry), opts);
  15. this._onEntryFinish();
  16. }
  17. _addBufferEntry(entry, opts) {
  18. if (!opts.relativePath) return this.emit('error', new Error('opts.relativePath is required if entry is a buffer'));
  19. this._zipfile.addBuffer(entry, opts.relativePath, opts);
  20. this._onEntryFinish();
  21. }
  22. _addStreamEntry(entry, opts) {
  23. if (!opts.relativePath) return this.emit('error', new Error('opts.relativePath is required if entry is a stream'));
  24. entry.on('error', err => this.emit('error', err));
  25. this._zipfile.addReadStream(entry, opts.relativePath, opts);
  26. this._onEntryFinish();
  27. }
  28. _finalize() {
  29. this._zipfile.end();
  30. }
  31. }
  32. module.exports = ZipStream;