tar-iterator
Version:
Extract contents from tar archive type using an iterator API using streams or paths. Use stream interface and pipe transforms to add decompression algorithms
64 lines (63 loc) • 2.02 kB
JavaScript
import { FileEntry, waitForAccess } from 'extract-base-iterator';
import fs from 'fs';
import oo from 'on-one';
let TarFileEntry = class TarFileEntry extends FileEntry {
create(dest, options, callback) {
if (typeof options === 'function') {
callback = options;
options = null;
}
if (typeof callback === 'function') {
options = options || {};
return FileEntry.prototype.create.call(this, dest, options, (err)=>{
callback(err);
if (this.lock) {
this.lock.release();
this.lock = null;
}
});
}
return new Promise((resolve, reject)=>{
this.create(dest, options, (err, done)=>err ? reject(err) : resolve(done));
});
}
_writeFile(fullPath, _options, callback) {
if (!this.stream) {
callback(new Error('Zip FileEntry missing stream. Check for calling create multiple times'));
return;
}
const stream = this.stream;
this.stream = null;
try {
const res = stream.pipe(fs.createWriteStream(fullPath));
oo(res, [
'error',
'end',
'close',
'finish'
], (err)=>{
err ? callback(err) : waitForAccess(fullPath, callback); // gunzip stream returns prematurely occasionally
});
} catch (err) {
callback(err);
}
}
destroy() {
FileEntry.prototype.destroy.call(this);
if (this.stream) {
this.stream.resume(); // drain stream
this.stream = null;
}
if (this.lock) {
this.lock.release();
this.lock = null;
}
}
constructor(attributes, stream, lock){
super(attributes);
this.stream = stream;
this.lock = lock;
this.lock.retain();
}
};
export { TarFileEntry as default };