I'm trying to serve a zip file I created with archiver library on my next js project. Eventhough the file I created is healthy, I can't open the file I downloaded because it is corrupted.
this is how I created my zip file
function zipDirectory(sourceDir, outPath) {
const archive = archiver('zip', { zlib: { level: 9 } });
const stream = fs.createWriteStream(outPath);
return new Promise((resolve, reject) => {
archive
.directory(sourceDir, false)
.on('error', (err) => reject(err))
.pipe(stream);
stream.on('close', () => resolve());
archive.finalize();
});
}
this is how I serve the file
import fs from 'fs';
export default async function handler(req, res) {
const { filePath, fileName } = req.body;
const fullPath = `${filePath}/${fileName}`;
res.setHeader('Content-Type', 'application/zip');
res.setHeader('Content-Disposition', `attachment; filename=${fileName}`);
let fileStream = fs.createReadStream(fullPath);
fileStream.pipe(res);
}
Note : when I check the file size in handler function, it gives different results each time even though the file is same.