How to use the Parse function from tar

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

tar.Parse is a class provided by the tar module that parses a tarball and extracts the contents.

232
233
234
235
236
237
238
239
240
    return setResult(err)
    //return closeReadStream(rstr)
  }
}

const tarParser = new tar.Parse()
const processEntry = entry => {
  /* istanbul ignore if */
  if (entry.ignore || entry.meta) return entry.resume()
fork icon3
star icon4
watch icon1

+ 17 other calls in file

46
47
48
49
50
51
52
53
54
55
56
 * @param {string} version eg. 1.8.0
 * @returns a Map<string, Buffer>, filename → content
 */
async function getRemoteDistFiles (packageName, version) {
  const files = new Map()
  const tarball = await pacote.tarball.stream(`${packageName}@${version}`, stream => pipeline(stream, new tar.Parse()))


  tarball.on('entry', (readEntry) => {
    if (readEntry.path.startsWith('package/dist/')) {
      readEntry
fork icon0
star icon0
watch icon1

+ 9 other calls in file

How does tar.Parse work?

tar.Parse works by consuming a stream of tarball data and parsing the contents of the tarball to extract the files and directories it contains.

When you create a new instance of tar.Parse, you provide it with a stream of tarball data (e.g. a readable stream from a file or network connection). tar.Parse then reads from this stream and parses the data to extract the files and directories in the tarball.

As tar.Parse reads through the tarball, it emits events for each file and directory it encounters. You can listen for these events using the on method, which allows you to process the files and directories as they are extracted.

Once tar.Parse has finished reading the tarball, it emits a end event to indicate that the entire tarball has been parsed.

By using tar.Parse, JavaScript developers can programmatically extract the contents of tarballs, allowing them to manipulate the files and directories they contain.

Ai Example

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

// create a new instance of tar.Parse and set up event handlers
const parser = new tar.Parse();
parser.on("entry", (entry) => {
  console.log(`Extracting file: ${entry.path}`);
});
parser.on("end", () => {
  console.log("Done!");
});

// create a readable stream from the tarball file
const stream = fs.createReadStream("my-tarball.tar");

// pipe the stream to the tar.Parse instance to extract the contents
stream.pipe(parser);

In this example, we're using tar.Parse to extract the contents of a tarball file named my-tarball.tar. We first create a new instance of tar.Parse and set up event handlers for the entry and end events. The entry event is emitted for each file and directory in the tarball, allowing us to log the path of each extracted file. The end event is emitted when the entire tarball has been parsed, allowing us to log a "Done!" message. We then create a readable stream from the my-tarball.tar file using the fs.createReadStream function. We then pipe this stream to the tar.Parse instance using the pipe method, which causes the tarball data to be read and parsed. When we run this code, it will extract the contents of the my-tarball.tar file and log the path of each extracted file to the console, followed by a "Done!" message: yaml Copy code