java.util.jar.JarOutputStream Scala Examples
The following examples show how to use java.util.jar.JarOutputStream.
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: ManifestJarGenerator.scala From coursier with Apache License 2.0 | 5 votes |
package coursier.launcher import java.io.{ByteArrayOutputStream, OutputStream} import java.nio.file.{Files, Path} import java.util.jar.{Attributes, JarOutputStream, Manifest} import coursier.launcher.internal.FileUtil object ManifestJarGenerator extends Generator[Parameters.ManifestJar] { def generate(parameters: Parameters.ManifestJar, output: Path): Unit = { val cp = parameters.classpath.map(_.toURI.getRawPath).mkString(" ") val manifest = new Manifest val attr = manifest.getMainAttributes attr.put(Attributes.Name.MANIFEST_VERSION, "1.0") attr.put(Attributes.Name.CLASS_PATH, cp) attr.put(Attributes.Name.MAIN_CLASS, parameters.mainClass) val content = { val baos = new ByteArrayOutputStream val jos = new JarOutputStream(baos, manifest) jos.close() baos.close() baos.toByteArray() } FileUtil.withOutputStream(output) { os => for (p <- parameters.preambleOpt.map(_.value)) os.write(p) os.write(content) } FileUtil.tryMakeExecutable(output) } }
Example 2
Source File: Using.scala From Argus-SAF with Apache License 2.0 | 5 votes |
package org.argus.jawa.core.compiler.compile.io import java.io.{Closeable, FileInputStream, FileOutputStream, InputStream, OutputStream, File => JavaFile} import java.io.{BufferedInputStream, BufferedOutputStream, InputStreamReader, OutputStreamWriter} import java.io.{BufferedReader, BufferedWriter} import java.util.zip.GZIPInputStream import java.net.URL import java.nio.channels.FileChannel import java.nio.charset.Charset import java.util.jar.{JarFile, JarInputStream, JarOutputStream} import java.util.zip.{GZIPOutputStream, ZipEntry, ZipFile, ZipInputStream, ZipOutputStream} import ErrorHandling.translate import scala.reflect.{Manifest => SManifest} abstract class Using[Source, T] { protected def open(src: Source): T def apply[R](src: Source)(f: T => R): R = { val resource = open(src) try { f(resource) } finally { close(resource) } } protected def close(out: T): Unit } abstract class WrapUsing[Source, T](implicit srcMf: SManifest[Source], targetMf: SManifest[T]) extends Using[Source, T] { protected def label[S](m: SManifest[S]): String = m.runtimeClass.getSimpleName protected def openImpl(source: Source): T protected final def open(source: Source): T = translate("Error wrapping " + label(srcMf) + " in " + label(targetMf) + ": ") { openImpl(source) } } trait OpenFile[T] extends Using[JavaFile, T] { protected def openImpl(file: JavaFile): T protected final def open(file: JavaFile): T = { val parent = file.getParentFile if(parent != null) IO.createDirectory(parent) openImpl(file) } } object Using { def wrap[Source, T<: Closeable](openF: Source => T)(implicit srcMf: SManifest[Source], targetMf: SManifest[T]): Using[Source,T] = wrap(openF, closeCloseable) def wrap[Source, T](openF: Source => T, closeF: T => Unit)(implicit srcMf: SManifest[Source], targetMf: SManifest[T]): Using[Source,T] = new WrapUsing[Source, T] { def openImpl(source: Source): T = openF(source) def close(t: T): Unit = closeF(t) } def resource[Source, T <: Closeable](openF: Source => T): Using[Source,T] = resource(openF, closeCloseable) def resource[Source, T](openF: Source => T, closeF: T => Unit): Using[Source,T] = new Using[Source,T] { def open(s: Source): T = openF(s) def close(s: T): Unit = closeF(s) } def file[T <: Closeable](openF: JavaFile => T): OpenFile[T] = file(openF, closeCloseable) def file[T](openF: JavaFile => T, closeF: T => Unit): OpenFile[T] = new OpenFile[T] { def openImpl(file: JavaFile): T = openF(file) def close(t: T): Unit = closeF(t) } private def closeCloseable[T <: Closeable]: T => Unit = _.close() def bufferedOutputStream: Using[OutputStream, BufferedOutputStream] = wrap((out: OutputStream) => new BufferedOutputStream(out) ) def bufferedInputStream: Using[InputStream, BufferedInputStream] = wrap((in: InputStream) => new BufferedInputStream(in) ) def fileOutputStream(append: Boolean = false): OpenFile[BufferedOutputStream] = file(f => new BufferedOutputStream(new FileOutputStream(f, append))) def fileInputStream: OpenFile[BufferedInputStream] = file(f => new BufferedInputStream(new FileInputStream(f))) def urlInputStream: Using[URL, BufferedInputStream] = resource((u: URL) => translate("Error opening " + u + ": ")(new BufferedInputStream(u.openStream))) def fileOutputChannel: OpenFile[FileChannel] = file(f => new FileOutputStream(f).getChannel) def fileInputChannel: OpenFile[FileChannel] = file(f => new FileInputStream(f).getChannel) def fileWriter(charset: Charset = IO.utf8, append: Boolean = false): OpenFile[BufferedWriter] = file(f => new BufferedWriter(new OutputStreamWriter(new FileOutputStream(f, append), charset)) ) def fileReader(charset: Charset): OpenFile[BufferedReader] = file(f => new BufferedReader(new InputStreamReader(new FileInputStream(f), charset)) ) def urlReader(charset: Charset): Using[URL, BufferedReader] = resource((u: URL) => new BufferedReader(new InputStreamReader(u.openStream, charset))) def jarFile(verify: Boolean): OpenFile[JarFile] = file(f => new JarFile(f, verify), (_: JarFile).close()) def zipFile: OpenFile[ZipFile] = file(f => new ZipFile(f), (_: ZipFile).close()) def streamReader: Using[(InputStream, Charset), InputStreamReader] = wrap{ (_: (InputStream, Charset)) match { case (in, charset) => new InputStreamReader(in, charset) } } def gzipInputStream: Using[InputStream, GZIPInputStream] = wrap((in: InputStream) => new GZIPInputStream(in, 8192) ) def zipInputStream: Using[InputStream, ZipInputStream] = wrap((in: InputStream) => new ZipInputStream(in)) def zipOutputStream: Using[OutputStream, ZipOutputStream] = wrap((out: OutputStream) => new ZipOutputStream(out)) def gzipOutputStream: Using[OutputStream, GZIPOutputStream] = wrap((out: OutputStream) => new GZIPOutputStream(out, 8192), (_: GZIPOutputStream).finish()) def jarOutputStream: Using[OutputStream, JarOutputStream] = wrap((out: OutputStream) => new JarOutputStream(out)) def jarInputStream: Using[InputStream, JarInputStream] = wrap((in: InputStream) => new JarInputStream(in)) def zipEntry(zip: ZipFile): Using[ZipEntry, InputStream] = resource((entry: ZipEntry) => translate("Error opening " + entry.getName + " in " + zip + ": ") { zip.getInputStream(entry) } ) }
Example 3
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 4
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 5
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 6
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 } }