Add files to zip and download cpu usage

87 Views Asked by At

I'm using express js as a backend.

Each user can upload 3/5 files. and the admin can download the files from the dashboard as zip file.

I'm using ADM-ZIP to zip the files and download the zip.

 const zip = new AdmZip();
   const uploadDir = await fsPromises.readdir("./static/uploads/applications/"+user.id);

  for(var i = 0; i < uploadDir.length;i++){
    zip.addLocalFile('./static/uploads/applications/'+user.id+'/'+uploadDir[i]);
  }
 
  const downloadName = `${user.name}.zip`;

  const data = await zip.toBuffer();

  res.set('Content-Type','application/octet-stream');
  res.set('Content-Disposition',`attachment; filename=${downloadName}`);
  res.set('Content-Length',data.length);
 return  res.send(data);

the code works fine. but I noticed there is high CPU usage when pressing download.

is this normal to happen in express? is this approach good for zipping multiple files and downloading them?

I'm new to express so I'm a bit worried about having problems taking this approach.

0

There are 0 best solutions below