feature(pack) add ability pack and unpack folders

This commit is contained in:
coderaiser 2014-09-10 08:16:21 -04:00
parent 999fefa0e5
commit ff198223eb
3 changed files with 68 additions and 12 deletions

View file

@ -1,22 +1,76 @@
(function() {
'use strict';
var DIR = './',
pipe = require(DIR + 'pipe');
var DIR = './',
DIR_LIB = DIR + '../',
path = require('path'),
fs = require('fs'),
tryRequire = require(DIR + 'tryRequire'),
tar = tryRequire('tar'),
fstream = tryRequire('fstream'),
Util = require(DIR_LIB + 'util'),
pipe = require(DIR + 'pipe');
exports.gzip = function(from, to, callback) {
var options = {
gzip : true
};
isDir(from, function(is) {
var options = {
gzip: true
};
pipe.create(from, to, options, callback);
if (!is || !fstream || !tar)
pipe.create(from, to, options, callback);
else
createTar(from, function(readStream) {
var dir = path.dirname(to),
name = path.basename(to, '.zip');
to = dir + path.sep + name + '.tar.gz';
pipe.create(readStream, to, options, callback);
});
});
};
exports.gunzip = function(from, to, callback) {
var options = {
gunzip : true
};
var write,
isStr = Util.isString(from),
check = Util.checkExt,
isTarGz = isStr && check(from, 'tar.gz'),
pipe.create(from, to, options, callback);
options = {
gunzip : true
};
if (tar && isTarGz) {
write = tar.Extract({ path: to });
} else {
write = to;
}
pipe.create(from, write, options, callback);
};
function createTar(from, callback) {
var options = { path: from, type: 'Directory' },
stream = fstream.Reader(options)
.pipe(tar.Pack({ noProprietary: true }));
callback(stream);
}
function isDir(name, callback) {
fs.stat(name, function(error, stat) {
var isDir;
if (!error)
isDir = stat.isDirectory();
callback(isDir);
});
}
})();

View file

@ -350,7 +350,7 @@
if (files.to)
to = mellow.convertPath(files.to);
else
to = Util.rmStrOnce(files.from, ['.zip', '.gzip']);
to = Util.rmStrOnce(files.from, ['.zip', '.tar.gz', '.gz']);
pack.gunzip(from, to, function(error) {
var name = path.basename(files.from),

View file

@ -28,6 +28,7 @@
"console-io": "~1.6.5",
"dropbox": "0.10.x",
"express": "~4.8.2",
"fstream": "~1.0.2",
"http-auth": "2.1.x",
"marked": "~0.3.2",
"minify": "~1.0.0",
@ -35,7 +36,8 @@
"morgan": "~1.3.0",
"ncp": "~0.6.0",
"rimraf": "~2.2.6",
"socket.io": "~1.1.0"
"socket.io": "~1.1.0",
"tar": "~1.0.1"
},
"devDependencies": {
"gulp": "~3.8.x",