Skip to content

Commit

Permalink
Fixed issue with duplicate archive entries.
Browse files Browse the repository at this point in the history
  • Loading branch information
durganmcbroom committed Jul 22, 2023
1 parent 2c8cc24 commit 1bbe93a
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 6 deletions.
4 changes: 1 addition & 3 deletions src/main/kotlin/net/yakclient/archives/zip/ZipReference.kt
Expand Up @@ -48,9 +48,7 @@ internal class ZipReference(
val alreadyRead = HashSet<String>()

return (overrides.keys.asSequence() + zip.entries().asSequence().map { it.name })
.filter {
!alreadyRead.contains(it)
}.mapNotNull(::of).onEach { ensureOpen() }
.filter(alreadyRead::add).mapNotNull(::of).onEach { ensureOpen() }
}
}

Expand Down
51 changes: 48 additions & 3 deletions src/test/kotlin/net/yakclient/archives/test/zip/TestZipArchives.kt
Expand Up @@ -16,9 +16,18 @@ import java.util.jar.JarFile
import java.util.jar.JarOutputStream

class TestZipArchives {
private fun setupEmptyJar() : Path {
private fun setupJar(
vararg entries: Pair<String, String>
) : Path {
val createTempFile = Files.createTempFile(UUID.randomUUID().toString(), ".jar")
JarOutputStream(FileOutputStream(createTempFile.toFile())).use { target ->
for (entry in entries) {
target.putNextEntry(JarEntry(entry.first))

target.write(entry.second.toByteArray())

target.closeEntry()
}
// val entry = JarEntry()
//
// target.putNextEntry(entry)
Expand Down Expand Up @@ -49,7 +58,7 @@ class TestZipArchives {

@Test
fun `Create zip archive`() {
val setupEmptyJar = setupEmptyJar()
val setupEmptyJar = setupJar()
val zip = ZipReference(JarFile(setupEmptyJar.toFile()), setupEmptyJar.toUri())

check(zip.reader.entries().sumOf { 1L } == 0L)
Expand All @@ -74,7 +83,7 @@ class TestZipArchives {

@Test
fun `Remove from zip archive`() {
val setupEmptyJar = setupEmptyJar()
val setupEmptyJar = setupJar()
val zip = ZipReference(JarFile(setupEmptyJar.toFile()), setupEmptyJar.toUri())

check(zip.reader.entries().sumOf { 1L } == 0L)
Expand All @@ -98,4 +107,40 @@ class TestZipArchives {

check(zip.reader.entries().sumOf { 1L } == 0L)
}

@Test
fun `Test no duplicates`() {
val setupEmptyJar = setupJar(
"c" to "Go watch",
"a" to "A jazz musician",
"b" to "Named: ",
"e" to "Patrick Bartley"
)
val archive = ZipReference(JarFile(setupEmptyJar.toFile()), setupEmptyJar.toUri())

val nanResource = object: SafeResource {
override val uri: URI
get() = throw UnsupportedOperationException("")

override fun open(): InputStream {
throw UnsupportedOperationException("")
}
}

fun ZipReference.put(name: String) {
writer.put(ArchiveReference.Entry(
name, nanResource, false, archive
))
}

archive.put("c")
archive.put("a")
archive.put("d")

val read = HashSet<String>()

archive.reader.entries().forEach {
assert(read.add(it.name)) {"Duplicates found!"}
}
}
}

0 comments on commit 1bbe93a

Please sign in to comment.