我从s3存储桶下载了一个zip文件夹。
现在我的代码中有一个json文件,我想使用节点js代码将我的JSON文件添加到现有的zip文件中。
是否存在任何预先存在的模块,用于在节点js中执行此操作。
我尝试过easy-zip,但我无法将文件添加到现有的zip文件夹中。
对此有什么想法吗?
答案 0 :(得分:2)
我找不到任何可以内联并修改已经完成的zip的库。这是一种蛮力方法,它需要执行以下步骤:
它使用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个步骤完成
loadAsync(data \[, options\])
方法加载zip
。folder(name)
为文件创建一个新文件夹。file(name, data \[,options\])
添加或更新文件。generateNodeStream(options\[, onUpdate\])
可用于生成完整的zip文件作为nodejs流,或使用generateAsync(options\[, onUpdate\])
在当前文件夹级别生成完整的zip文件。