From f675500213f2aa6401c34c0c3a16a73bfd331d84 Mon Sep 17 00:00:00 2001 From: johnsonlee Date: Mon, 4 Sep 2023 19:46:09 +0900 Subject: [PATCH] Fix issue #422: end-of-stream caused by multiple collector --- .../booster/transform/util/Collectors.kt | 30 ++++++++----------- .../transform/util/CompositeCollector.kt | 3 +- 2 files changed, 14 insertions(+), 19 deletions(-) diff --git a/booster-transform-util/src/main/kotlin/com/didiglobal/booster/transform/util/Collectors.kt b/booster-transform-util/src/main/kotlin/com/didiglobal/booster/transform/util/Collectors.kt index fafd6f804..595c46d5f 100644 --- a/booster-transform-util/src/main/kotlin/com/didiglobal/booster/transform/util/Collectors.kt +++ b/booster-transform-util/src/main/kotlin/com/didiglobal/booster/transform/util/Collectors.kt @@ -3,11 +3,10 @@ package com.didiglobal.booster.transform.util import com.didiglobal.booster.kotlinx.search import com.didiglobal.booster.transform.AbstractTransformContext import com.didiglobal.booster.transform.TransformContext -import org.apache.commons.compress.archivers.ArchiveEntry -import org.apache.commons.compress.archivers.ArchiveStreamFactory import java.io.File -import java.io.IOException import java.util.regex.Pattern +import java.util.zip.ZipEntry +import java.util.zip.ZipFile typealias Collector = com.didiglobal.booster.transform.Collector @@ -88,22 +87,17 @@ fun File.collect(collector: Collector): List = when { collector.collect(base.relativize(f.toURI()).normalize().path, f::readBytes) } } - this.isFile -> { - this.inputStream().buffered().use { - ArchiveStreamFactory().createArchiveInputStream(it).let { archive -> - generateSequence { - try { - archive.nextEntry - } catch (e: IOException) { - null - } - }.filterNot(ArchiveEntry::isDirectory).filter { entry -> - collector.accept(entry.name) - }.map { entry -> - collector.collect(entry.name, archive::readBytes) - }.toList() - } + this.isFile -> when (this.extension.lowercase()) { + "jar", "zip" -> ZipFile(this).use { zip -> + zip.entries().asSequence().filterNot(ZipEntry::isDirectory).filter { entry -> + collector.accept(entry.name) + }.map { entry -> + collector.collect(entry.name) { + zip.getInputStream(entry).readBytes() + } + }.toList() } + else -> emptyList() } else -> emptyList() } diff --git a/booster-transform-util/src/main/kotlin/com/didiglobal/booster/transform/util/CompositeCollector.kt b/booster-transform-util/src/main/kotlin/com/didiglobal/booster/transform/util/CompositeCollector.kt index 3ca22c1e4..82e172942 100644 --- a/booster-transform-util/src/main/kotlin/com/didiglobal/booster/transform/util/CompositeCollector.kt +++ b/booster-transform-util/src/main/kotlin/com/didiglobal/booster/transform/util/CompositeCollector.kt @@ -16,10 +16,11 @@ class CompositeCollector(private val collectors: Iterable>) : Colle } override fun collect(name: String, data: () -> ByteArray): List<*> { + val bytes by lazy(data) // to avoid end-of-stream caused by multiple readers return collectors.filter { it.accept(name) }.mapNotNull { - it.collect(name, data) + it.collect(name) { bytes } } }