Node.js和zip存档中的更新文件 [英] Nodejs and update file inside zip archive

查看:73
本文介绍了Node.js和zip存档中的更新文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 nodejs 在zip归档文件中更新文件.例如,我有两个文件的zip文件:

I want update file in zip arhive with nodejs. For example i have zip file with two files:

 a.zip
   |-a.txt
   |-b.txt 

我使用存档器:

var archiver = require('archiver');
var archive = archiver('zip', {});
archive.pipe(fs.createWriteStream('./a.zip'));
archive.append(fs.createReadStream('./c.txt'), { name: 't.txt' });
archive.finalize();

但是我有一个问题,我的存档已被完全覆盖.结果,我得到:

But I have a problem, my archive is completely overwritten. As a result, I get:

 a.zip
   |-t.txt

如果我使用:

archive.file('./a.txt', { name: 't.txt' });

结果保持不变.我想得到这样的结构

the result remains the same. And I want to get this structure as a result

 a.zip
   |-a.txt
   |-b.txt
   |-t.txt

或更新文件 a.txt b.txt

推荐答案

当前无法使用 archiver 附加到现有存档.以下是2013年提出的相关问题:支持附加到现有#23档案中

Appending to an existing archive is currently not possible with archiver. Here's the relevant issue opened in 2013: support appending to existing archives #23

在您的情况下, append 会追加到 stream ,而不是追加到新文件的目标位置. archiver 并不关心 a.zip 中的内容,它只是用您给 archiver.append 提供的任何内容覆盖它.

In your case, append is appending to the stream, not to whatever was at the destination of the new file. archiver doesn't care about what's at a.zip, it just overwrites it with whatever you give to archiver.append.

似乎最好的选择是使用 maxogden/ extract-zip ,然后使用 archiver 将结果附加到新版本的存档中.

It seems like your best bet would be to unzip the existing file with maxogden/extract-zip, then append the results to the new version of the archive with archiver.

这篇关于Node.js和zip存档中的更新文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆