UNPKG

zip-iterator

Version:

Extract contents from zip archive type using an iterator API using streams or paths. Use stream interface and pipe transforms to add decompression algorithms

54 lines (53 loc) 1.81 kB
import fs from 'fs'; import { FileEntry } from 'extract-base-iterator'; import oo from 'on-one'; import waitForAccess from './lib/waitForAccess.mjs'; let ZipFileEntry = class ZipFileEntry extends FileEntry { create(dest, options, callback) { if (typeof options === 'function') { callback = options; options = null; } const self = this; if (typeof callback === 'function') { options = options || {}; return FileEntry.prototype.create.call(this, dest, options, (err)=>{ callback(err); if (self.lock) { self.lock.release(); self.lock = null; } }); } return new Promise(function createPromise(resolve, reject) { self.create(dest, options, (err, done)=>err ? reject(err) : resolve(done)); }); } _writeFile(fullPath, _, callback) { if (!this.entry) return callback(new Error('Zip FileEntry missing entry. Check for calling create multiple times')); const res = this.entry.getStream().pipe(fs.createWriteStream(fullPath)); oo(res, [ 'error', 'end', 'close', 'finish' ], (err)=>{ err ? callback(err) : waitForAccess(fullPath, callback); // gunzip stream returns prematurely occasionally }); } destroy() { FileEntry.prototype.destroy.call(this); this.entry = null; if (this.lock) { this.lock.release(); this.lock = null; } } constructor(attributes, entry, lock){ super(attributes); this.entry = entry; this.lock = lock; this.lock.retain(); } }; export { ZipFileEntry as default };