如何使用lambda中的node-js将我的文件添加到现有的zip文件夹?

时间:2018-04-05 09:44:16

标签: node.js lambda zip aws-lambda

我从s3存储桶下载了一个zip文件夹。

现在我的代码中有一个json文件,我想使用节点js代码将我的JSON文件添加到现有的zip文件中。

是否存在任何预先存在的模块,用于在节点js中执行此操作。

我尝试过easy-zip,但我无法将文件添加到现有的zip文件夹中。

对此有什么想法吗?

4 个答案:

答案 0 :(得分:2)

我找不到任何可以内联并修改已经完成的zip的库。这是一种蛮力方法,它需要执行以下步骤:

  1. 创建临时目录
  2. 将邮政编码提取到临时目录中
  3. 删除原始邮政编码
  4. 使用Temp Dir构建存档和种子
  5. 回调以附加任何其他文件
  6. 完成存档
  7. 删除临时目录

它使用extract-zip来解压缩zip,并使用archiver来将其打包备份。

您可以在此仓库KyleMit/append-zip

中查看完整的源代码。

appendZip.js

// require modules
const fs = require('fs');
const fsp = fs.promises
const archiver = require('archiver');
const extract = require('extract-zip')

async function appendZip(source, callback) {
    try {
        let tempDir = source + "-temp"

        // create temp dir (folder must exist)
        await fsp.mkdir(tempDir, { recursive: true })

        // extract to folder
        await extract(source, { dir: tempDir })

        // delete original zip
        await fsp.unlink(source)

        // recreate zip file to stream archive data to
        const output = fs.createWriteStream(source);
        const archive = archiver('zip', { zlib: { level: 9 } });

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

        // append files from temp directory at the root of archive
        archive.directory(tempDir, false);

        // callback to add extra files
        callback.call(this, archive)

        // finalize the archive
        await archive.finalize();

        // delete temp folder
        fs.rmdirSync(tempDir, { recursive: true })

    } catch (err) {
        // handle any errors
        console.log(err)
    }
}

用法

async function main() {
    let source = __dirname + "/functions/func.zip"

    await appendZip(source, (archive) => {
        archive.file('data.json');
    });
}

在回调中,可以使用node-archiver可用的任何方法将文件追加到存档中,例如:

// append a file from stream
const file1 = __dirname + '/file1.txt';
archive.append(fs.createReadStream(file1), { name: 'file1.txt' });

// append a file from string
archive.append('string cheese!', { name: 'file2.txt' });

// append a file from buffer
const buffer3 = Buffer.from('buff it!');
archive.append(buffer3, { name: 'file3.txt' });

// append a file
archive.file('file1.txt', { name: 'file4.txt' });

// append files from a sub-directory and naming it `new-subdir` within the archive
archive.directory('subdir/', 'new-subdir');

// append files from a sub-directory, putting its contents at the root of archive
archive.directory('subdir/', false);

// append files from a glob pattern
archive.glob('subdir/*.txt');

进一步阅读

答案 1 :(得分:0)

您可以关注this链接。

由于上面的方法有点长,唯一的另一种选择是使用node-stream-zip,在读取现有对象后创建一个新对象,添加文件然后重新创建zip。您可以选择删除原始zip。但是,它不会附加'出现的效果。这是一个折衷,你可以轻松地做到这一点,因为涉及流媒体。

答案 2 :(得分:0)

Adm-zip提供了将文件直接添加到压缩文件夹的功能。浏览api时,他们有两种方法可以将文件添加到压缩文件夹中。首先通过从文件系统添加文件,其次通过提供文件名和内容(缓冲区)。

addLocalFile(localPath, zipPath) // Adds a file from the disk to the archive
addFile(entryName, content, comment, attr) // Allows you to programmatically create a entry (file or directory) in the zip file.

我在我的本地计算机上尝试了它,并且在不解压缩现有目录的情况下可以正常工作。但是唯一的问题是添加的文件存在于内存中,如果需要永久查看更改,我们需要将其刷新回文件系统。

// test.zip (contents)
//   - pdfReader.py
//   - white-cuts-on-black-forever.py

var AdmZip = require('adm-zip');
var zip = new AdmZip("test.zip"); // Could also give a buffer here (if it's in memory)

var data = Buffer.from(JSON.stringify({"a": 3, "b": "hello"}));
zip.addFile("data.json", data);   // New file created from contents in memory

zip.addLocalFile("trigger.json"); // Local file in file system

zip.writeZip("test.zip");

// test.zip (contents after program execution)
//   - data.json
//   - trigger.json
//   - pdfReader.py
//   - white-cuts-on-black-forever.py

答案 3 :(得分:0)

对于此任务,我建议使用JSZip。据我了解,您想从zip存储桶中下载S3文件,将您的JSON文档合并到zip的现有结构中(这意味着实际上是打开并读取它)。 / p>

使用JSZip可以通过4个步骤完成