Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node js file system: end event not called for readable stream

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!

like image 840
odieatla Avatar asked Aug 05 '15 02:08

odieatla


1 Answers

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');
})
like image 65
hassansin Avatar answered Nov 14 '22 22:11

hassansin