1 // give it a tarball and a path, and it'll dump the contents
3 module.exports = Extract
5 var tar = require("../tar.js")
6 , fstream = require("fstream")
7 , inherits = require("inherits")
8 , path = require("path")
10 function Extract (opts) {
11 if (!(this instanceof Extract)) return new Extract(opts)
14 // have to dump into a directory
15 opts.type = "Directory"
18 if (typeof opts !== "object") {
22 // better to drop in cwd? seems more standard.
23 opts.path = opts.path || path.resolve("node-tar-extract")
24 opts.type = "Directory"
27 // similar to --strip or --strip-components
28 opts.strip = +opts.strip
29 if (!opts.strip || opts.strip <= 0) opts.strip = 0
31 this._fst = fstream.Writer(opts)
36 // Hardlinks in tarballs are relative to the root
37 // of the tarball. So, they need to be resolved against
38 // the target directory in order to be created properly.
39 me.on("entry", function (entry) {
40 // if there's a "strip" argument, then strip off that many
43 var p = entry.path.split("/").slice(opts.strip).join("/")
44 entry.path = entry.props.path = p
46 var lp = entry.linkpath.split("/").slice(opts.strip).join("/")
47 entry.linkpath = entry.props.linkpath = lp
50 if (entry.type !== "Link") return
51 entry.linkpath = entry.props.linkpath =
52 path.join(opts.path, path.join("/", entry.props.linkpath))
55 this._fst.on("ready", function () {
56 me.pipe(me._fst, { end: false })
60 this._fst.on('error', function(err) {
64 this._fst.on('drain', function() {
68 // this._fst.on("end", function () {
69 // console.error("\nEEEE Extract End", me._fst.path)
72 this._fst.on("close", function () {
73 // console.error("\nEEEE Extract End", me._fst.path)
79 inherits(Extract, tar.Parse)
81 Extract.prototype._streamEnd = function () {
83 if (!me._ended) me.error("unexpected eof")
85 // my .end() is coming later.