【问题标题】:How do I extract a tar file in nodejs using the pipeline() method?如何使用 pipeline() 方法在 nodejs 中提取 tar 文件?
【发布时间】:2021-10-10 04:14:27
【问题描述】:

我当前的代码如下所示:

const gzip2 = zlib.createUnzip();
const pack2 = new tar.Unpack();
const source2 = Readable.from('public/img.tar.gz');
const destination2 = fs.createWriteStream('public/images2');
pipeline(source2, gzip2, pack2, destination2, (err) => {
    if (err) {
        console.error('An error occurred:', err);
        process.exitCode = 1;
    }
});

但是我收到了这个错误:

internal/streams/pipeline.js:108
  throw new ERR_INVALID_ARG_TYPE(
  ^

TypeError [ERR_INVALID_ARG_TYPE]: The "val" argument must be an instance of Readable, Iterable, or AsyncIterable. Received an instance of Unpack
    at makeAsyncIterable (internal/streams/pipeline.js:108:9)
    at pipeline (internal/streams/pipeline.js:265:15)
    at Object.<anonymous> (/mnt/c/index.js:39:1)
    at Module._compile (internal/modules/cjs/loader.js:1068:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1097:10)
    at Module.load (internal/modules/cjs/loader.js:933:32)
    at Function.Module._load (internal/modules/cjs/loader.js:774:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
    at internal/main/run_main_module.js:17:47 {
  code: 'ERR_INVALID_ARG_TYPE'
}

我也试过

const pack2 = tar.extract();

没有运气。 如果有人可以帮助我使用管道提取此 tar 文件,那么我也可以解压缩该文件。我会很感激。我见过人们在网上使用 .pipe(tar.extract()) ,但我试图让我的代码尽可能的干净和统一。这个:https://www.npmjs.com/package/tar 很遗憾没有提供多大帮助。

【问题讨论】:

    标签: node.js pipeline tar unzip


    【解决方案1】:
    const tar = require('tar.gz2')
    var read = tar().createReadStream('/public/images');
    var write = fs.createWriteStream('compressed.tar.gz');
    pipeline(read,write,  
       (err) => {
       if (err) {
        throw err
       }
    })
    

    【讨论】:

    • 我收到这个错误,“internal/streams/pipeline.js:77 throw new ERR_INVALID_CALLBACK(streams[streams.length - 1]); ^ TypeError [ERR_INVALID_CALLBACK]: 回调必须是一个函数. 在 Object. ( /mnt/c/Users/Ethan/Dropbox/Koding/WebDev/MyWebsite/platform-land/index.js:52:1) { code: 'ERR_INVALID_CALLBACK' }" 还有更多,但是太长了,放不下。
    • 无论如何我都会给你赏金,只是因为我不想浪费它。
    • 编辑的回调函数,现在应该可以工作了
    • 我现在收到此错误,“错误错误:Zlib.zlibOnError [as onerror] (zlib.js:181:17) { errno: -5, code:' Z_BUF_ERROR' }" 感谢您继续为此工作,看来您是我唯一的希望。
    • 完成,伊桑。如果有任何错误,请告诉我。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多