daraosn / node-zip

217 stars 38 forks source link

Unable to clear Cache from ZIP #36

Closed PratikThorve closed 6 months ago

PratikThorve commented 6 months ago

Hello Team,

I wrote the below piece of code to zip a file and then delete once the same is done. At first instance it works fine , and create the zip. But for 2nd instance it zips the new new file and also add the previous file to the bundle , so now in my zip i have 2 file. Please note i am also deleting the files physically from disk. Can you please confim on this if this a bug / or how can clear the cache / in-memory of ZIP before being called again.

  const zip = require('node-zip')();
  zip.file(`${resultFileName}`, fs.readFileSync(resultFilePath));
  const data = zip.generate({ base64: false, compression: 'DEFLATE' });
  fs.writeFileSync(path.join(resultDirectory, `${date}.zip`), data, 'binary');

     if (fs.existsSync(resultFilePath)) {
          fs.unlinkSync(resultFilePath);
        }
        if (fs.existsSync(path.join(resultDirectory, `${date}.zip`))) {
          fs.unlinkSync(path.join(resultDirectory, `${date}.zip`));
        }
PratikThorve commented 6 months ago

Figured out the solution

const zip = require('node-zip')(); was declared at top of the file hence was global and shared.

Once i moved the same to my function it worked, nothing was cached. / kept in memory.