node.js - 使用nodejs归档器下载多个谷歌云存储文件并压缩它们
问题描述
我正在尝试从谷歌云存储下载文件并压缩它们。
async function makeZippedFiles(destination, all_file_links) {
console.log("In the zip file function");
for (let i in all_file_links) {
let name = all_file_links[i]['name']
let archive = archiver('zip', {
zlib: {level: 9} // Sets the compression level.
});
archive.on('error', function (err) {
throw err;
});
let output = fs.createWriteStream(__dirname + `/${name}.zip`);
console.log("loop number", i);
let sourceFile = all_file_links[i]['source'];
console.log(sourceFile, name);
let remoteFile = bucket.file(sourceFile);
let read_file = remoteFile.createReadStream();
await archive.append(read_file, {name: name});
read_file
.on('error', function (err) {
console.log(err);
})
.on('response', function (response) {
console.log("writing file", name);
// console.log(response);
// Server connected and responded with the specified status and headers.
})
.on('end', function () {
console.log("file downloaded", name);
// The file is fully downloaded.
})
archive.pipe(output);
archive.finalize();
}
}
在上面的示例中,我正在遍历所有文件并创建单独的档案。即,如果我下载两个文件,我将创建两个单独的档案。这行得通。
但是,如果我想将所有文件压缩到一个存档中,则会收到以下错误:
找不到中央目录的开始;zip 文件损坏。(请检查您是否以
适当的 BINARY 模式传输或创建了 zipfile,并且您已正确编译 UnZip)
我使用的代码是:
async function makeZippedFiles(destination, all_file_links) {
console.log("In the zip file function");
let archive = archiver('zip', {
zlib: {level: 9} // Sets the compression level.
});
archive.on('error', function (err) {
throw err;
});
let output = fs.createWriteStream(__dirname + `/${destination}.zip`);
for (let i in all_file_links) {
let name = all_file_links[i]['name']
console.log("loop number", i);
let sourceFile = all_file_links[i]['source'];
console.log(sourceFile, name);
let remoteFile = bucket.file(sourceFile);
let read_file = remoteFile.createReadStream();
await archive.append(read_file, {name: name});
read_file
.on('error', function (err) {
console.log(err);
})
.on('response', function (response) {
console.log("writing file", name);
// console.log(response);
// Server connected and responded with the specified status and headers.
})
.on('end', function () {
console.log("file downloaded", name);
// The file is fully downloaded.
})
archive.pipe(output);
}
archive.finalize();
}
解决方案
找到了解决方案。其实是粗心大意。
async function makeZippedFiles(destination, all_file_links) {
console.log("In the zip file function");
let archive = archiver('zip', {
zlib: {level: 9} // Sets the compression level.
});
archive.on('error', function (err) {
throw err;
});
let output = fs.createWriteStream(__dirname + `/${destination}.zip`);
archive.pipe(output);
for (let i in all_file_links) {
let name = all_file_links[i]['name']
console.log("loop number", i);
let sourceFile = all_file_links[i]['source'];
console.log(sourceFile, name);
let remoteFile = bucket.file(sourceFile);
let read_file = remoteFile.createReadStream();
await archive.append(read_file, {name: name});
read_file
.on('error', function (err) {
console.log(err);
})
.on('response', function (response) {
console.log("writing file", name);
// console.log(response);
// Server connected and responded with the specified status and headers.
})
.on('end', function () {
console.log("file downloaded", name);
// The file is fully downloaded.
})
}
archive.finalize();
}
我在 for 循环之前移动了 archive.pipe(output) 并且它可以工作。
推荐阅读
- angular - 仅来自 Angular 的 CORS 错误,而不是不是 Angular 的 Chrome 扩展
- c++ - 用 XOR 乘以字节
- python - 使用用户输入获取来自类 python3 的信息
- scala - 如何将动态数据加载到 cassandra 表中?如何读取带有标题的csv文件?
- python - Numpy:基于单独的向量交换二维数组的值
- javascript - iframe 中的 PDF - 适用于 Firefox,但不适用于 OSX 上的 Chrome
- php - 用于更改 SEO 友好 URL 的 WordPress 查询字符串
- angular - 具有 3 种以上颜色的 Angular 主题
- silverstripe - Silverstripe 安装 - 友好的 URL 不起作用
- r - 使用 Dplyr 将两个 Dataframe 相乘并使用结果创建另一个