js-cfb/bin/cfb.njs

81 lines
2.7 KiB
Plaintext
Raw Normal View History

2013-09-05 18:55:36 +00:00
#!/usr/bin/env node
/* cfb.js (C) 2013-present SheetJS -- http://sheetjs.com */
/* eslint-env node */
/* vim: set ts=2 ft=javascript: */
var X = require('../');
2017-09-14 21:14:22 +00:00
var fs = require('fs');
var program = require('commander');
var PRINTJ = require("printj");
program
.version(X.version)
.usage('[options] <file>')
.option('-q, --quiet', 'process but do not report')
.option('-l, --list-files', 'list files')
.option('-d, --dump', 'dump internal representation but do not extract')
2017-09-14 21:14:22 +00:00
.option('-r, --repair', 'attempt to repair and garbage-collect archive')
.option('--dev', 'development mode')
.option('--read', 'read but do not print out contents');
program.parse(process.argv);
2013-09-05 18:55:36 +00:00
if(program.args.length === 0 || !fs.existsSync(program.args[0])) {
console.error("Usage: " + process.argv[1] + " [-q] <cfb_file>");
process.exit(1);
2013-09-05 18:55:36 +00:00
}
var opts = ({type:'file'}/*:any*/);
if(program.dev) opts.WTF = true;
var cfb = X.read(program.args[0], opts);
if(program.quiet) process.exit(0);
if(program.dump) {
console.log("Full Paths:");
console.log(cfb.FullPaths.map(function(x) { return " " + x; }).join("\n"));
console.log("Full Path Directory:");
console.log(cfb.FullPathDir);
process.exit(0);
}
2017-09-14 21:14:22 +00:00
if(program.repair) {
X.writeFile(cfb, program.args[0]);
process.exit(0);
}
2017-09-14 21:14:22 +00:00
var sprintf = PRINTJ.sprintf;
function fix_string(x/*:string*/)/*:string*/ { return x.replace(/[\u0000-\u001f]/, function($$) { return sprintf("\\u%04X", $$.charCodeAt(0)); }); }
if(program.listFiles) {
var format_date = function(date/*:Date*/)/*:string*/ {
return sprintf("%02u-%02u-%02u %02u:%02u", date.getUTCMonth()+1, date.getUTCDate(), date.getUTCFullYear()%100, date.getUTCHours(), date.getUTCMinutes());
};
var basetime = new Date(1980,0,1);
2017-09-14 21:14:22 +00:00
var cnt = 0, rootsize = 0, filesize = 0;
console.log(" Length Date Time Name");
console.log(" -------- ---- ---- ----");
2017-09-14 21:14:22 +00:00
cfb.FileIndex.forEach(function(file, i/*:number*/) {
switch(file.type) {
case 5:
basetime = file.ct || file.mt || basetime;
rootsize = file.size;
break;
case 2:
2017-09-14 21:14:22 +00:00
console.log(sprintf("%9lu %s %s", file.size, format_date(basetime), fix_string(cfb.FullPaths[i])));
filesize += file.size;
++cnt;
}
});
console.log(" -------- -------");
console.log(sprintf("%9lu %lu file%s", rootsize || filesize, cnt, (cnt !== 1 ? "s" : "")));
process.exit(0);
2013-09-05 18:55:36 +00:00
}
for(var i=0; i!==cfb.FullPaths.length; ++i) {
if(cfb.FullPaths[i].slice(-1) === "/") {
2017-09-14 21:14:22 +00:00
console.error("mkdir " + fix_string(cfb.FullPaths[i]));
fs.mkdirSync(cfb.FullPaths[i]);
2013-09-05 18:55:36 +00:00
} else {
2017-09-14 21:14:22 +00:00
console.error("write " + fix_string(cfb.FullPaths[i]));
fs.writeFileSync(cfb.FullPaths[i], /*::new Buffer((*/cfb.FileIndex[i].content/*:: :any))*/);
2013-09-05 18:55:36 +00:00
}
}