java.util.jar.JarEntry Scala Examples
The following examples show how to use java.util.jar.JarEntry.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
Example 1
Source File: Package.scala From seed with Apache License 2.0 | 5 votes |
package seed.generation import java.io.{File, FileInputStream, OutputStream} import java.util.jar.{Attributes, JarEntry, JarOutputStream, Manifest} import org.apache.commons.io.IOUtils import java.nio.file.Path import seed.Log import seed.cli.util.Ansi import scala.collection.mutable // Adapted from https://stackoverflow.com/a/1281295 object Package { def create( source: List[(Path, String)], target: OutputStream, mainClass: Option[String], classPath: List[String], log: Log ): Unit = { val manifest = new Manifest() val mainAttributes = manifest.getMainAttributes mainAttributes.put(Attributes.Name.MANIFEST_VERSION, "1.0") // TODO Set additional package fields: https://docs.oracle.com/javase/tutorial/deployment/jar/packageman.html mainClass.foreach( cls => mainAttributes.put(Attributes.Name.MAIN_CLASS, cls) ) if (classPath.nonEmpty) mainAttributes.put(Attributes.Name.CLASS_PATH, classPath.mkString(" ")) val targetFile = new JarOutputStream(target, manifest) val entryCache = mutable.Set[String]() source.foreach { case (path, jarPath) => log.debug(s"Packaging ${Ansi.italic(path.toString)}...") add(path.toFile, jarPath, targetFile, entryCache, log) } targetFile.close() } def add( source: File, jarPath: String, target: JarOutputStream, entryCache: mutable.Set[String], log: Log ): Unit = { val path = if (source.isFile) jarPath else { require(!jarPath.endsWith("/")) jarPath + "/" } val addedEntry = if (entryCache.contains(path)) { if (source.isFile) log.warn( s"Skipping file ${Ansi.italic(source.toString)} as another module already added it" ) false } else { val entry = new JarEntry(path) entry.setTime(source.lastModified) target.putNextEntry(entry) entryCache += path if (source.isFile) IOUtils.copy(new FileInputStream(source), target) true } if (!source.isFile) for (nestedFile <- source.listFiles) add(nestedFile, path + nestedFile.getName, target, entryCache, log) if (addedEntry) target.closeEntry() } }
Example 2
Source File: ScioReplClassLoader.scala From scio with Apache License 2.0 | 5 votes |
package com.spotify.scio.repl import java.io.{File, FileOutputStream} import java.net.{URL, URLClassLoader} import java.nio.file.Files import java.util.jar.{JarEntry, JarOutputStream} import java.lang.invoke.{MethodHandles, MethodType} import com.spotify.scio.repl.compat.ILoopClassLoader import org.slf4j.LoggerFactory import scala.tools.nsc.io._ object ScioReplClassLoader { private val Logger = LoggerFactory.getLogger(this.getClass) private[this] val JDK9OrHigher: Boolean = util.Properties.isJavaAtLeast("9") private[this] val BootClassLoader: ClassLoader = { if (!JDK9OrHigher) null else { try { MethodHandles .lookup() .findStatic( classOf[ClassLoader], "getPlatformClassLoader", MethodType.methodType(classOf[ClassLoader]) ) .invoke() } catch { case _: Throwable => null } } } def classLoaderURLs(cl: ClassLoader): Array[java.net.URL] = cl match { case null => Array.empty case u: java.net.URLClassLoader => u.getURLs ++ classLoaderURLs(cl.getParent) case _ => classLoaderURLs(cl.getParent) } @inline final def apply(urls: Array[URL]) = new ScioReplClassLoader(urls, BootClassLoader) } private def addVirtualDirectoryToJar( dir: Iterable[AbstractFile], entryPath: String, jarStream: JarOutputStream ): Unit = dir.foreach { file => if (file.isDirectory) { // Recursively descend into subdirectories, adjusting the package name as we do. val dirPath = entryPath + file.name + "/" jarStream.putNextEntry(new JarEntry(dirPath)) jarStream.closeEntry() addVirtualDirectoryToJar(file, dirPath, jarStream) } else if (file.hasExtension("class")) { // Add class files as an entry in the jar file and write the class to the jar. jarStream.putNextEntry(new JarEntry(entryPath + file.name)) jarStream.write(file.toByteArray) jarStream.closeEntry() } } }
Example 3
Source File: JarUtils.scala From sope with Apache License 2.0 | 5 votes |
package com.sope.etl.utils import java.io._ import java.nio.file.Files import java.util.jar.{JarEntry, JarOutputStream} def buildJar(classFolder: String, jarLocation: String): Unit = { val target = new JarOutputStream(new FileOutputStream(jarLocation)) add(classFolder, target, classFolder) target.close() } // adds files to jar private def add(folder: String, target: JarOutputStream, replacement: String): Unit = { val source = new File(folder) if (source.isDirectory) { val name = source.getPath.replace("\\", "/") if (!name.isEmpty) { val folderName = if (!name.endsWith("/")) name + "/" else name val entry = new JarEntry(folderName) entry.setTime(source.lastModified) target.putNextEntry(entry) target.closeEntry() } for (nestedFile <- source.listFiles) { add(nestedFile.getAbsolutePath, target, replacement) } } else { val entry = new JarEntry(source.getPath .replace("\\", "/") .replace(replacement, "")) entry.setTime(source.lastModified) target.putNextEntry(entry) val byteArray = Files.readAllBytes(source.toPath) target.write(byteArray) target.closeEntry() } } }
Example 4
Source File: JarHelper.scala From cassandra-util with Apache License 2.0 | 5 votes |
package com.protectwise.testing.ccm import java.io.{FileOutputStream, FileInputStream, BufferedInputStream, File} import java.util.jar.{JarEntry, JarOutputStream} object JarHelper { def createJarForPath(path: File, targetJar: File): File = { def add(source: File, target: JarOutputStream): Unit = { var in: BufferedInputStream = null try { var name = source.getPath.replace("\\", "/").drop(path.getPath.length()+1) if (source.isDirectory && !name.isEmpty && !name.endsWith("/")) name += "/" println(s" $name") if (source.isDirectory) { if (!name.isEmpty) { val entry = new JarEntry(name) entry.setTime(source.lastModified()) target.putNextEntry(entry) target.closeEntry() } source.listFiles.foreach(add(_, target)) return } val entry = new JarEntry(name) entry.setTime(source.lastModified()) target.putNextEntry(entry) in = new BufferedInputStream(new FileInputStream(source)) val buffer = Array.ofDim[Byte](1024) var count = 0 while (count != -1) { count = in.read(buffer) if (count >= 0) target.write(buffer, 0, count) } target.closeEntry() } finally { if (in != null) in.close() } } // val manifest = new java.util.jar.Manifest() val target = new JarOutputStream(new FileOutputStream(targetJar)) add(path, target) target.close() targetJar } }