首页 > 解决方案 > 使用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();



}

标签: node.jsgoogle-cloud-storage

解决方案


找到了解决方案。其实是粗心大意。

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) 并且它可以工作。


推荐阅读