Nodejs and update file inside zip archive

余生颓废 提交于 2021-02-19 08:01:42

问题


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

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

I use archiver:

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

If I use:

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

Or update the contents of one of the files a.txt or b.txt


回答1:


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

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.

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.



来源:https://stackoverflow.com/questions/55191756/nodejs-and-update-file-inside-zip-archive

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!