1
0
Fork 0
mirror of https://github.com/ethauvin/kobalt.git synced 2025-04-26 00:17:11 -07:00

Create directories before actual entries in the jar file.

This commit is contained in:
Cedric Beust 2018-02-23 00:59:13 -08:00
parent df89933cc8
commit a6f36de6a6

View file

@ -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 // If no manifest was passed, create an empty one so it's the first one in the archive
val m = manifest ?: Manifest() val m = manifest ?: Manifest()
val manifestFile = File.createTempFile("kobalt", "tmpManifest") val manifestFile = File.createTempFile("kobalt", "tmpManifest")
addEntry(ZipArchiveEntry("META-INF/"), null)
if (manifest != null) { if (manifest != null) {
FileOutputStream(manifestFile).use { fos -> FileOutputStream(manifestFile).use { fos ->
m.write(fos) m.write(fos)
@ -40,10 +41,16 @@ class MetaArchive(outputFile: File, val manifest: Manifest?) : Closeable {
fun addFile(f: File, entryFile: File, path: String?) { 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() val file = f.normalize()
FileInputStream(file).use { inputStream -> FileInputStream(file).use { inputStream ->
val actualPath = KFiles.fixSlashes(if (path != null) path + entryFile.path else entryFile.path) val actualPath = KFiles.fixSlashes(if (path != null) path + entryFile.path else entryFile.path)
ZipArchiveEntry(actualPath).let { entry -> ZipArchiveEntry(actualPath).let { entry ->
maybeCreateParentDirectories(File(actualPath))
maybeAddEntry(entry) { maybeAddEntry(entry) {
addEntry(entry, inputStream) addEntry(entry, inputStream)
} }
@ -51,6 +58,30 @@ class MetaArchive(outputFile: File, val manifest: Manifest?) : Closeable {
} }
} }
private val createdDirs = hashSetOf<String>()
/**
* 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<String>()
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) { fun addArchive(jarFile: File) {
ApacheZipFile(jarFile).use { jar -> ApacheZipFile(jarFile).use { jar ->
val jarEntries = jar.entries val jarEntries = jar.entries
@ -73,9 +104,9 @@ class MetaArchive(outputFile: File, val manifest: Manifest?) : Closeable {
override fun close() = zos.close() override fun close() = zos.close()
private fun addEntry(entry: ArchiveEntry, inputStream: FileInputStream) { private fun addEntry(entry: ArchiveEntry, inputStream: FileInputStream?) {
zos.putArchiveEntry(entry) zos.putArchiveEntry(entry)
inputStream.use { ins -> inputStream?.use { ins ->
ins.copyTo(zos, 50 * 1024) ins.copyTo(zos, 50 * 1024)
} }
zos.closeArchiveEntry() zos.closeArchiveEntry()