Socket
Socket
Sign inDemoInstall

unzip-stream

Package Overview
Dependencies
6
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.1.2 to 0.2.0

76

lib/extract.js

@@ -0,27 +1,75 @@

var fs = require('fs');
var path = require('path');
var util = require('util');
var Writer = require('fstream').Writer;
var ParserStream = require('./parser-stream');
var mkdirp = require('mkdirp');
var Transform = require('stream').Transform;
var UnzipStream = require('./unzip-stream');
function Extract (opts) {
if (!(this instanceof Extract))
if (!(this instanceof Extract))
return new Extract(opts);
var self = this;
Transform.call(this);
ParserStream.call(self,opts);
this.opts = opts || {};
this.unzipStream = new UnzipStream();
this.unfinishedEntries = [];
self.on('data', function(entry) {
if (entry.isDirectory) return;
entry.pipe(Writer({
path: path.join(opts.path,entry.path)
}))
.on('error',function(e) {
self.emit('error',e);
var self = this;
this.unzipStream.on('entry', this._processEntry.bind(this));
this.unzipStream.on('error', function(error) {
self.emit('error', error);
});
});
}
util.inherits(Extract, ParserStream);
util.inherits(Extract, Transform);
Extract.prototype._transform = function (chunk, encoding, cb) {
this.unzipStream.write(chunk, encoding, cb);
}
Extract.prototype._flush = function (cb) {
var self = this;
var allDone = function() {
process.nextTick(function() { self.emit('close'); });
cb();
}
this.unzipStream.end(function() {
if (self.unfinishedEntries.length === 0) return allDone();
var waitingEntries = self.unfinishedEntries.length;
var finishedFn = function() {
waitingEntries--;
if (waitingEntries === 0) return allDone();
}
for(var i=0; i < waitingEntries; i++) {
self.unfinishedEntries[i].on('finish', finishedFn);
}
});
}
Extract.prototype._processEntry = function (entry) {
var self = this;
var destPath = path.join(this.opts.path, entry.path);
var directory = entry.isDirectory ? destPath : path.dirname(destPath);
mkdirp(directory, function(err) {
if (err) return self.emit('error', err);
if (entry.isDirectory) return;
var pipedStream = fs.createWriteStream(destPath);
self.unfinishedEntries.push(pipedStream);
pipedStream.on('finish', function() {
var idx = self.unfinishedEntries.indexOf(pipedStream);
if (idx < 0) return;
self.unfinishedEntries.splice(idx, 1);
});
pipedStream.on('error', function (error) {
self.emit('error', error);
});
entry.pipe(pipedStream);
});
}
module.exports = Extract;

6

lib/parser-stream.js

@@ -15,3 +15,2 @@ var Transform = require('stream').Transform;

this.unzipStream = new UnzipStream();
this.finishCb = null;

@@ -34,4 +33,5 @@ var self = this;

ParserStream.prototype._flush = function (cb) {
this.unzipStream.end(() => {
this.emit('close'); // compatibility with node-unzip
var self = this;
this.unzipStream.end(function() {
process.nextTick(function() { self.emit('close'); });
cb();

@@ -38,0 +38,0 @@ });

@@ -153,2 +153,4 @@ var binary = require('binary');

var isDirectory = vars.compressedSize === 0 && /[\/\\]$/.test(entry.path);
// protect against malicious zip files which want to extract to parent dirs
entry.path = entry.path.replace(/^([/\\]*[.]+[/\\]+)*[/\\]*/, "");
entry.type = isDirectory ? 'Directory' : 'File';

@@ -155,0 +157,0 @@ entry.isDirectory = isDirectory;

{
"name": "unzip-stream",
"version": "0.1.2",
"description": "Unzip cross-platform streaming API compatible with fstream",
"version": "0.2.0",
"description": "Process zip files using streaming API",
"author": "Michal Hruby <michal.mhr@gmail.com>",

@@ -19,3 +19,3 @@ "maintainers": [

"binary": "^0.3.0",
"fstream": "^1.0.10"
"mkdirp": "^0.5.1"
},

@@ -22,0 +22,0 @@ "devDependencies": {

@@ -72,3 +72,3 @@ # unzip-stream

Extract emits the 'finish' (also 'close' for compatibility with unzip) event once the zip's contents have been fully extracted to disk.
Extract will emit the 'close' event when the archive is fully extracted, do NOT use the 'finish' event, which can be emitted before the writing finishes.

@@ -75,0 +75,0 @@ ### What's missing?

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc