Extract only some folders into a directory

I have a .zip file with a complex hierarchy of folders. I’d like to extract only the ones that ends with .framework and put them into a target directory so that I have all the frameworks flattened.
I was thinking of something like:

task<Copy>("extractFirebaseIosZip") {
    dependsOn(downloadFirebaseIos)
    group = "ios firebase setup"
    from(zipTree(downloadFirebaseIos.dest)) {
        include("*.framework")
        eachFile { path = name }
    }
    into("$buildDir/firebaseios/${downloadFirebaseIos.dest.nameWithoutExtension}")
}

Or maybe:

task<Copy>("extractFirebaseIosZip") {
    dependsOn(downloadFirebaseIos)
    group = "ios firebase setup"
    from(zipTree(downloadFirebaseIos.dest).matching {
        include("*.framework/**")
    }) {
        eachFile {
            println(path)
            path = path.substringAfter(".framework")
                .removePrefix("/")
                .removePrefix("\\")
                .let { "$name$it" }
        }
    }
    into("$buildDir/firebaseios/${downloadFirebaseIos.dest.nameWithoutExtension}")
}

Of course with that code I am filtering only the folders themselves without the actual content. Also since I did not set includeEmptyDirs = true the output is none.

What would be the smart way to do so, (in Kotlin)?

Solved :slight_smile:

task<Copy>("extractFirebaseIosZip") {
    dependsOn(downloadFirebaseIos)
    group = "ios firebase setup"
    from(zipTree(downloadFirebaseIos.dest).matching {
        include("**/*.framework/**")
    }) {
        val frameRegex = Regex(".*[\\/\\\\](.*\\.framework[\\/\\\\].*)")
        eachFile {
            val newPath = frameRegex.matchEntire(path)!!.groups[1]!!.value
            println("$path->$newPath")
            path = newPath
        }
    }
    into("$buildDir/firebaseios/${downloadFirebaseIos.dest.nameWithoutExtension}")
}

Thanks to this response! Hope may helps.