private void addEntry(ZipOutputStream zipStream, String root, File file) { if (file.isHidden()) { return; } var name = root + file.getName(); var zipEntry = new ZipEntry(name); zipStream.putNextEntry(zipEntry); var buffer = new byte[4096]; var inputStream = new FileInputStream(file); int read; while ((read = inputStream.read(buffer)) != -1) { zipStream.write(buffer, 0, read); } }
public int run(String[] arguments) { sourceFiles.clear(); if (!handleArguments(arguments)) { return 1; } var t0 = System.nanoTime(); try { var results = new Compiler().compileFromFiles(parameters, sourceFiles.toArray(new File[sourceFiles.size()])); var hasErrors = false; foreach (var error in results.Errors) { var filename = error.Filename; if (filename != null) { System.out.print(new File(error.Filename).getAbsolutePath()); } else { System.out.print("Unknown source"); } if (error.Line > 0) { System.out.print(" ("); System.out.print(error.Line); if (error.Column > 0) { System.out.print(", "); System.out.print(error.Column); } System.out.print(")"); } if (error.Level == 0) { hasErrors = true; System.out.print(" error "); } else { System.out.print(" warning "); } System.out.print(error.Id); System.out.print(": "); System.out.println(error.Message); } if (!hasErrors) { var outputFile = new File(outputPath); if (outputFile.isDirectory() || outputPath.endsWith("\\") || outputPath.endsWith("/")) { foreach (var e in results.ClassFiles.entrySet()) { var file = new File(outputFile, e.Key.replace('.', '/') + ".class"); var dir = file.getParentFile(); if (!dir.exists()) { dir.mkdirs(); } using (var s = new FileOutputStream(file)) { s.write(e.Value); } } } else { var destination = outputPath; if (PathHelper.getExtension(destination).length() == 0) { destination += ".jar"; } using (var zipStream = new ZipOutputStream(new FileOutputStream(destination))) { if (manifestPath != null) { var zipEntry = new ZipEntry("META-INF/MANIFEST.MF"); zipStream.putNextEntry(zipEntry); var buffer = new byte[4096]; var inputStream = new FileInputStream(manifestPath); int read; while ((read = inputStream.read(buffer)) != -1) { zipStream.write(buffer, 0, read); } inputStream.close(); } if (resourcesPath != null) { var rootDir = new File(resourcesPath); foreach (var content in rootDir.list()) { var file = new File(rootDir, content); if (file.isDirectory()) { exploreDirectory(zipStream, "", file); } else { addEntry(zipStream, "", file); } } } foreach (var e in results.ClassFiles.entrySet()) { var zipEntry = new ZipEntry(e.Key.replace('.', '/') + ".class"); zipStream.putNextEntry(zipEntry); zipStream.write(e.Value); } } } System.out.println(); System.out.println(String.format("%d class(es) successfully generated in %.2fs", results.classFiles.size(), (System.nanoTime() - t0) / 1e9)); return 0; } else { System.out.println(); System.out.println("Compilation failed"); return 1; } } catch (TypeLoadException e) { System.out.println("Cannot find type " + e.TypeName + ". The class is missing from the classpath."); System.out.println("Compilation failed"); return 1; } }