Created
January 17, 2015 03:36
-
-
Save silverwind/ac8ec0c33753057cafe7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
var fs = require("graceful-fs"), | |
yazl = require("yazl"), | |
archiver = require("archiver"); | |
var dir = process.argv[2]; | |
yazlFunc(function () { | |
archiverFunc(); | |
}); | |
function archiverFunc(cb) { | |
var start = now(); | |
var output = fs.createWriteStream("./archiver.zip"); | |
var archive = archiver("zip"); | |
output.on('close', function () { | |
console.log("archiver took", ((now() - start) / 1e6).toFixed(0), "ms"); | |
if (cb) cb(); | |
}); | |
archive.on('error', function (err) { | |
console.log(err); | |
}); | |
archive.pipe(output); | |
walk(dir, function (err, dirs) { | |
dirs.forEach(function (dir) { | |
archive.append(fs.createReadStream(dir), { name: dir }); | |
}); | |
archive.finalize(); | |
}); | |
} | |
function yazlFunc(cb) { | |
var start = now(); | |
var zipfile = new yazl.ZipFile(); | |
walk(dir, function (err, dirs) { | |
dirs.forEach(function (dir) { | |
zipfile.addFile(dir, dir); | |
}); | |
zipfile.outputStream.pipe(fs.createWriteStream("yazl.zip")).on("close", function () { | |
console.log("yazl took", ((now() - start) / 1e6).toFixed(0), "ms"); | |
if (cb) cb(); | |
}); | |
zipfile.end(); | |
}); | |
} | |
function walk(dir, callback) { | |
var results = []; | |
fs.readdir(dir, function (error, list) { | |
var i = 0; | |
if (error) return callback(error); | |
(function next() { | |
var file = list[i++]; | |
if (!file) return callback(null, results); | |
file = dir + "/" + file; | |
fs.stat(file, function (error, stats) { | |
if (stats && stats.isDirectory()) { | |
walk(file, function (error, res) { | |
results = results.concat(res); | |
next(); | |
}); | |
} else { | |
results.push(file); | |
next(); | |
} | |
}); | |
})(); | |
}); | |
} | |
function now() { | |
var hrtime = process.hrtime(); | |
return hrtime[0] * 1e9 + hrtime[1]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment