节点js文件系统:不为可读流调用结束事件 [英] Node js file system: end event not called for readable stream

查看:28
本文介绍了节点js文件系统:不为可读流调用结束事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试提取 .tar 文件(从目录中打包),然后检查提取目录中文件的名称.我正在使用 tar-fs 来提取 tar 文件,然后使用 fs.createReadStream 来操作数据.这是我到目前为止所得到的:

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..

据我所知,在管道之后我得到了一个可读的流.可读流有结束事件吗?我的问题是,为什么代码中的 end 事件没有被调用?

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?

谢谢!

推荐答案

监听可写流的 finish 事件.当 end() 被调用并且条目的处理完成时会触发它.有关更多信息此处.

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');
})

这篇关于节点js文件系统:不为可读流调用结束事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆