2017-08-09 06:50:59 +00:00
|
|
|
/* cfb.js (C) 2013-present SheetJS -- http://sheetjs.com */
|
|
|
|
/* eslint-env node */
|
|
|
|
/* vim: set ts=2 ft=javascript: */
|
2017-09-14 21:14:22 +00:00
|
|
|
import * as X from 'cfb';
|
2017-07-28 17:53:08 +00:00
|
|
|
import fs = require('fs');
|
|
|
|
import program = require('commander');
|
2017-08-09 06:50:59 +00:00
|
|
|
import PRINTJ = require("printj");
|
2017-07-28 17:53:08 +00:00
|
|
|
program
|
2017-09-14 21:14:22 +00:00
|
|
|
.version(X.version)
|
2017-07-28 17:53:08 +00:00
|
|
|
.usage('[options] <file>')
|
|
|
|
.option('-q, --quiet', 'process but do not report')
|
2017-08-09 06:50:59 +00:00
|
|
|
.option('-l, --list-files', 'list files')
|
2017-07-28 17:53:08 +00:00
|
|
|
.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')
|
2017-08-09 06:50:59 +00:00
|
|
|
.option('--dev', 'development mode')
|
|
|
|
.option('--read', 'read but do not print out contents');
|
|
|
|
|
|
|
|
program.parse(process.argv);
|
2017-07-28 17:53:08 +00:00
|
|
|
|
|
|
|
if(program.args.length === 0 || !fs.existsSync(program.args[0])) {
|
|
|
|
console.error("Usage: " + process.argv[1] + " [-q] <cfb_file>");
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
|
2017-09-14 21:14:22 +00:00
|
|
|
const opts: X.CFBParsingOptions = {type:'file'};
|
2017-08-09 06:50:59 +00:00
|
|
|
if(program.dev) opts.WTF = true;
|
2017-07-28 17:53:08 +00:00
|
|
|
|
2017-09-14 21:14:22 +00:00
|
|
|
const cfb: X.CFBContainer = X.read(program.args[0], opts);
|
2017-08-09 06:50:59 +00:00
|
|
|
if(program.quiet) process.exit(0);
|
2017-07-28 17:53:08 +00:00
|
|
|
|
|
|
|
if(program.dump) {
|
|
|
|
console.log("Full Paths:");
|
|
|
|
console.log(cfb.FullPaths.map((x) => " " + x).join("\n"));
|
|
|
|
console.log("Full Path Directory:");
|
|
|
|
console.log(cfb.FullPathDir);
|
2017-08-09 06:50:59 +00:00
|
|
|
process.exit(0);
|
|
|
|
}
|
2017-09-14 21:14:22 +00:00
|
|
|
if(program.repair) {
|
|
|
|
X.writeFile(cfb, program.args[0]);
|
|
|
|
process.exit(0);
|
|
|
|
}
|
2017-08-09 06:50:59 +00:00
|
|
|
|
2017-09-14 21:14:22 +00:00
|
|
|
const sprintf = PRINTJ.sprintf;
|
|
|
|
function fix_string(x: string): string { return x.replace(/[\u0000-\u001f]/, function($$) { return sprintf("\\u%04X", $$.charCodeAt(0)); }); }
|
|
|
|
if(program.listFiles) {
|
2017-08-09 06:50:59 +00:00
|
|
|
const 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());
|
|
|
|
};
|
|
|
|
|
|
|
|
let basetime = new Date(1980,0,1);
|
2017-09-14 21:14:22 +00:00
|
|
|
let cnt = 0, rootsize = 0, filesize = 0;
|
2017-08-09 06:50:59 +00:00
|
|
|
console.log(" Length Date Time Name");
|
|
|
|
console.log(" -------- ---- ---- ----");
|
2017-09-14 21:14:22 +00:00
|
|
|
cfb.FileIndex.forEach(function(file: X.CFBEntry, i: number) {
|
2017-08-09 06:50:59 +00:00
|
|
|
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])));
|
2017-08-09 06:50:59 +00:00
|
|
|
filesize += file.size;
|
|
|
|
++cnt;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
console.log(" -------- -------");
|
|
|
|
console.log(sprintf("%9lu %lu file%s", rootsize || filesize, cnt, (cnt !== 1 ? "s" : "")));
|
|
|
|
|
|
|
|
process.exit(0);
|
2017-07-28 17:53:08 +00:00
|
|
|
}
|
2017-08-09 06:50:59 +00:00
|
|
|
for(let i=0; i!==cfb.FullPaths.length; ++i) {
|
2017-07-28 17:53:08 +00:00
|
|
|
if(cfb.FullPaths[i].slice(-1) === "/") {
|
2017-09-14 21:14:22 +00:00
|
|
|
console.error("mkdir " + fix_string(cfb.FullPaths[i]));
|
2017-07-28 17:53:08 +00:00
|
|
|
fs.mkdirSync(cfb.FullPaths[i]);
|
|
|
|
} else {
|
2017-09-14 21:14:22 +00:00
|
|
|
console.error("write " + fix_string(cfb.FullPaths[i]));
|
2017-07-28 17:53:08 +00:00
|
|
|
fs.writeFileSync(cfb.FullPaths[i], cfb.FileIndex[i].content);
|
|
|
|
}
|
|
|
|
}
|