-
-
Notifications
You must be signed in to change notification settings - Fork 235
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
45 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
komga/src/main/kotlin/org/gotson/komga/infrastructure/util/ZipFileUtils.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package org.gotson.komga.infrastructure.util | ||
|
||
import org.apache.commons.compress.archivers.zip.ZipFile | ||
import org.gotson.komga.domain.model.EntryNotFoundException | ||
import java.nio.file.Path | ||
|
||
inline fun <R> ZipFile.Builder.use(block: (ZipFile) -> R) = this.get().use(block) | ||
|
||
fun getZipEntryBytes( | ||
path: Path, | ||
entryName: String, | ||
): ByteArray { | ||
// fast path. Only read central directory record and try to find entry in it | ||
val zipBuilder = | ||
ZipFile.builder() | ||
.setPath(path) | ||
.setUseUnicodeExtraFields(true) | ||
.setIgnoreLocalFileHeader(true) | ||
val bytes = zipBuilder.use { it.getEntryBytes(entryName) } | ||
if (bytes != null) return bytes | ||
|
||
// slow path. Entry with that name wasn't in central directory record | ||
// Iterate each entry and, if present, set name from Unicode extra field in local file header | ||
return zipBuilder.setIgnoreLocalFileHeader(false).use { | ||
it.getEntryBytes(entryName) | ||
?: throw EntryNotFoundException("Entry does not exist: $entryName") | ||
} | ||
} | ||
|
||
private fun ZipFile.getEntryBytes(entryName: String) = | ||
this.use { zip -> | ||
zip.getEntry(entryName)?.let { entry -> | ||
zip.getInputStream(entry).use { it.readBytes() } | ||
} | ||
} |