From a6f36de6a6e19d2ec976ed30dba8c4174d25deed Mon Sep 17 00:00:00 2001 From: Cedric Beust Date: Fri, 23 Feb 2018 00:59:13 -0800 Subject: [PATCH] Create directories before actual entries in the jar file. --- .../com/beust/kobalt/archive/MetaArchive.kt | 35 +++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/archive/MetaArchive.kt b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/archive/MetaArchive.kt index 23b7843b..9d00c8d1 100644 --- a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/archive/MetaArchive.kt +++ b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/archive/MetaArchive.kt @@ -29,6 +29,7 @@ class MetaArchive(outputFile: File, val manifest: Manifest?) : Closeable { // If no manifest was passed, create an empty one so it's the first one in the archive val m = manifest ?: Manifest() val manifestFile = File.createTempFile("kobalt", "tmpManifest") + addEntry(ZipArchiveEntry("META-INF/"), null) if (manifest != null) { FileOutputStream(manifestFile).use { fos -> m.write(fos) @@ -40,10 +41,16 @@ class MetaArchive(outputFile: File, val manifest: Manifest?) : Closeable { fun addFile(f: File, entryFile: File, path: String?) { + maybeCreateParentDirectories(f) + addFile2(f, entryFile, path) + } + + private fun addFile2(f: File, entryFile: File, path: String?) { val file = f.normalize() FileInputStream(file).use { inputStream -> val actualPath = KFiles.fixSlashes(if (path != null) path + entryFile.path else entryFile.path) ZipArchiveEntry(actualPath).let { entry -> + maybeCreateParentDirectories(File(actualPath)) maybeAddEntry(entry) { addEntry(entry, inputStream) } @@ -51,6 +58,30 @@ class MetaArchive(outputFile: File, val manifest: Manifest?) : Closeable { } } + private val createdDirs = hashSetOf() + + /** + * For an entry a/b/c/File, an entry needs to be created for each individual directory: + * a/ + * a/b/ + * a/b/c + * a/b/c/File + */ + private fun maybeCreateParentDirectories(file: File) { + val toCreate = arrayListOf() + var current = file.parentFile + while (current != null && current.path != ".") { + if (!createdDirs.contains(current.path)) { + toCreate.add(0, KFiles.fixSlashes(current) + "/") + createdDirs.add(current.path) + } + current = current.parentFile + } + toCreate.forEach { dir -> + addEntry(ZipArchiveEntry(dir), null) + } + } + fun addArchive(jarFile: File) { ApacheZipFile(jarFile).use { jar -> val jarEntries = jar.entries @@ -73,9 +104,9 @@ class MetaArchive(outputFile: File, val manifest: Manifest?) : Closeable { override fun close() = zos.close() - private fun addEntry(entry: ArchiveEntry, inputStream: FileInputStream) { + private fun addEntry(entry: ArchiveEntry, inputStream: FileInputStream?) { zos.putArchiveEntry(entry) - inputStream.use { ins -> + inputStream?.use { ins -> ins.copyTo(zos, 50 * 1024) } zos.closeArchiveEntry()