我正在尝试使用 Archiverjs 为从 Firebase存储流式传输的多个文件创建一个zip文件,而且已归档的文件也将作为即时响应流式传输到客户端,但在完成后归档结果,即在客户端上下载是不合适的,所有文件都在zip中,但是其中一些文件的大小为0。
const archiver = require("archiver");
const fs = require("fs");
const downloadZip = async ({ items }, response) => {
try {
const archive = archiver("zip");
archive.on("error", err => {
archive.unpipe();
archive.destroy();
console.error("Error :: ", err);
throw err;
});
archive.on("end", () => {
archive.unpipe();
archive.destroy();
console.log("Archiver ends & zip sent.");
});
response.writeHead(200, {
"Content-Type": "application/zip",
"Content-Disposition": `attachment; filename="testItems.zip"`,
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET, POST, OPTIONS"
});
//Piping archive to response so that user can receive the response while archiving
archive.pipe(response);
const bucket = storage.bucket("test-bucket");
const promiseLimit = 5;
//Streaming files in multiple Promise.all and appending it into archiver
while (items.length) {
await Promise.all(
items.splice(0, promiseLimit).map(item => {
return new Promise((resolve, reject) => {
const fileName = item.fileName;
const path = item.path;
if (bucket && fileName) {
//Create read stream of firebase storage file
const readableStream = bucket.file(path).createReadStream();
//Save same files on local, for testing i.e make sure that archived files are same to these files
readableStream.pipe(fs.createWriteStream(fileName));
//Appending Readable stream to archiver
archive.append(readableStream, { name: fileName });
readableStream.on("data", data => {});
readableStream.on("error", error => {
console.error("Downloading error : ", error);
});
readableStream.on("end", () => {
console.error("Downloading complete : ", fileName);
readableStream.destroy();
resolve({});
});
readableStream.on("close", () => {
console.log("Connection closed : ", fileName);
resolve({});
});
} else {
resolve({});
}
});
})
);
}
archive.finalize();
} catch (err) {
console.error("Error : ", err);
}
};
对我来说,似乎无法处理显示文件已完全下载的Firebase事件“结束”。 我也使用单个“全部承诺”来尝试相同的操作,但也无济于事。