I found it easiest to wrap my own wrapper around 7-zip, but you can also easily use zip or any other zip command-line tool available in your runtime. This particular module just does one thing: archive the directory.
const { spawn } = require('child_process'); const path = require('path'); module.exports = (directory, zipfile, log) => { return new Promise((resolve, reject) => { if (!log) log = console; try { const zipArgs = ['a', zipfile, path.join(directory, '*')]; log.info('zip args', zipArgs); const zipProcess = spawn('7z', zipArgs); zipProcess.stdout.on('data', message => { // received a message sent from the 7z process log.info(message.toString()); }); // end the input stream and allow the process to exit zipProcess.on('error', (err) => { log.error('err contains: ' + err); throw err; }); zipProcess.on('close', (code) => { log.info('The 7z exit code was: ' + code); if (code != 0) throw '7zip exited with an error'; // throw and let the handler below log it else { log.info('7zip complete'); return resolve(); } }); } catch(err) { return reject(err); } }); }
Use it this way if you saved the above code in zipdir.js . The third parameter to log is optional. Use it if you have your own registrar. Or completely delete my nasty log entries.
const zipdir = require('./zipdir'); (async () => { await zipdir('/path/to/my/directory', '/path/to/file.zip'); })();
Todd Price May 02 '19 at 11:00 2019-05-02 11:00
source share