How to zip single file using archive module

43 Views Asked by At

Im trying to zip a single file using archiver and I succeed but resulting zip file contains directory when unzipped and I want just the file.

Here is a sample code:

export async function zipFile(srcFilePath: string, zipFilePath: string) {
  const output = fs.createWriteStream(zipFilePath);
  const archive = archiver('zip',{
    zlib:{
      level: 9
    }
  })

  return new Promise((resolve,reject)=>{
    output.on('close', function() {
      console.log('ARCHIVE FINISHED.');
      resolve(zipFilePath)
    });

    archive.on('error', function(err) {
      reject(err)
    });

    archive.pipe(output);
    archive.file(srcFilePath, {name:path.basename(srcFilePath)})

    archive.finalize();

  })
}

  const zippedFile = await zipFile(
    "/home/rikotech/code/scalewest/repos/hype-nielsen-report/csv/csv.csv",
    "/home/rikotech/code/scalewest/repos/hype-nielsen-report/csv/zipped.zip"
  ); 

The result is zipped.zip and when I unzip it I see a folder named zipped and then iside the folder I have the unzipped file csv.csv.

I want when I unzip the zip file to get a file (the csv file) not a folder and in it the file. enter image description here

0

There are 0 best solutions below