如何使用pipeline()方法在nodejs中提取tar文件?



我当前的代码是这样的:

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并不是很有帮助。

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

最新更新