【发布时间】:2022-01-06 18:45:49
【问题描述】:
我正在尝试创建一个读取流,然后将 word 文档 XML 文件的内容通过管道传输到写入流,然后从完成的写入流中读取。我遇到的问题是,在第一个读取、写入和读取的序列中,我得到一个[Error: ENOENT: no such file or directory, open] 错误。但是,在第一次尝试创建此文件后,代码运行平稳并按预期返回 pageCount 值。
我试图从完成的文件中读取,然后在“完成”事件中返回 pageCount 值,但这只会给我留下一个未定义的返回值。因此,我不确定该怎么做。
对于这位苦苦挣扎的小辈,我们将不胜感激。
更新,以下代码对我有用。
console.log("unzipping");
const createWordOutput = new Promise((resolve, reject) => {
console.log("end is firing");
fs.createReadStream(data)
.pipe(unzipper.Parse())
.on("entry", async function (entry) {
const fileName = entry.path;
const type = entry.type;
const size = entry.vars.uncompressedSize;
//docProps has the meta data of the document, the word/document.xml is the actual document
if (fileName === "docProps/app.xml") {
// console.log(fileName);
entry.pipe(fs.createWriteStream("./wordOutput")).on("finish", () => {
console.log("finished writing the file");
console.log("resolving");
return resolve();
});
//once the piping is completed and the XML structure is fully writen a 'finish' event is emitted. This event accepts a callback. Here I put the cb and call readTheFile on the ./output file. This successfully reads the metadata of each file
} else {
entry.autodrain();
}
});
});
await createWordOutput;
const pageCount = await readWordFile("./wordOutput");
if (pageCount === undefined) {
console.log("PAGECOUNT IS UNDEFINED");
}
console.log("logging page count in unzip the file");
console.log(pageCount);
return pageCount;
};```
【问题讨论】: