protected void unpackComponents() throws IOException, FileNotFoundException {
    File applicationPackage = new File(getApplication().getPackageResourcePath());
    File componentsDir = new File(sGREDir, "components");
    if (componentsDir.lastModified() == applicationPackage.lastModified()) return;

    componentsDir.mkdir();
    componentsDir.setLastModified(applicationPackage.lastModified());

    GeckoAppShell.killAnyZombies();

    ZipFile zip = new ZipFile(applicationPackage);

    byte[] buf = new byte[32768];
    try {
      if (unpackFile(zip, buf, null, "removed-files")) removeFiles();
    } catch (Exception ex) {
      // This file may not be there, so just log any errors and move on
      Log.w(LOG_FILE_NAME, "error removing files", ex);
    }

    // copy any .xpi file into an extensions/ directory
    Enumeration<? extends ZipEntry> zipEntries = zip.entries();
    while (zipEntries.hasMoreElements()) {
      ZipEntry entry = zipEntries.nextElement();
      if (entry.getName().startsWith("extensions/") && entry.getName().endsWith(".xpi")) {
        Log.i("GeckoAppJava", "installing extension : " + entry.getName());
        unpackFile(zip, buf, entry, entry.getName());
      }
    }
  }
  private boolean unpackFile(ZipFile zip, byte[] buf, ZipEntry fileEntry, String name)
      throws IOException, FileNotFoundException {
    if (fileEntry == null) fileEntry = zip.getEntry(name);
    if (fileEntry == null)
      throw new FileNotFoundException("Can't find " + name + " in " + zip.getName());

    File outFile = new File(sGREDir, name);
    if (outFile.lastModified() == fileEntry.getTime() && outFile.length() == fileEntry.getSize())
      return false;

    File dir = outFile.getParentFile();
    if (!dir.exists()) dir.mkdirs();

    InputStream fileStream;
    fileStream = zip.getInputStream(fileEntry);

    OutputStream outStream = new FileOutputStream(outFile);

    while (fileStream.available() > 0) {
      int read = fileStream.read(buf, 0, buf.length);
      outStream.write(buf, 0, read);
    }

    fileStream.close();
    outStream.close();
    outFile.setLastModified(fileEntry.getTime());
    return true;
  }
Exemple #3
0
  public static void upgrade(File wrFile) throws Exception {
    System.out.println("Installing/upgrading Myna in '" + wrFile.toString() + "'...");
    wrFile.mkdirs();
    File web_inf = new File(wrFile.toURI().resolve("WEB-INF"));
    boolean isUpgrade = false;
    File backupDir = null;
    if (web_inf.exists()) {

      String dateString =
          new java.text.SimpleDateFormat("MM-dd-yyyy_HH.mm.ss.S").format(new Date());
      String backupBase = "WEB-INF/upgrade_backups/backup_" + dateString;
      backupDir = new File(wrFile.toURI().resolve(backupBase));
      backupDir.mkdirs();
      isUpgrade = true;
      System.out.println("Backups stored in " + backupDir);
      // backup entire /myna folder because we're wiping it out
      FileUtils.copyDirectory(
          new File(wrFile.toURI().resolve("myna")), new File(backupDir.toURI().resolve("myna")));
      FileUtils.deleteDirectory(new File(wrFile.toURI().resolve("myna")));
    }

    if (isJar) {
      String jarFilePath = classUrl.substring(classUrl.indexOf(":") + 1, classUrl.indexOf("!"));
      File jarFile = new File(new java.net.URL(jarFilePath).toURI());
      ZipFile zipFile = new ZipFile(jarFile);

      for (ZipEntry entry : java.util.Collections.list(zipFile.entries())) {;
        File outputFile =
            new File(wrFile.toURI().resolve(java.net.URLEncoder.encode(entry.getName(), "UTF-8")));
        File backupFile = null;
        if (isUpgrade) {
          backupFile =
              new File(
                  backupDir.toURI().resolve(java.net.URLEncoder.encode(entry.getName(), "UTF-8")));
        }
        if (entry.isDirectory()) {
          outputFile.mkdirs();
          if (isUpgrade) backupFile.mkdirs();
        } else {
          if (isUpgrade && outputFile.exists()) {

            java.io.InputStream sourceIS = zipFile.getInputStream(entry);
            java.io.InputStream targetIS = FileUtils.openInputStream(outputFile);
            boolean isSame = IOUtils.contentEquals(sourceIS, targetIS);
            sourceIS.close();
            targetIS.close();

            if (isSame
                || entry.toString().equals("index.html")
                || entry.toString().equals("application.sjs")
                || entry.toString().equals("WEB-INF/classes/general.properties")
                || entry.toString().startsWith("WEB-INF/myna/ds")) {
              continue;
            } else {
              System.out.println("...backing up " + entry);
              FileUtils.copyFile(outputFile, backupFile, true);
              // outputFile.copyTo(backupFile);
              // fusebox.upgradeLog("Backup: " + backupFile);
            }
          }
          java.io.InputStream is = zipFile.getInputStream(entry);
          java.io.OutputStream os = FileUtils.openOutputStream(outputFile);
          IOUtils.copyLarge(is, os);
          is.close();
          os.close();
        }
      }
      zipFile.close();
      // FileUtils.deleteDirectory()

      System.out.println("Done unpacking.");
    }
  }