archive folder using archivejs library

224 Views Asked by At

Im trying to archive my folder that contains multiple files using archivejs library.

As param, Im sending absolute path to the dir that contains my files. I would like to get zipped directory with files in the root of archive. Im creating zip file using createWriteStream. From some reason it does not work properly.

What I want to achieve:

lets say I have folder download where are my files I would like to archive. then Im creating download.zip where I would like to have all my files in the root of archive.

Files weren't written in my zip file means it becomes empty. Where should be the problem ?

function createArchiveZip(destinationDir) {
      const archive = archiver("zip", { zlib: { level: 9 } }); 
      const archiveDir = `${destinationDir}.zip`;
      const zipOutput = fs.createWriteStream(archiveDir);
      console.log("archiveDirarchiveDir",archiveDir);

      zipOutput.on("error", writeStreamError => {
        console.log("writeStreamError", writeStreamError);
      });
      zipOutput.on("end", writeStreamEnd => {
        console.log("writeStream finish", writeStreamEnd);
      });
      archive.on("warning", archiveWarnings => {
        console.log("archiveWarnings", archiveWarnings);
      });
      archive.on("error", archiveErr => {
        console.log("archiveErr", archiveErr);
      });
      archive.pipe(zipOutput);
      archive.directory(destinationDir, false);
      archive.finalize();
      return archiveDir;
    }
0

There are 0 best solutions below