Skip to content

Commit

Permalink
Update getResourceFilePaths code
Browse files Browse the repository at this point in the history
Most of it was written by ChatGPT.
  • Loading branch information
esotericenderman committed Oct 29, 2024
1 parent 124f24c commit e342f12
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
2 changes: 1 addition & 1 deletion lib/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ plugins {
}

group = "foundation.esoteric"
version = "0.2.2-experimental.1"
version = "0.2.2-experimental.2"

repositories {
mavenCentral()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class ResourceUtility {
* @param path The path to the folder in **resources** to get the file paths of.
* @return A set of all the paths of all files stored in the folder specified by the path parameter.
*/
fun getResourceFilePaths(path: Path): Set<Path> {
fun getResourceFilePaths(path: Path): Set<Path> {
val filePaths = mutableSetOf<Path>()

val url = object {}.javaClass.classLoader.getResource(path.toString())?.toURI()
Expand All @@ -38,18 +38,29 @@ class ResourceUtility {
"jar" -> {
println("URL path: " + url.path)

val jarPath = url.path.substringBefore("!").removePrefix("file:")
try {
val jarFileUrl = url.toURL().openConnection() as java.net.JarURLConnection

println("jarPath = " + jarPath)
println("jarFileUrl = " + jarFileUrl)
println("jarFileUrl.jarFileURL.path = " + jarFileUrl.jarFileURL.path)

JarFile(jarPath).use { jarFile ->
val entries = jarFile.entries()
while (entries.hasMoreElements()) {
val entry = entries.nextElement()
if (entry.name.startsWith(path.toString()) && !entry.isDirectory) {
filePaths.add(Paths.get(entry.name))
JarFile(jarFileUrl.jarFileURL.path).use { jarFile ->
val entries = jarFile.entries()

while (entries.hasMoreElements()) {
val entry = entries.nextElement()

println("Entry name: " + entry.name)
println("Entry is directory: " + entry.isDirectory)

if (entry.name.startsWith(path.toString()) && !entry.isDirectory) {
filePaths.add(Paths.get(entry.name))
}
}
}
} catch (e: Exception) {
println("Error accessing JAR file: ${e.message}")
throw IllegalStateException("Failed to access JAR contents", e)
}
}
}
Expand Down

0 comments on commit e342f12

Please sign in to comment.