UNPKG

fast-extract

Version:

Extract contents from various archive types (tar, tar.bz2, tar.gz, tar.xz, tgz, zip)

49 lines (48 loc) 1.4 kB
import ZipIterator from 'zip-iterator'; import { Transform } from '../../compat/stream.js'; let ZipTransform = class ZipTransform extends Transform { _transform(chunk, _encoding, callback) { const fullPath = typeof chunk === 'string' ? chunk : chunk.toString(); this._iterator = new ZipIterator(fullPath); this._iterator.forEach((entry)=>{ this.push(entry); }, { concurrency: 1 }, (err)=>{ if (!this._iterator) return; err || this.push(null); this._iterator.destroy(); this._iterator = null; this._callback ? this._callback(err) : this.end(err); this._callback = null; callback(err); }); } _flush(callback) { if (!this._iterator) { callback(); return; } this._callback = callback; this._iterator.end(); } destroy(error) { if (this._iterator) { const iterator = this._iterator; this._iterator = null; iterator.destroy(error); this.end(error); } return this; } constructor(options){ options = options ? { ...options, objectMode: true } : { objectMode: true }; super(options); } }; export { ZipTransform as default };