/** * Extrait une arborescence d'un jar. * * @param jarFile Jarre dont on extrait les fichiers. * @param destDir Dossier où on déploie les fichiers. * @param jarEntry Racine des sous-dossiers à extraire. Si null extrait tout les fichiers. */ public static void jarExtract(String jarFile, String destDir, String jarEntry) { try { ProgletsBuilder.log( "Extract files from " + jarFile + " to " + destDir + ((!jarEntry.isEmpty()) ? " which start with " + jarEntry : ""), true); JarFile jf = new JarFile(jarFile); JarInputStream jip = new JarInputStream(new FileInputStream(jarFile)); jf.entries(); JarEntry je; while ((je = jip.getNextJarEntry()) != null) { if ((jarEntry.isEmpty() ? true : je.getName().startsWith(jarEntry)) && !je.isDirectory() && !je.getName().contains("META-INF")) { File dest = new File(destDir + File.separator + je.getName()); dest.getParentFile().mkdirs(); JarManager.copyStream(jip, new FileOutputStream(dest)); } } jip.close(); } catch (Exception ex) { throw new IllegalStateException(ex); } }
/** * Copie un répertoire/fichier dans un autre en oubliant les svn. * * @param srcDir Dossier source. * @param dstDir Dossier cible. */ public static void copyFiles(String srcDir, String dstDir) throws IOException { if (new File(srcDir).isDirectory()) { if (!new File(srcDir).getName().equals(".svn")) { for (String s : FileManager.list(srcDir)) { String d = dstDir + File.separator + new File(s).getAbsoluteFile().getName(); JarManager.copyFiles(s, d); } } } else { new File(dstDir).getParentFile().mkdirs(); JarManager.copyStream(new FileInputStream(srcDir), new FileOutputStream(dstDir)); } }
// 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); } }