问题
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