mirror of
https://github.com/coderaiser/cloudcmd.git
synced 2026-01-23 18:55:26 +00:00
feature(pack) add ability pack and unpack folders
This commit is contained in:
parent
999fefa0e5
commit
ff198223eb
3 changed files with 68 additions and 12 deletions
|
|
@ -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);
|
||||
});
|
||||
}
|
||||
})();
|
||||
|
|
|
|||
|
|
@ -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),
|
||||
|
|
|
|||
|
|
@ -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",
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue