private void insertFile(
      Path apkPath, Map<String, String> zip_properties, File insert, String method, Path location)
      throws AndrolibException, IOException {
    // ZipFileSystem only writes at .close()
    // http://mail.openjdk.java.net/pipermail/nio-dev/2012-July/001764.html
    try (FileSystem fs = FileSystems.newFileSystem(apkPath, null)) {
      Path root = fs.getPath("/");

      // in order to get the path relative to the zip, we strip off the absolute path, minus what we
      // already have in the zip. thus /var/files/apktool/apk/unknown/folder/file => /folder/file
      Path dest =
          fs.getPath(root.toString(), insert.getAbsolutePath().replace(location.toString(), ""));
      Path newFile = Paths.get(insert.getAbsolutePath());
      Files.copy(newFile, dest, StandardCopyOption.REPLACE_EXISTING);
      fs.close();
    }
  }
  private void insertFolder(
      Path apkPath, Map<String, String> zip_properties, File insert, String method, Path location)
      throws AndrolibException, IOException {
    try (FileSystem fs = FileSystems.newFileSystem(apkPath, null)) {
      Path root = fs.getPath("/");
      Path dest =
          fs.getPath(root.toString(), insert.getAbsolutePath().replace(location.toString(), ""));
      Path parent = dest.normalize();

      // check for folder existing in apkFileSystem
      if (parent != null && Files.notExists(parent)) {
        if (!Files.isDirectory(parent, LinkOption.NOFOLLOW_LINKS)) {
          Files.createDirectories(parent);
        }
      }
      fs.close();
    }
  }
  public void buildUnknownFiles(File appDir, File outFile, Map<String, Object> meta)
      throws AndrolibException {
    File file;
    Path globalPath = Paths.get(appDir.getPath() + File.separatorChar + UNK_DIRNAME);

    if (meta.containsKey("unknownFiles")) {
      LOGGER.info("Copying unknown files/dir...");

      Map<String, String> files = (Map<String, String>) meta.get("unknownFiles");

      try {
        // set our filesystem options
        Map<String, String> zip_properties = new HashMap<>();
        zip_properties.put("create", "false");
        zip_properties.put("encoding", "UTF-8");

        // create filesystem
        Path path = Paths.get(outFile.getAbsolutePath());

        // loop through files inside
        for (Map.Entry<String, String> entry : files.entrySet()) {

          file = new File(globalPath.toFile(), entry.getKey());
          if (file.isFile() && file.exists()) {
            insertFolder(
                path,
                zip_properties,
                file.getParentFile(),
                entry.getValue(),
                globalPath.toAbsolutePath());

            insertFile(path, zip_properties, file, entry.getValue(), globalPath.toAbsolutePath());
          }
        }
      } catch (IOException ex) {
        throw new AndrolibException(ex);
      }
    }
  }
  public void aaptPackage(
      File apkFile,
      File manifest,
      File resDir,
      File rawDir,
      File assetDir,
      File[] include,
      boolean update,
      boolean framework)
      throws AndrolibException {
    List<String> cmd = new ArrayList<String>();

    cmd.add("aapt");
    cmd.add("p");
    cmd.add("-v"); // mega debug mode.@todo REMOVE ON FINAL
    if (update) {
      cmd.add("-u");
    }
    if (mMinSdkVersion != null) {
      cmd.add("--min-sdk-version");
      cmd.add(mMinSdkVersion);
    }
    if (mTargetSdkVersion != null) {
      cmd.add("--target-sdk-version");
      cmd.add(mTargetSdkVersion);
    }
    if (mMaxSdkVersion != null) {
      cmd.add("--max-sdk-version");
      cmd.add(mMaxSdkVersion);
    }
    cmd.add("-F");
    cmd.add(apkFile.getAbsolutePath());

    if (framework) {
      cmd.add("-x");
      //            cmd.add("-0");
      //            cmd.add("arsc");
    }

    if (include != null) {
      for (File file : include) {
        cmd.add("-I");
        cmd.add(file.getPath());
      }
    }
    if (resDir != null) {
      cmd.add("-S");
      cmd.add(resDir.getAbsolutePath());
    }
    if (manifest != null) {
      cmd.add("-M");
      cmd.add(manifest.getAbsolutePath());
    }
    if (assetDir != null) {
      cmd.add("-A");
      cmd.add(assetDir.getAbsolutePath());
    }
    if (rawDir != null) {
      cmd.add(rawDir.getAbsolutePath());
    }

    try {
      OS.exec(cmd.toArray(new String[0]));
    } catch (BrutException ex) {
      throw new AndrolibException(ex);
    }
  }