Created
September 3, 2013 02:55
-
-
Save ctalkington/6419249 to your computer and use it in GitHub Desktop.
ref ctalkington/node-archiver #44
This file contains hidden or 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
var fs = require('fs') | |
var path = require('path') | |
var zlib = require('zlib') | |
var ls = require('ls-stream') | |
var archiver = require('archiver') | |
var lazystream = require('lazystream') | |
module.exports = streamFolder | |
function streamFolder(dir) { | |
var gzipper = zlib.createGzip() | |
var archive = archiver('tar') | |
var done | |
var pending = 0 | |
archive.on('error', function(err) { | |
throw err | |
}) | |
archive.pipe(gzipper) | |
ls(dir) | |
.on('data', function(file) { | |
if (!file.stat.isFile()) return | |
var relPath = path.relative(dir, file.path) | |
var fileData | |
var source = new lazystream.Readable(function(options) { | |
return fs.createReadStream(file.path); | |
}); | |
pending++ | |
archive.append(source, { name: relPath }) | |
finish() | |
}) | |
.on('end', function() { | |
done = true | |
}) | |
function finish() { | |
pending-- | |
if (pending !== 0 || !done) return | |
archive.finalize() | |
} | |
return gzipper | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment