gpt4 book ai didi

node.js - 使用 NodeJS 打包 epub

转载 作者:太空宇宙 更新时间:2023-11-04 01:55:50 24 4
gpt4 key购买 nike

我正在开发一个小型 Nodejs 应用程序,它可以解压 epub、修改标题,然后重新打包。

拆包和修改工作。当我使用 npm 包“archiver”重新打包时,存档文件似乎已损坏。它无法由电子阅读器打开或由压缩实用程序解包。

如果我在独立的 js 文件中使用相同的代码打包目录,它可以正常工作。

在主应用程序内,我使用以下代码来调用每个模块(解压缩、重命名、重新压缩)。据我所知,这是此应用程序中的代码(不起作用)与独立文件中的代码(起作用)之间的唯一区别。

var unzip = require('../services/unzip');
var replacetitle = require('../services/replacetitle');
var rezip = require('../services/rezip');

fileObject = {};
fileObject.unzipPath = "/Users/me/Downloads/myPath/";
fileObject.filePath = "/Users/me/Downloads";
fileObject.novelTitle = "myFile";

unzip.unzip(fileObject)
.then(
replacetitle.replacetitle(fileObject)
).then(
rezip.rezip(fileObject)
);

这是来自 rezip.js 的代码(不起作用):

var fs = require("fs");
var archiver = require("archiver");

module.exports = {
rezip: function(fileObject){

var path = fileObject.unzipPath;
var outputPath = fileObject.filePath + '/' + fileObject.novelTitle + '.epub';
var output = fs.createWriteStream(outputPath);
var archive = archiver("zip");

// pipe archive data to the file
archive.pipe(output);

// add the mimetype file. This MUST be the first file added
archive.file(path + "mimetype", { name: "mimetype" });

// append the other files and folders
var files = fs.readdirSync(path);

// Loop through each item in the directory, determine if
// it is a file or a directory and append accordingly
function fileAppender(fileIndex) {
if (fileIndex < files.length) {
var stat = fs.statSync(path + files[fileIndex]);
if (stat.isFile()) {
if (
files[fileIndex] !== "mimetype" &&
files[fileIndex] !== ".DS_Store"
) {
archive.file(path + files[fileIndex], { name: files[fileIndex] });
}
} else if (stat.isDirectory()) {
archive.directory(path + files[fileIndex], files[fileIndex]);
}
fileAppender(fileIndex + 1);

} else {
// finalize the archive (ie we are done appending files but streams have to finish yet)
// 'close', 'end' or 'finish' may be fired right after calling this method so register to them beforehand
archive.finalize();
return new Promise(function(resolve, reject){
resolve(fileObject);
})
}
}
fileAppender(0);
}
}

下面是来自独立文件的有效代码。在调试器中观察其中的每一个,它们以相同的方式执行,只有一个输出可以使用电子阅读器或存档实用程序打开的文件,而另一个输出似乎已损坏的文件。

fileObject = {
path: "/Users/me/Downloads/myPath/",
outputPath: "/Users/me/Downloads/myPath/myFile.epub"
}
var fs = require("fs");
var archiver = require("archiver");

var path = fileObject.path;
var outputPath = fileObject.outputPath;
var output = fs.createWriteStream(outputPath);
var archive = archiver("zip");

// pipe archive data to the file
archive.pipe(output);

// add the mimetype file. This MUST be the first file added
archive.file(path + "mimetype", { name: "mimetype" });

// append the other files and folders
var files = fs.readdirSync(path);

// Loop through each item in the directory, determine if
// it is a file or a directory and append accordingly
function fileAppender(fileIndex) {
if (fileIndex < files.length) {
var stat = fs.statSync(path + files[fileIndex]);
if (stat.isFile()) {
if (
files[fileIndex] !== "mimetype" &&
files[fileIndex] !== ".DS_Store"
) {
archive.file(path + files[fileIndex], { name: files[fileIndex] });
}
} else if (stat.isDirectory()) {
archive.directory(path + files[fileIndex], files[fileIndex]);
}
fileAppender(fileIndex + 1);
} else {
// finalize the archive (ie we are done appending files but streams have to finish yet)
// 'close', 'end' or 'finish' may be fired right after calling this method so register to them beforehand
archive.finalize();
}
}
fileAppender(0);

我已经测试过设置路径而不是将它们传递到 fileObject 对象中,但我得到了相同的结果。

我是 Nodejs 新手。

最佳答案

问题是由于没有正确处理 promise 造成的。如果我不使用 promise 链而只是将它们作为单独的脚本手动运行,那么一切都会正常。看起来我真的只是需要更加熟悉 Promise。

关于node.js - 使用 NodeJS 打包 epub,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48026104/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com