nsIZipReader

 

An example of use of nsIZipReader to extract a zip file provided by the user through the nsIFilePicker. The content of the zip archive is unpacked inside a subdirectory of the profile folder: profile/quizfx/nameOfTheArchive.

 

 

var store2 = Components.classes["@mozilla.org/file/directory_service;1"]
                                  .getService(Components.interfaces.nsIProperties).get("ProfD",
                                          Components.interfaces.nsIFile);
store2.append("quizfx");

function importQuiz(){
      var file = pickZipQuiz();
        var fileName = file.leafName;
        var extensionDelimiter = fileName.lastIndexOf(".");
          var prefix = fileName.substr(0, extensionDelimiter);
          store2.append(prefix);
          alert(store2.path);
          if( !store2.exists() || !store2.isDirectory() ) { // if it doesn't exist, create
                store2.create(Components.interfaces.nsIFile.DIRECTORY_TYPE, 0777); }
          alert(store2.path);
      extractExtensionFiles(prefix, file);
}


function pickZipQuiz(){
    const nsIFilePicker = Components.interfaces.nsIFilePicker;

    var fp = Components.classes["@mozilla.org/filepicker;1"]
                   .createInstance(nsIFilePicker);
    fp.init(window, "Dialog Title", nsIFilePicker.modeOpen);
    fp.appendFilters(nsIFilePicker.filterAll | nsIFilePicker.filterText);

    var rv = fp.show();
    if (rv == nsIFilePicker.returnOK || rv == nsIFilePicker.returnReplace) {
      var file = fp.file;
      // Get the path as string. Note that you usually won't
      // need to work with the string paths.
      var path = fp.file.path;
      // work with returned nsILocalFile...
      return file;
   
        }
}

function extractExtensionFiles(extensionID, xpiFile) {
  

    var zipReader = Components.classes["@mozilla.org/libjar/zip-reader;1"]
                                     .createInstance(Components.interfaces.nsIZipReader);
    zipReader.open(xpiFile);
    zipReader.test(null);
    // create directories first
    var entries = zipReader.findEntries("*/");
    while (entries.hasMore()) {
      var entryName = entries.getNext();
      var target = getItemFile(entryName);
      if (!target.exists()) {
        try {
          target.create(Ci.nsILocalFile.DIRECTORY_TYPE, 0777);
        }
        catch (e) {
          alert("extractExtensionsFiles: failed to create target directory for extraction " +
                " file = " + target.path + ", exception = " + e + "\n");
        }
      }
    }

    entries = zipReader.findEntries(null);
    while (entries.hasMore()) {
      var entryName = entries.getNext();
      target = getItemFile(entryName);
      if (target.exists())
        continue;

      try {
        target.create(Ci.nsILocalFile.DIRECTORY_TYPE, 0777);
      }
      catch (e) {
        alert("extractExtensionsFiles: failed to create target file for extraction " +
              " file = " + target.path + ", exception = " + e + "\n");
      }
      zipReader.extract(entryName, target);
    }
    zipReader.close();
  }

function getItemFile( filePath) {
    //alert(store2.path);
    var itemLocation = store2.clone();
    var parts = filePath.split("/");
    for (var i = 0; i < parts.length; ++i)
      itemLocation.append(parts[i]);
    return itemLocation;
  }

Document Tags and Contributors

 Contributors to this page: Fxparlant
 Last updated by: Fxparlant,