123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167 |
- var zlib = require('zlib');
- var engine = require('tar-stream');
- var util = require('archiver-utils');
- var Tar = function(options) {
- if (!(this instanceof Tar)) {
- return new Tar(options);
- }
- options = this.options = util.defaults(options, {
- gzip: false
- });
- if (typeof options.gzipOptions !== 'object') {
- options.gzipOptions = {};
- }
- this.supports = {
- directory: true,
- symlink: true
- };
- this.engine = engine.pack(options);
- this.compressor = false;
- if (options.gzip) {
- this.compressor = zlib.createGzip(options.gzipOptions);
- this.compressor.on('error', this._onCompressorError.bind(this));
- }
- };
- Tar.prototype._onCompressorError = function(err) {
- this.engine.emit('error', err);
- };
- Tar.prototype.append = function(source, data, callback) {
- var self = this;
- data.mtime = data.date;
- function append(err, sourceBuffer) {
- if (err) {
- callback(err);
- return;
- }
- self.engine.entry(data, sourceBuffer, function(err) {
- callback(err, data);
- });
- }
- if (data.sourceType === 'buffer') {
- append(null, source);
- } else if (data.sourceType === 'stream' && data.stats) {
- data.size = data.stats.size;
- var entry = self.engine.entry(data, function(err) {
- callback(err, data);
- });
- source.pipe(entry);
- } else if (data.sourceType === 'stream') {
- util.collectStream(source, append);
- }
- };
- Tar.prototype.finalize = function() {
- this.engine.finalize();
- };
- Tar.prototype.on = function() {
- return this.engine.on.apply(this.engine, arguments);
- };
- Tar.prototype.pipe = function(destination, options) {
- if (this.compressor) {
- return this.engine.pipe.apply(this.engine, [this.compressor]).pipe(destination, options);
- } else {
- return this.engine.pipe.apply(this.engine, arguments);
- }
- };
- Tar.prototype.unpipe = function() {
- if (this.compressor) {
- return this.compressor.unpipe.apply(this.compressor, arguments);
- } else {
- return this.engine.unpipe.apply(this.engine, arguments);
- }
- };
- module.exports = Tar;
|