How to use the x function from tar

Find comprehensive JavaScript tar.x code examples handpicked from public code repositorys.

tar.x is a Node.js module that extracts the contents of a tarball file into a directory.

85
86
87
88
89
90
91
92
93
94
  await decompress(artifactPath, CSFLE_DIRECTORY);
} else {
  // For .tar files, `decompress` is buggy, so we use `tar` instead
  await promisify(pipeline)(
    createReadStream(artifactPath),
    tar.x({
      C: CSFLE_DIRECTORY,
    })
  );
}
fork icon120
star icon761
watch icon0

90
91
92
93
94
95
96
97
98
99
if (fs.existsSync(EXTRACTED_MARK_FILE)) {
  log.silly('Lfw', 'extract() already extracted')
  return
}

await tar.x({
  file:   this.downloadFile,
  strip:  1,
  cwd:    this.directory,
})
fork icon76
star icon399
watch icon26

How does tar.x work?

The tar.x method in the tar library is used to extract a tar archive. It reads the archive file, creates the necessary directories, and writes the extracted files to their respective paths. It can also optionally decompress the archive if it is gzip- or bzip2-compressed.

192
193
194
195
196
197
198
199
200
201
    fs.rmdirSync(path.join(homeDir, dir), { recursive: true });
}
// Download the tar file
downloadFile(`${bucketParentPath}/${dir}.tar.gz`, path.join(homeDir, `${dir}.tar.gz`), win).then(() => {
    // Extract the tar file
    tar.x({ cwd: homeDir, preservePaths: true, file: path.join(homeDir, `${dir}.tar.gz`) }).then(() => {
        // Delete the tar file
        deleteFile(path.join(homeDir, `${dir}.tar.gz`)).then(() => {
            win.webContents.send("updateStatus", `Downloaded ${dir}`);
            total++;
fork icon1
star icon3
watch icon2

52
53
54
55
56
57
58
59
60
61
  let node
  if (platform == 'win32') {
    await extract(filepath, {dir: cwd})
    node = path.join(cwd, filename, 'node.exe')
  } else {
    await tar.x({file: filepath, cwd})
    node = path.join(cwd, filename, 'bin', 'node')
  }
  process.exit(spawnSync(node, ['--expose-gc', 'napi_yue/test', outDir, '--run-gc-tests']).status)
}
fork icon137
star icon0
watch icon0

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
const tar = require("tar");

tar.x(
  {
    file: "archive.tar",
    cwd: "/path/to/extract",
  },
  (err) => {
    if (err) {
      console.error(err);
    } else {
      console.log("Extraction complete!");
    }
  }
);

In this example, tar.x() is called with an object that has two properties: file, which specifies the name of the tar archive to extract, and cwd, which specifies the path to the directory where the files should be extracted. The second argument to tar.x() is a callback function that is called when the extraction is complete or if an error occurs.

41
42
43
44
45
46
47
48
49
50
51
useTmpDir(async (cwd) => {
  const url = `${prefix[runtime]}/${version}/node-${version}-headers.tar.gz`
  const file = path.join(cwd, 'node_headers.tar.gz')
  const res = await fetch(url)
  await streamPromise(res.body.pipe(fs.createWriteStream(file)))
  await tar.x({file, cwd})


  const subdir = runtime == 'electron' ? 'node_headers' : `node-${version}`
  await fs.move(path.join(cwd, subdir, 'include'),
                path.join(nodeDir, 'include'))
fork icon137
star icon0
watch icon0

16
17
18
19
20
21
22
23
24
25
26
27
dotenv.config();


const dbName = 'GeoLite2-Country';


const extractArchive = async (output) => {
    const archive = await tar.x({
        file: output,
        C: './',
    });

fork icon0
star icon1
watch icon1

209
210
211
212
213
214
215
216
217
218
await mkdirp(`./metadata/${provider}.cache`);
// TODO allow for authentication
const codeloadUrl = `https://codeload.github.com/${md.org}/${md.repo}/tar.gz/${md.branch}`;
const res = await fetch(codeloadUrl, { cacheFolder: archiveCache });
if (res.ok) {
  const tarx = tar.x({ strip: 1, C: `./metadata/${provider}.cache` });
  const stream = res.body.pipe(tarx);
  stream.on('warn',function(code,message,data){
    logger.warn('tar !',message);
  });
fork icon0
star icon0
watch icon1

+ 5 other calls in file

33
34
35
36
37
38
39
40
41
42
            : extractTarGz(filePath, dir);
    });
}
exports.extract = extract;
function extractTarGz(filePath, dir) {
    return tar.x({ file: filePath, cwd: dir }).then(() => new Promise((resolve, reject) => {
        fs.unlink(filePath, (err) => {
            if (err) {
                reject(err);
            }
fork icon0
star icon0
watch icon0

+ 9 other calls in file

25
26
27
28
29
30
31
32
33
34
}

console.log('Starting Decrypt.js');
const IV = data.iv ? Buffer.from(data.iv.toString()) : defaultIV;
const decipher = crypto.createDecipheriv(algorithm, Buffer.from(password), IV);
let extractor = tar.x({
  trim: 1,
  C: dir // the dir where it will extract
})
createReadStream(data.sourcePath).pipe(decipher).pipe(extractor);
fork icon0
star icon0
watch icon0

73
74
75
76
77
78
79
80
81
82

let started = 0;
let timeout = null;

return new Promise((resolve, reject) => {
  tar.x(
    {
      file: tarPath,
      preservePaths: true,
      cwd: destination,
fork icon0
star icon0
watch icon0