Column: RAR Version

A primitive column script which displays the RAR archive version inside it's own column.
This is meant as a sample for file header comparison. Everyone may modify and adapt it to their liking.

Special thanks to wowbagger for his great custom Column script. This borrows parts of his code.

Installation:

  • Download DevRarVersion.js.txt (6.7 KB)
  • Drag it to the list under Preferences / Toolbars / Scripts.

Then, in a file display:

  • Right click Column header (in details view) and choose:
    Columns > Script > RAR Version Column.

How it works:

  • Check file extension against a list of desired extensions.
  • If we get a match we send it to a comparator for version matching.
  • There we read the headers via FSUtil and compare them to pre-made blobs containing signatures.

Preview:


Script Code:

The script code in the download above is reproduced here for reference.

function OnInit(initData) {
    // Provide basic information about the script by initializing the properties of the ScriptInitData object
    initData.name = "RAR Version Column";
    initData.desc = "Adds a column to display RAR archive version";
    initData.copyright = "2015 Devocalypse";
    initData.min_version = "11.8";
    initData.version = "1";
    initData.default_enable = true;

    // Create a new ScriptColumn object and initialize it to add the column to Opus
    for (var i = 0; i < config.columns.length; ++i) {
        var cmd = initData.AddColumn();
        cmd.name = scriptNamePrefix + config.columns[i].name;
        cmd.method = "OnCustomTextColumnAdvanced";
        cmd.label = config.columns[i].name;
        cmd.autogroup = true;
        cmd.autorefresh = true;      // auto-refresh column when file changes
        cmd.namerefresh = false;     //refresh only on name change

        //Customisation
        cmd.justify = config.columns[i].justify != null ? config.columns[i].justify : "left";
        cmd.type = config.columns[i].type != null ? config.columns[i].type : "text";

        cmd.match.push_back("Yes");  // when searching, these are the only two options
        cmd.match.push_back("No");
    }
}

var scriptNamePrefix = "DevColumns:";
var config = {
    //Configuration for different patterns
    columns: [
        {
            name: "RAR Version",
            matchType: "file",
            matchExtension: ".rar|.cbr"
        }
    ]
};

function OnCustomTextColumnAdvanced(scriptColData) {
    // scriptColData is a ScriptColumnData object. first check that this right column
    if (scriptColData.col.substr(0, scriptNamePrefix.length) != scriptNamePrefix) return;

    // get column's config
    var config = GetConfig(scriptColData.col.replace(scriptNamePrefix, ""));

    //matchType option
    if (scriptColData.item.is_dir && config.matchType == "file") return;
    else if (!scriptColData.item.is_dir && config.matchType == "folder") return;

    //matchExtension option
    if (config.matchExtension != null) {
        var exts = config.matchExtension.split("|");
        if (!exts.contains(scriptColData.item.ext)) return;
    }
    //output value to column
    scriptColData.value = CheckRarType(scriptColData.item);
}

function CheckRarType(rarpath) {
    //define preset signatures (make a loop later)
    var rar4sig = DOpus.Create.Blob(82, 97, 114, 33, 26, 7, 0);
    var rar5sig = DOpus.Create.Blob(82, 97, 114, 33, 26, 7, 1);
    //read file header
    var b = DOpus.Create.Blob();
    var f = DOpus.FSUtil.OpenFile(rarpath);
    if ((f) && (f.error == 0)) {
        f.Seek(0, "b");
        //read # of bytes from start of file
        f.Read(b, 7);
        f.Close();
    }
    //compare signatures
    if (b.Compare(rar4sig) == 0)
        return "RAR v2.9";
    if (b.Compare(rar5sig) == 0)
        return "RAR v5.0";
    //no match
    return "Bad Header";
}

/* HELPER FUNCTIONS */
function GetConfig(key) {
    var match = null;

    for (var i = 0; i < config.columns.length; ++i) {
        if (config.columns[i].name == key) {
            match = config.columns[i];
            break;
        }
    }
    return match;
}


//Define contains function for extension checking purposes
Array.prototype.contains = function (k) {
    for (var i = 0; i < this.length; i++) {
        if (this[i] === k) {
            return true;
        }
    }
    return false;
}
1 Like