Example #1
0
 // Ajoute un stream a un jar
 private static void copyFileToJar(
     File source, JarOutputStream target, File root, String[] jarEntries) throws IOException {
   // Teste si la source est dans les fichier à extraire
   if (jarEntries != null) {
     boolean skip = true;
     for (String jarEntry : jarEntries) {
       String entry = root.toString() + File.separator + jarEntry;
       skip &= !(entry.startsWith(source.toString()) | source.toString().startsWith(entry));
     }
     if (skip) return;
   }
   try {
     if (source.isDirectory()) {
       String name =
           source
               .getPath()
               .replace(root.getAbsolutePath() + File.separator, "")
               .replace(File.separator, "/");
       if (!name.isEmpty() && (!source.equals(root))) {
         if (!name.endsWith("/")) {
           name += "/";
         }
         JarEntry entry = new JarEntry(name);
         entry.setTime(source.lastModified());
         target.putNextEntry(entry);
         target.closeEntry();
       }
       for (File nestedFile : source.listFiles()) {
         JarManager.copyFileToJar(nestedFile, target, root, jarEntries);
       }
     } else {
       JarEntry entry =
           new JarEntry(
               source
                   .getPath()
                   .replace(root.getAbsolutePath() + File.separator, "")
                   .replace(File.separator, "/"));
       entry.setTime(source.lastModified());
       target.putNextEntry(entry);
       JarManager.copyStream(new BufferedInputStream(new FileInputStream(source)), target);
     }
   } catch (Throwable e) {
     e.printStackTrace(System.out);
     throw new IllegalStateException(e);
   }
 }
Example #2
0
 /**
  * Crée un jar à partir d'une arborescence.
  *
  * @param jarFile Jar à construire. Elle est détruite avant d'être crée.
  * @param mfFile Fichier de manifeste (obligatoire).
  * @param srcDir Dossier source avec les fichiers à mettre en jarre.
  * @param jarEntries Racine des sous-dossiers à extraire. Si null extrait tout les fichiers.
  */
 public static void jarCreate(String jarFile, String mfFile, String srcDir, String[] jarEntries) {
   try {
     ProgletsBuilder.log("Création du jar " + jarFile, true);
     File parent = new File(jarFile).getParentFile();
     if (parent != null) {
       parent.mkdirs();
     }
     new File(jarFile).delete();
     srcDir = new File(srcDir).getCanonicalPath();
     Manifest manifest = new Manifest(new FileInputStream(mfFile));
     manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
     JarOutputStream target = new JarOutputStream(new FileOutputStream(jarFile), manifest);
     JarManager.copyFileToJar(new File(srcDir), target, new File(srcDir), jarEntries);
     target.close();
   } catch (Exception ex) {
     ex.printStackTrace();
     throw new RuntimeException(ex);
   }
 }