diff --git a/lib/server/pack.js b/lib/server/pack.js index 9313c1f7..be91c1dc 100644 --- a/lib/server/pack.js +++ b/lib/server/pack.js @@ -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); + }); + } })(); diff --git a/lib/server/rest.js b/lib/server/rest.js index bbb75bd4..140d1671 100644 --- a/lib/server/rest.js +++ b/lib/server/rest.js @@ -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), diff --git a/package.json b/package.json index 40b1b115..df67ebc0 100644 --- a/package.json +++ b/package.json @@ -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",