Skip to content

Commit

Permalink
Simplify JarBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
Goooler committed Dec 20, 2024
1 parent c6006a8 commit 4b96aa2
Showing 1 changed file with 6 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,17 @@ import java.util.jar.JarEntry
import java.util.jar.JarOutputStream

class JarBuilder(os: OutputStream) {
private val entries = mutableListOf<String>()
private val entries = mutableSetOf<String>()
private val jos = JarOutputStream(os)

private fun addDirectory(name: String) {
if (!entries.contains(name)) {
if (entries.add(name)) {
val parent = name.substringBeforeLast('/', "")
if (parent.isNotEmpty() && !entries.contains(parent)) {
addDirectory(parent)
}

// directory entries must end in "/"
val entry = JarEntry("$name/")
jos.putNextEntry(entry)
entries.add(name)
jos.putNextEntry(JarEntry("$name/"))
}
}

Expand All @@ -27,11 +24,9 @@ class JarBuilder(os: OutputStream) {
if (idx != -1) {
addDirectory(path.substring(0, idx))
}
if (!entries.contains(path)) {
val entry = JarEntry(path)
jos.putNextEntry(entry)
entries.add(path)
data.byteInputStream().use { it.copyTo(jos) }
if (entries.add(path)) {
jos.putNextEntry(JarEntry(path))
data.byteInputStream().copyTo(jos)
}
return this
}
Expand Down

0 comments on commit 4b96aa2

Please sign in to comment.