B2G/gecko/toolkit/mozapps/extensions/XPIProvider.jsm
/**
 * Extracts files from a ZIP file into a directory.
 *
 * @param  aZipFile
 *         The source ZIP file that contains the add-on.
 * @param  aDir
 *         The nsIFile to extract to.
 */
Extracts files from a ZIP file into a directory.
@param  aZipFile
The source ZIP file that contains the add-on.
@param  aDir
The nsIFile to extract to.
Extracts files from a ZIP file into a directory . @param aZipFile The source ZIP file that contains the add - on . @param aDir The nsIFile to extract to .
function extractFiles(aZipFile, aDir) {
  function getTargetFile(aDir, entry) {
    let target = aDir.clone();
    entry.split("/").forEach(function(aPart) {
      target.append(aPart);
    });
    return target;
  }
  let zipReader = Cc["@mozilla.org/libjar/zip-reader;1"].
                  createInstance(Ci.nsIZipReader);
  zipReader.open(aZipFile);
  try {
    let entries = zipReader.findEntries("*/");
    while (entries.hasMore()) {
      var entryName = entries.getNext();
      let target = getTargetFile(aDir, entryName);
      if (!target.exists()) {
        try {
          target.create(Ci.nsIFile.DIRECTORY_TYPE,
                        FileUtils.PERMS_DIRECTORY);
        }
        catch (e) {
          ERROR("extractFiles: failed to create target directory for " +
                "extraction file = " + target.path, e);
        }
      }
    }
    entries = zipReader.findEntries(null);
    while (entries.hasMore()) {
      let entryName = entries.getNext();
      let target = getTargetFile(aDir, entryName);
      if (target.exists())
        continue;
      zipReader.extract(entryName, target);
      try {
        target.permissions |= FileUtils.PERMS_FILE;
      }
      catch (e) {
        WARN("Failed to set permissions " + aPermissions.toString(8) + " on " +
             target.path, e);
      }
    }
  }
  finally {
    zipReader.close();
  }
}
function extractFiles ( aZipFile , aDir ) { function getTargetFile ( aDir , entry ) { let target = aDir . clone ( ) ; entry . split ( "/" ) . forEach ( function ( aPart ) { target . append ( aPart ) ; } ) ; return target ; } let zipReader = Cc [ "@mozilla.org/libjar/zip-reader;1" ] . createInstance ( Ci . nsIZipReader ) ; zipReader . open ( aZipFile ) ; try { let entries = zipReader . findEntries ( "*/" ) ; while ( entries . hasMore ( ) ) { var entryName = entries . getNext ( ) ; let target = getTargetFile ( aDir , entryName ) ; if ( ! target . exists ( ) ) { try { target . create ( Ci . nsIFile . DIRECTORY_TYPE , FileUtils . PERMS_DIRECTORY ) ; } catch ( e ) { ERROR ( "extractFiles: failed to create target directory for " + "extraction file = " + target . path , e ) ; } } } entries = zipReader . findEntries ( null ) ; while ( entries . hasMore ( ) ) { let entryName = entries . getNext ( ) ; let target = getTargetFile ( aDir , entryName ) ; if ( target . exists ( ) ) continue ; zipReader . extract ( entryName , target ) ; try { target . permissions |= FileUtils . PERMS_FILE ; } catch ( e ) { WARN ( "Failed to set permissions " + aPermissions . toString ( 8 ) + " on " + target . path , e ) ; } } } finally { zipReader . close ( ) ; } }
Extracts files from a ZIP file into a directory.
Extracts files from a ZIP file into a directory .
// create directories first