public static void packClassFilesIntoJar( @NotNull String[] firstPackageDirPaths, @NotNull String[] libFirstPackageDirPaths, @NotNull File jarFile) throws IOException { final JarOutputStream jos = new JarOutputStream(new FileOutputStream(jarFile)); try { for (String path : firstPackageDirPaths) { final File firstPackageDir = new File(path); if (firstPackageDir.exists()) { addFileToJar(jos, firstPackageDir, firstPackageDir.getParentFile(), true); } } for (String path : libFirstPackageDirPaths) { final File firstPackageDir = new File(path); if (firstPackageDir.exists()) { addFileToJar(jos, firstPackageDir, firstPackageDir.getParentFile(), false); } } } finally { jos.close(); } }
private static void addFileToJar( @NotNull JarOutputStream jar, @NotNull File file, @NotNull File rootDirectory, boolean packRClasses) throws IOException { if (file.isDirectory()) { for (File child : file.listFiles()) { addFileToJar(jar, child, rootDirectory, packRClasses); } } else if (file.isFile()) { if (!FileUtil.getExtension(file.getName()).equals("class")) { return; } if (!packRClasses && R_PATTERN.matcher(file.getName()).matches()) { return; } final String rootPath = rootDirectory.getAbsolutePath(); String path = file.getAbsolutePath(); path = FileUtil.toSystemIndependentName(path.substring(rootPath.length())); if (path.charAt(0) == '/') { path = path.substring(1); } final JarEntry entry = new JarEntry(path); entry.setTime(file.lastModified()); jar.putNextEntry(entry); BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file)); try { final byte[] buffer = new byte[1024]; int count; while ((count = bis.read(buffer)) != -1) { jar.write(buffer, 0, count); } jar.closeEntry(); } finally { bis.close(); } } }