August 23, 2005

Reading jarred files with JSLib

Assuming jslib gets a fix for a perceived bustage in fileUtils.js soon, here's some code I whipped up for grabbing files from chrome, either jarred or flat:

  const fUtils = new FileUtils();
  var localPath = fUtils.chromeToPath(sourceURI);
  var isJarRE = /^jar:(file:\/\/\/.*\.jar)!\/(.*)/.exec(localPath);
  var fileAsString = "";

  if (isJarRE) {
    include(jslib_zip);
    var jarPath = fUtils.urlToPath(isJarRE[1]);
    var jarFile = new File(jarPath);

    var jarContents = new Zip(jarPath);
    jarContents.open();
    fileAsString = jarContents.readEntry(isJarRE[2]);
    jarContents.close();

  } else {
    var fileObj = new File(localPath);
    fileObj.open("r");
    fileAsString = fileObj.read();
    fileObj.close();
  }

This assumes you've loaded chrome://jslib/content/jslib.js and chrome://jslib/content/io/io.js already.

On a side note, I've finished the first step of the 34-step Verbosio smoketest. This code above was needed to make sure I did things right. Posted by WeirdAl at August 23, 2005 11:08 PM