feat: scripts
This commit is contained in:
95
scripts/split.js
Normal file
95
scripts/split.js
Normal file
@@ -0,0 +1,95 @@
|
||||
#! /usr/bin/env runjs
|
||||
|
||||
var argsjs = require('component-args.js');
|
||||
|
||||
var parseSize = (s) => {
|
||||
if (s == null) { return $$.num(0); }
|
||||
s = s.toLowerCase();
|
||||
if (!(/^\d+[kmg]?$/i.test(s))) {
|
||||
throw 'Size format error: ' + s;
|
||||
}
|
||||
if (s.endsWith('k')) {
|
||||
return $$.num(s.substring(0, s.length - 1)).mul(1024);
|
||||
}
|
||||
if (s.endsWith('m')) {
|
||||
return $$.num(s.substring(0, s.length - 1)).mul(1024).mul(1024);
|
||||
}
|
||||
if (s.endsWith('g')) {
|
||||
return $$.num(s.substring(0, s.length - 1)).mul(1024).mul(1024).mul(1024);
|
||||
}
|
||||
return $$.num(s);
|
||||
};
|
||||
|
||||
var main = () => {
|
||||
var args = argsjs.parseDefARGs(['h', 'help']);
|
||||
if ((args.flg('h', 'help')) || (args.length == 0)) {
|
||||
println('split.js <file.ext>');
|
||||
println(' -h, --help Print help');
|
||||
println(' -s, --size File size(default 20m)');
|
||||
println(' -p, --prefix File prefix(default filename + ".")');
|
||||
println(' --suffix File suffix(default "")');
|
||||
return;
|
||||
}
|
||||
var file = $$.file(args[0]);
|
||||
if (!file.exists()) {
|
||||
xprintln('[ERROR] File not exists: ' + file);
|
||||
return;
|
||||
}
|
||||
var size = parseSize(args.val('s', 'size') || '20m');
|
||||
xprintln('[INFO] Split size: ' + size);
|
||||
|
||||
if (size.compareTo(0) < 0) {
|
||||
xprintln('[ERROR] Split file size cannot less than 0: ' + size);
|
||||
return;
|
||||
}
|
||||
if (size.compareTo(file.length()) >= 0) {
|
||||
xprintln('[ERROR] Split file size larger or equals then origin file, ' + size + ' vs ' + file.length());
|
||||
return;
|
||||
}
|
||||
|
||||
var prefix = args.val('p', 'prefix') || file.getName();
|
||||
if (!prefix.endsWith('.')) { prefix += '.'; }
|
||||
xprintln('[INFO] Split file prefix: ' + prefix);
|
||||
var suffix = args.val('suffix') || '';
|
||||
if (!suffix.startsWith('.') && ($STR(suffix) != '')) { suffix = '.' + suffix; }
|
||||
xprintln('[INFO] Split file suffix: ' + (($STR(suffix) == '') ? '(empty)' : suffix));
|
||||
|
||||
var fileIndex = 0;
|
||||
var writted = $$.num(0);
|
||||
var buff = $$.byteArray(1024);
|
||||
|
||||
var fnList = [];
|
||||
var fn = prefix + $$.numFormat('000').format(fileIndex) + suffix;
|
||||
if ($$.file(fn).exists()) {
|
||||
xprintln('[ERROR] File exists: ' + fn);
|
||||
return;
|
||||
}
|
||||
|
||||
fnList.push(fn);
|
||||
xprintln('[INFO] Write file: ' + fn);
|
||||
var os = new java.io.BufferedOutputStream(new java.io.FileOutputStream($$.file(fn)));
|
||||
var is = $$.rFile(file).rStream().buffered().stream();
|
||||
for (var len; ((len = is.read(buff)) != -1);) {
|
||||
os.write(buff, 0, len);
|
||||
writted = writted.add(len);
|
||||
if (writted.compareTo(size.mul(fileIndex + 1)) >= 0) {
|
||||
os.close();
|
||||
fileIndex++;
|
||||
if (writted.compareTo(file.length()) < 0) {
|
||||
fn = prefix + $$.numFormat('000').format(fileIndex) + suffix;
|
||||
fnList.push(fn);
|
||||
xprintln('[INFO] Write file: ' + fn);
|
||||
os = new java.io.BufferedOutputStream(new java.io.FileOutputStream($$.file(fn)));
|
||||
} else {
|
||||
os = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (os != null) { os.close(); }
|
||||
println();
|
||||
xprintln('[OK] Use command to join file:\ncat ' + fnList.join(' \\\n ') + ' \\\n > ' + file.getName());
|
||||
println();
|
||||
xprintln('[INFO] Split file done!');
|
||||
};
|
||||
|
||||
main();
|
||||
Reference in New Issue
Block a user