From cf5e98b6ea69590b169b4c0db77c8392cecc058c Mon Sep 17 00:00:00 2001 From: sharoninator Date: Thu, 8 Aug 2024 18:21:17 +0300 Subject: [PATCH] Added helper compress function. --- lib/core.js | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/lib/core.js b/lib/core.js index 7c0a74d7..fd23abc8 100644 --- a/lib/core.js +++ b/lib/core.js @@ -593,6 +593,30 @@ Archiver.prototype.append = function(source, data) { return this; }; +/** + * A general compress function that checks whether the given path is a file or directory + * and adds data to the archive accordingly, then compresses it. + * + * @param {String} [inputPath] Path of file or folder of data to be compressed. + * @param {String} [outputPath] Path of where to save the archive. + * @return {this} + */ +Archiver.prototype.compress = function(inputPath, outputPath) { + const output = fs.createWriteStream(outputPath); + this.pipe(output); + fs.stat(inputPath, (err, stats) => { + if (err) { + return; + } + if (stats.isDirectory()) { + this.directory(inputPath); + } else if (stats.isFile()) { + this.file(inputPath, { name: path.basename(inputPath) }); + } + this.finalize(); + }); +}; + /** * Appends a directory and its files, recursively, given its dirpath. *