decompress
Advanced tools
Comparing version 0.1.1 to 0.1.2
'use strict'; | ||
var fs = require('fs'); | ||
var _ = require('lodash'); | ||
var mkdir = require('mkdirp'); | ||
var mout = require('mout'); | ||
var pipeline = require('stream-combiner'); | ||
var tar = require('tar'); | ||
@@ -10,6 +11,19 @@ var unzip = require('unzip'); | ||
_.str = require('underscore.string'); | ||
_.mixin(_.str.exports()); | ||
/** | ||
* Initialize Decompress with options | ||
* | ||
* Options: | ||
* | ||
* - `ext` String with file name, MIME type, etc | ||
* - `path` Path to extract to | ||
* | ||
* @param {Object} opts | ||
* @api private | ||
*/ | ||
function Decompress() { | ||
function Decompress(opts) { | ||
opts = opts || {}; | ||
this.opts = opts; | ||
this.path = opts.path || process.cwd(); | ||
this.ext = opts.ext || ''; | ||
this.extractors = { | ||
@@ -25,16 +39,30 @@ '.zip': this._extractZip, | ||
this.extractorTypes = Object.keys(this.extractors); | ||
this.extractor = this._getExtractor(this.ext); | ||
} | ||
Decompress.prototype.extract = function (opts) { | ||
opts = opts || {}; | ||
opts.path = opts.path || process.cwd(); | ||
var extractor = this._getExtractor(opts.type); | ||
/** | ||
* Extract an archive | ||
* | ||
* @api public | ||
*/ | ||
if (!fs.existsSync(opts.path)) { | ||
mkdir.sync(opts.path); | ||
Decompress.prototype.extract = function () { | ||
var self = this; | ||
var stream = this.extractor(); | ||
if (!fs.existsSync(this.path)) { | ||
mkdir.sync(self.path); | ||
} | ||
return extractor(opts); | ||
return stream; | ||
}; | ||
/** | ||
* Check if a file can be extracted | ||
* | ||
* @param {String} src | ||
* @param {String} mime | ||
* @api public | ||
*/ | ||
Decompress.prototype.canExtract = function (src, mime) { | ||
@@ -52,36 +80,65 @@ if (this._getExtractor(src)) { | ||
/** | ||
* Get the extractor for a desired file | ||
* | ||
* @param {String} src | ||
* @api private | ||
*/ | ||
Decompress.prototype._getExtractor = function (src) { | ||
src = src.toLowerCase(); | ||
var type = _.find(this.extractorTypes, function (type) { | ||
return _.endsWith(src, type); | ||
var ext = mout.array.find(this.extractorTypes, function (ext) { | ||
return mout.string.endsWith(src, ext); | ||
}); | ||
return type ? this.extractors[type] : null; | ||
return ext ? this.extractors[ext] : null; | ||
}; | ||
/** | ||
* Extract a zip file | ||
* | ||
* @api private | ||
*/ | ||
Decompress.prototype._extractZip = function (opts) { | ||
return unzip.Extract(opts); | ||
Decompress.prototype._extractZip = function () { | ||
var stream = unzip.Extract(this.opts); | ||
return stream; | ||
}; | ||
Decompress.prototype._extractTar = function (opts) { | ||
return tar.Extract(opts); | ||
/** | ||
* Extract a tar file | ||
* | ||
* @api private | ||
*/ | ||
Decompress.prototype._extractTar = function () { | ||
var stream = tar.Extract(this.opts); | ||
return stream; | ||
}; | ||
Decompress.prototype._extractTarGz = function (opts) { | ||
var stream = zlib.Gunzip(); | ||
stream.pipe(tar.Extract(opts)); | ||
/** | ||
* Extract a tar.gz file | ||
* | ||
* @api private | ||
*/ | ||
return stream; | ||
Decompress.prototype._extractTarGz = function () { | ||
var stream = zlib.Unzip(); | ||
var dest = tar.Extract(this.opts); | ||
return pipeline(stream, dest); | ||
}; | ||
/** | ||
* Module exports | ||
*/ | ||
module.exports.extract = function (opts) { | ||
var unpacker = new Decompress(); | ||
return unpacker.extract(opts); | ||
var decompress = new Decompress(opts); | ||
return decompress.extract(); | ||
}; | ||
module.exports.canExtract = function (src, mime) { | ||
var unpacker = new Decompress(); | ||
return unpacker.canExtract(src, mime); | ||
var decompress = new Decompress(); | ||
return decompress.canExtract(src, mime); | ||
}; |
{ | ||
"name": "decompress", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"description": "Easily extract zip, tar and tar.gz archives", | ||
@@ -28,10 +28,11 @@ "keywords": [ | ||
"dependencies": { | ||
"lodash": "~1.3.1", | ||
"mkdirp": "~0.3.5", | ||
"mout": "~0.6.0", | ||
"stream-combiner": "~0.0.2", | ||
"tar": "~0.1.18", | ||
"underscore.string": "~2.3.3", | ||
"unzip": "~0.1.8" | ||
}, | ||
"devDependencies": { | ||
"mocha": "~1.12.0" | ||
"mocha": "~1.12.0", | ||
"rimraf": "~2.2.2" | ||
}, | ||
@@ -38,0 +39,0 @@ "engines": { |
@@ -12,3 +12,3 @@ # decompress [![Build Status](https://secure.travis-ci.org/kevva/decompress.png?branch=master)](http://travis-ci.org/kevva/decompress) | ||
You'll only need to give decompress a `type` and it'll figure the rest out for | ||
You'll only need to pass a type into `ext` and it'll figure the rest out for | ||
you. | ||
@@ -20,4 +20,6 @@ | ||
fs.createReadStream('foo.tar.gz') | ||
.pipe(decompress.extract({ type: '.tar.gz', path: 'bar' })); | ||
var src = fs.createReadStream('foo.tar.gz'); | ||
var dest = decompress.extract({ ext: '.tar.gz' }); | ||
src.pipe(dest); | ||
``` | ||
@@ -29,3 +31,3 @@ | ||
Extract an archive using the `type` option to determine which extractor to use. | ||
Extract an archive using the `ext` option to determine which extractor to use. | ||
If no `path` is specified it'll extract it to your current location. | ||
@@ -48,4 +50,4 @@ | ||
* `type` — String that can be a file name, URL or a MIME type for example. | ||
* `path` — Path to extract the archive to. If no `path` is specified it'll | ||
* `ext` — String that can be a file name, URL, MIME type etc. | ||
* `path` — Path to extract the archive to. If no `path` is defined it'll | ||
extract it to your current location. | ||
@@ -52,0 +54,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
5462
117
57
2
+ Addedmout@~0.6.0
+ Addedstream-combiner@~0.0.2
+ Addedduplexer@0.1.2(transitive)
+ Addedmout@0.6.0(transitive)
+ Addedstream-combiner@0.0.4(transitive)
- Removedlodash@~1.3.1
- Removedunderscore.string@~2.3.3
- Removedlodash@1.3.1(transitive)
- Removedunderscore.string@2.3.3(transitive)