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 if (typeof opts !== "object") {
18 // better to drop in cwd? seems more standard.
19 opts.path = opts.path || path.resolve("node-tar-extract")
20 opts.type = "Directory"
23 // similar to --strip or --strip-components
24 opts.strip = +opts.strip
25 if (!opts.strip || opts.strip <= 0) opts.strip = 0
27 this._fst = fstream.Writer(opts)
32 // Hardlinks in tarballs are relative to the root
33 // of the tarball. So, they need to be resolved against
34 // the target directory in order to be created properly.
35 me.on("entry", function (entry) {
36 // if there's a "strip" argument, then strip off that many
39 var p = entry.path.split("/").slice(opts.strip).join("/")
40 entry.path = entry.props.path = p
42 var lp = entry.linkpath.split("/").slice(opts.strip).join("/")
43 entry.linkpath = entry.props.linkpath = lp
46 if (entry.type === "Link") {
47 entry.linkpath = entry.props.linkpath =
48 path.join(opts.path, path.join("/", entry.props.linkpath))
51 if (entry.type === "SymbolicLink") {
52 var dn = path.dirname(entry.path) || ""
53 var linkpath = entry.props.linkpath
54 var target = path.resolve(opts.path, dn, linkpath)
55 if (target.indexOf(opts.path) !== 0) {
56 linkpath = path.join(opts.path, path.join("/", linkpath))
58 entry.linkpath = entry.props.linkpath = linkpath
62 this._fst.on("ready", function () {
63 me.pipe(me._fst, { end: false })
67 this._fst.on('error', function(err) {
71 this._fst.on('drain', function() {
75 // this._fst.on("end", function () {
76 // console.error("\nEEEE Extract End", me._fst.path)
79 this._fst.on("close", function () {
80 // console.error("\nEEEE Extract End", me._fst.path)
87 inherits(Extract, tar.Parse)
89 Extract.prototype._streamEnd = function () {
91 if (!me._ended || me._entry) me.error("unexpected eof")
93 // my .end() is coming later.