Skip to content

Instantly share code, notes, and snippets.

@armornick
Last active August 29, 2015 14:17
Show Gist options
  • Save armornick/d96777a7dac583d252ab to your computer and use it in GitHub Desktop.
Save armornick/d96777a7dac583d252ab to your computer and use it in GitHub Desktop.
7-zip a whole bunch of directories in a directory
// Module imports
var _ = require('underscore'),
test = require('shelljs').test,
which = require('shelljs').which,
listdir = require('shelljs').ls,
exec = require('shelljs').exec;
// suppress output of commands
require('shelljs').config.silent = true;
// Parse commandline arguments
var argv = process.argv.slice(2);
var zipFormat = _.contains(argv, '-zip');
argv = _.filter(argv, function(arg) { return !(_.contains(['-zip'], arg)); })
// Check 7za is on the PATH
if (!which('7za')) { throw new Error("7-zip archiver not found on the PATH.") };
// Build compression function
var compress = (function (zipFormat) {
var ext = zipFormat ? '.zip' : '.7z',
typeSwitch = zipFormat ? '-tzip' : '';
return function (item) {
console.log('compressing '+item+' to '+item+ext);
exec('7za a '+typeSwitch+' '+item+ext+' '+item);
}
})(zipFormat);
// Compress every folder in the given directory
function compressFolders (directory) {
if (!test('-d', directory)) { throw new Error("argument '"+directory+"' is not a directory") };
listdir(directory+'/*').forEach(function (item) {
if (test('-d', item)) {
// console.log('directory: ', item);
compress(item);
};
});
}
// Compress folders in directories given as arguments
_.each(argv, compressFolders);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment