From d18b4e8ccb256ecf74ef64e43211ac94bc7cd69e Mon Sep 17 00:00:00 2001 From: "Erik C. Thauvin" Date: Tue, 11 Apr 2017 16:54:43 -0700 Subject: [PATCH] Added MANIFEST creation. --- libs/fatjar-0.1.jar | 4 ++-- src/main/kotlin/com/example/Main.kt | 29 ++++++++++++++++++++++------- 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/libs/fatjar-0.1.jar b/libs/fatjar-0.1.jar index 26eb2ce..c8eec90 100644 --- a/libs/fatjar-0.1.jar +++ b/libs/fatjar-0.1.jar @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:637f87433d5a688192869489ecce116591c3d7215639a64550a981e5624c5994 -size 4036 +oid sha256:9bcb64e84639e11c6d4a56adccdf2942a6596f0427fcec1d9cc05df066f4d1b5 +size 5312 diff --git a/src/main/kotlin/com/example/Main.kt b/src/main/kotlin/com/example/Main.kt index f23f51c..8d86ba7 100644 --- a/src/main/kotlin/com/example/Main.kt +++ b/src/main/kotlin/com/example/Main.kt @@ -6,6 +6,7 @@ import java.util.* import kotlin.system.measureTimeMillis internal var allFilesPredicate: ZipArchiveEntryPredicate = ZipArchiveEntryPredicate { true } +internal val MANIFEST = "MANIFEST.MF" fun main(args: Array) { val kobalt = File("kobalt-1.0.58.jar") @@ -19,8 +20,7 @@ fun main(args: Array) { // Straight raw copy, not very flexible fun rezip(jarIn: File, zipOut: File) { val time = measureTimeMillis { - val zos = ZipArchiveOutputStream(zipOut) - zos.encoding = "UTF-8" + val zos = ZipArchiveOutputStream(zipOut).apply { encoding = "UTF-8" } val zip = ZipFile(jarIn) zip.copyRawEntries(zos, allFilesPredicate) @@ -35,8 +35,7 @@ fun rezip(jarIn: File, zipOut: File) { // Raw copy: jar -> zip fun rezip2(jarIn: File, zipOut: File) { val time = measureTimeMillis { - val zos = ZipArchiveOutputStream(zipOut) - zos.encoding = "UTF-8" + val zos = ZipArchiveOutputStream(zipOut).apply { encoding = "UTF-8" } val zip = ZipFile(jarIn) for (entry in zip.entries) { @@ -54,8 +53,7 @@ fun rezip2(jarIn: File, zipOut: File) { // Raw copy: jar x 2 -> zip fun rezip3(jarIn: File, srcJar: File, zipOut: File) { val time = measureTimeMillis { - val zos = ZipArchiveOutputStream(zipOut) - zos.encoding = "UTF-8" + val zos = ZipArchiveOutputStream(zipOut).apply { encoding = "UTF-8" } val jar = ZipFile(jarIn) // get the jar entries @@ -63,7 +61,9 @@ fun rezip3(jarIn: File, srcJar: File, zipOut: File) { // copy the entries for (entry in jar.entries) { - zos.addRawArchiveEntry(entry, jar.getRawInputStream(entry)) + if (!entry.name.endsWith(MANIFEST)) { + zos.addRawArchiveEntry(entry, jar.getRawInputStream(entry)) + } } jar.close() @@ -78,6 +78,18 @@ fun rezip3(jarIn: File, srcJar: File, zipOut: File) { } } + val tmp = File.createTempFile(MANIFEST, ".tmp") + tmp.bufferedWriter().use { out -> + out.write("Manifest-Version: 1.0\r\nMain-Class: com.beust.kobalt.MainKt\r\n") + } + + val entry = ZipArchiveEntry(tmp, "META-INF/$MANIFEST") + zos.putArchiveEntry(entry) + tmp.inputStream().use { ins -> + ins.copyTo(zos, 50 * 1024) + } + zos.closeArchiveEntry() + src.close() zos.close() } @@ -88,6 +100,9 @@ fun rezip3(jarIn: File, srcJar: File, zipOut: File) { // Look for duplicate entries fun entryExists(jarEntries: Enumeration, entry: ZipArchiveEntry): Boolean { for (e in jarEntries) { + if (e.name.endsWith(MANIFEST)) { + return true + } if (e.name == entry.name) { return true }