8

I'm trying to extract a .tar file(packed from a directory) and then check the names of the files in the extracted directory. I'm using tar-fs to extract the tar file and then use fs.createReadStream to manipulate the data. Here's what I've got so far:

fs.createReadStream(req.files.file.path)
  .pipe(tar.extract(req.files.file.path + '0'))
  .on('error', function() {
    errorMessage = 'Failed to extract file. Please make sure to upload a tar file.';
  })
  .on('entry', function(header, stream, callback) {
    console.error(header);
    stream.on('end', function() {
      console.error("this is working");
    });
  })
  .on('end', function() {
    //the one did not get called
    console.error('end');
  })
;

I was hoping to extract the whole folder and then check the file names. Well, I haven't get that far yet..

To my understanding, I got a readable stream after the pipe. And a readable stream has an end event? My question is, why the end event in the code is not called?

Thanks!

Community
  • 1
  • 1
odieatla
  • 1,049
  • 3
  • 15
  • 35

1 Answers1

15

Listen for finish event for writable streams. This is fired when end() has been called and processing of the entry is finished. More on it here.

.on('finish', function() {
  console.error('end');
})
Ramon Snir
  • 7,520
  • 3
  • 43
  • 61
hassansin
  • 16,918
  • 3
  • 43
  • 49
  • Hi @hassansin Thanks for the direction! One more question, `The finish and end events are from the parent Writable and Readable classes respectively.` The 1st sentence is saying finish for Writable and end for Readable, right? `The finish event is fired after .end() is called and all chunks have been processed by _transform, end is fired after all data has been output which is after the callback in _flush has been called.` Then why finish is fired after end if they are belong to different things? It's confusing for a rookie like..Thanks very much, – odieatla Aug 05 '15 at 21:10
  • 1
    `finish` is associated with `tar.extract()` which is a writable stream. This writable stream internally maintains several entry `stream`s based on the no. of files in the tar. So when all the entry files are written on disk and `end` is emitted for them, the writer finally emits `finish`. – hassansin Aug 05 '15 at 21:56
  • OK I think I may get it.. So `finish` is emitted after all `end` of the stream are emitted? – odieatla Aug 05 '15 at 22:16
  • What about Duplex streams, i.e. both Readable and Writeable: Should stream.end() call the `end` or `finish` callback ? I need to know the expected behavior when there was neither write nor read from such stream. – martin-g Sep 09 '21 at 13:27