我使用 node-archiver 来压缩文件,如下所示。但我得到损坏的 zip 文件。
app.get('/download', async (req, res) => {
const arch = archiver('zip');
arch.append('abc', { name: 'abc.txt'});
arch.append('cdf', { name: 'cdf.txt'});
res.attachment('test.zip').type('zip');
arch.pipe(res);
arch.finalize();
});
我修改了代码直接保存到文件系统。使用此代码,我得到了在文件系统上创建的工作 zip 文件。
app.get('/download', async (req, res) => {
const arch = archiver('zip');
arch.append('abc', { name: 'abc.txt'});
arch.append('cdf', { name: 'cdf.txt'});
const output = fs.createWriteStream('./test.zip');
arch.pipe(output);
arch.finalize();
});
为什么通过 expressres
对象发送 zip 文件时会损坏?解决办法是什么?
编辑:
如果我使用输出格式tar
而不是 zip 它可以正常工作。
const arch = archiver('tar');