ソースコード (select2zip.zip をダウンロード)
/**
* select2zip
* 選択中のファイルを7zipで圧縮するMDIE用スクリプト
*
* @author mizugame (html://picsmate.net/)
* @version 0.7.6 (2008-12-19)
* @license GPL
*/
var $files = [];
for (var i=0; i<FolderView.count; i++)
if (FolderView.Items(i).Selected) $files.push(FolderView.Items(i).Path);
var $setting = {},
$system = $setting.$system = {},
$option = $setting.$option = {};
/**************************************************
* 設定ここから
**************************************************/
/*
* システム設定(7-zipの位置とか、ログファイルの場所を指定)
* $system.exe : 7zipプログラムのパス
* $system.logfile : ログファイルのパス(設定しない場合は、実行したディレクトリ内に自動作成)
* $system.zipMode : 圧縮タイプ(デフォルトはzip)
* $system.skipExt : 圧縮しないファイル拡張子($option.skipZipがtrueの場合のみ有効)
*/
$system.exe = '';
$system.logfile = '';
$system.zipMode = 'zip';
$system.skipExt = ['lha','zip','cab','rar','7z','gz','bz2','tar'];
/*
* オプション設定(圧縮対象の設定とか、ログを残すかとか)
* $option.pressDir : 選択したフォルダを圧縮するか(圧縮する:true, 圧縮しない:false)
* $option.pressFile : 選択したファイルを圧縮するか(圧縮する:true, 圧縮しない:false)
* $option.saveLog : 実行ログを記録するか(記録する:true, 記録しない:false)
* $option.skipZip : 選択ファイルがZIPの場合、重複圧縮を回避するか(回避する:true, 回避しない:false)
* $option.removeExt : ファイル圧縮時に拡張子を外すか(外す:true, 残す:false)
* $option.keepAction : ユーザによる圧縮中断後、残りの圧縮を継続するか(継続する:true, 継続しない:false)
* $option.finish : 終了時のアラート(アラートする:true, アラートしない:false)
*/
$option.pressDir = true;
$option.pressFile = true;
$option.saveLog = true;
$option.skipZip = true;
$option.removeExt = true;
$option.keepAction = false;
$option.finish = false;
/**************************************************
* 設定ここまで
**************************************************/
(function()
{
if ($files.length == 0) {
MDIE.echo('ファイルを選択してください');
return;
}
/*
* setup
*/
var $fso = new ActiveXObject('Scripting.FileSystemObject'),
$shell = new ActiveXObject('WScript.Shell');
/*
* common method
*/
var $common = {};
/**
* $common.time
* @return {string}[hh:mn:ss.milliseconds]
*/
$common.time = function()
{
var date = new Date();
return date.getHours() + ':' +
date.getMinutes() + ':' +
date.getSeconds() + '.' +
date.getMilliseconds();
};
/**
* $common.timestamp
* @return {string}[yyyy年m(m)月d(d)日 hh:mn:ss]
*/
$common.timestamp = function()
{
return (new Date()).toLocaleString();
};
/**
* $common.parseFile
* @param {string} path
* @return {object}{path, dir, file, name, ext}
*/
$common.parseFile = function(path)
{
return {
path : path,
dir : $fso.GetParentFolderName(path),
file : $fso.GetFileName(path),
name : $fso.GetBaseName(path),
ext : $fso.GetExtensionName(path)
};
};
/**
* $common.uniquePath
* @param {string} baseDir
* @param {string} baseName
* @param {string} ext
* @return {string}
*/
$common.uniquePath = function(baseDir, baseName, ext)
{
var fileExt = (ext.length > 0 ? '.' + ext : ''),
path = $fso.BuildPath(baseDir, baseName + fileExt);
if ($fso.FileExists(path))
for (var j=1; $fso.FileExists(path); j++)
path = $fso.BuildPath(baseDir, baseName + '(' + j + ')' + fileExt);
return path;
};
/*
* filestream class
*/
var $file = function(path)
{
this.$handle = this.open(path);
};
/**
* $file.prototype.open
* @param {string} path
* @return {object}[textStream]
*/
$file.prototype.open = function(path)
{
return $fso.FileExists(path) ?
$fso.GetFile(path).OpenAsTextStream(8) :
$fso.CreateTextFile(path, false);
};
/**
* $file.prototype.write
* @param string {string}
* @return object[$file {object}] | false
*/
$file.prototype.write = function(string)
{
try {
this.$handle.Write(string);
} catch(e) {
return false;
}
};
/**
* $select2zip
*/
var $select2zip = function(setting)
{
if (typeof setting == 'object') {
this.$system = {
exe : (setting.$system.exe == undefined ?
this.$default.$system : setting.$system).exe,
logfile : (setting.$system.logfile == undefined ?
this.$default.$system : setting.$system).logfile,
zipMode : this.zipCase(setting.$system.zipMode),
skipExt : this.skipList(setting.$system.skipExt)
};
this.$option = setting.$option;
for (var i in this.$default.$option)
if (this.$option[i] == undefined) this.$option[i] = this.$default.$option[i];
} else {
this.$option = $default.setting;
}
this.$result = {
count : 0,
success : 0,
fault : 0,
skip : 0
};
this.$path = {};
this.$path.log = $fso.FolderExists($fso.GetParentFolderName(this.$option.logfile)) ?
this.$option.logfile : $fso.BuildPath($fso.GetParentFolderName($files[0]), 'select2zip.log');
/* log file stream */
if (this.$option.saveLog == true) {
this.$log = new $file(this.$path.log);
this.startMessage();
}
};
/**
* $select2zip.$default
* default setting
*/
$select2zip.$default = $select2zip.prototype.$default = {
$system : {
exe : '%PROGRAMFILES%\\7z\\7z.exe',
logfile : '',
zipMode : 'zip',
skipExt : ['lha','zip','7z']
},
$option : {
pressDir : true,
pressFile : true,
saveLog : true,
skipZip : true,
keepAction : false,
finish : true
}
};
/**
* $select2zip.$errorCode
* 7-zip Error Code
*/
$select2zip.$errorCode = $select2zip.prototype.$errorCode = {
NO_ERROR : 0,
WARNING : 1,
FATAL : 2,
SYNTAX : 7,
MEMORY : 8,
CANCEL : 255
};
/**
* $select2zip.$skipCode
* select2zip Skip Code
*/
$select2zip.$skipCode = $select2zip.prototype.$skipCode = {
NO_SKIP : 0,
FOLDER : 1,
FILE : 2,
ZIP : 4
};
/**
* $select2zip.reportText
* @param {string} value
*/
$select2zip.prototype.reportText = function(value)
{
if (typeof value == 'string' && this.$option.saveLog == true)
this.$text.push(value);
return this;
};
/**
* $select2zip.reportWrite
* @param {string} value
*/
$select2zip.prototype.reportWrite = function(value)
{
if (this.$option.saveLog != true) return;
this.reportText(value);
if (this.$text.length > 0) {
this.$log.write(this.$text.join('\r\n') + '\r\n');
this.reportClear();
}
return this;
};
/**
* $select2zip.reportClear
*/
$select2zip.prototype.reportClear = function(value)
{
this.$text = [];
return this;
};
/**
* $select2zip.zipCase
* @param {string} ext
* @return {object} [zipInfo]
*/
$select2zip.prototype.zipCase = function(ext)
{
switch (ext.toLowerCase())
{
case '7z' : return {type:'7z', ext:'7z'};
case 'tar' : return {type:'tar', ext:'tar'};
case 'gz' :
case 'gzip' : return {type:'gzip', ext:'gz'};
case 'bz2' :
case 'bzip' :
case 'bzip2': return {type:'bzip2',ext:'bz2'};
default : return {type:'zip', ext:'zip'};
}
};
/**
* $select2zip.skip
* @param {string} ext
* @return {boolean}
*/
$select2zip.prototype.skip = function(ext)
{
return (ext && this.$option.pressDir === false) ? this.$skipCode.FOLDER :
(ext && this.$option.pressFile === false) ? this.$skipCode.FILE :
(this.$option.skipZip == true &&
this.$system.skipExt.indexOf( '<' + ext.toLowerCase() + '>') != -1) ?
this.$skipCode.ZIP : this.$skipCode.NO_SKIP;
};
/**
* $select2zip.skipList
* @param {array|string} list
* @return {string}
*/
$select2zip.prototype.skipList = function(list)
{
if (list instanceof Array) {
for (var i=0, types=''; i<list.length; i++)
types += '<' + list[i].toLowerCase() + '>';
} else {
var types = (typeof list == 'string') ?
'<' + list.toLowerCase() + '>' : '';
}
return types;
};
/**
* $select2zip.startMessage
*/
$select2zip.prototype.startMessage = function()
{
return this.reportClear()
.reportWrite('#select2zipスクリプトの起動: [date ' +
$common.timestamp() + ']');
};
/**
* $select2zip.exitMessage
* @param {string} code[exit code]
*/
$select2zip.prototype.exitMessage = function(code)
{
this.$result.count++;
this.$result[code > 0 ? 'fault' : 'success']++;
return this.reportText(' [完了時間] ' + $common.time())
.reportText(' [ステータス] ' +
(code == this.$errorCode.NO_ERROR ? '圧縮完了' :
code == this.$errorCode.WARNING ? '正常に処理を完了できませんでした' :
code == this.$errorCode.FATAL ? '致命的なエラーが発生しました' :
code == this.$errorCode.SYNTAX ? '構文エラーが発生しました' :
code == this.$errorCode.MEMORY ? '十分なメモリーがありません' :
code == this.$errorCode.CANCEL ? 'ユーザによって実行がキャンセルされました' :
'圧縮に失敗しました。(不明な終了コード:' + code + ')')
);
};
/**
* $select2zip.skipMessage
* @param {string} code[skip code]
*/
$select2zip.prototype.skipMessage = function(code)
{
this.$result.count++;
if (this.$skipCode.NO_SKIP == code) return;
this.$result.skip++;
return this.reportText(' [ステータス] ' +
(code == this.$skipCode.FOLDER ? 'フォルダの圧縮をスキップしました' :
code == this.$skipCode.FILE ? 'ファイルの圧縮をスキップしました' :
code == this.$skipCode.ZIP ? '重複圧縮を回避しました' :
'不明なスキップコード(' + code + ')を受け付けました')
);
};
/**
* $select2zip.resultMessage
*/
$select2zip.prototype.resultMessage = function()
{
return this.reportText(' #実行結果 (' + this.$result.count + '個のファイル中)')
.reportText(' 正常終了 ' + this.$result.success + '個')
.reportText(' 作成失敗 ' + this.$result.fault + '個')
.reportText(' スキップ ' + this.$result.skip + '個')
.reportWrite('');
};
/**
* $select2zip.finish
*/
$select2zip.prototype.finish = function()
{
if (this.$option.finish == true)
MDIE.echo($z.$result.count == 0 ?
'圧縮対象が見つかりませんでした。' : 'select2zipを終了しました。');
};
/**
* $select2zip.zip
* @param {string} archive[create zip file name]
* @param {string} path[zip file or folder]
* @return {integer}[exit code]
*/
$select2zip.prototype.zip = function(archive, path)
{
return $shell.run(
'"' + this.$system.exe + '" a -t' + this.$system.zipMode.type + ' "' +
archive + '" "' + path + '"',
7, true);
};
/**
* $select2zip.unzip
* @param {string} path[unzip file]
* @param {string} basedir[unzip directory]
* @return {integer}[exit code]
*/
$select2zip.prototype.unzip = function(path, basedir)
{
return $shell.run(
'"' + this.$system.exe + '" x "'
+ path + '" -o"' + basedir + '"',
7, true);
};
/**
* $select2zip.exec
* @param {array} files
*/
$select2zip.prototype.exec = function(files)
{
for (var i=0; i<files.length; i++) {
info = $common.parseFile(files[i]);
this.reportText(' ' + info.path + ' {')
.reportText(' [開始時間] ' + $common.time());
if ((skip = this.skip(info.ext)) > 0) {
this.skipMessage(skip).reportWrite(' }');
continue;
}
var local = $common.uniquePath(
info.dir,
this.$option.removeExt == true ? info.name : info.file,
this.$system.zipMode.ext
);
var result = this.zip(local, info.path);
this.exitMessage(result);
if ($fso.FileExists(local)) this.reportText(' [作成ファイル] ' + local);
this.reportWrite(' }');
FolderView.Refresh(2);
if (this.$option.keepAction == false &&
this.$errorCode.CANCEL == result) {
this.reportText(' #実行の中止。');
return;
}
}
};
/*
* 実行
*/
var $z = new $select2zip($setting);
$z.exec($files);
$z.resultMessage();
$z.finish();
})();
記事に戻る