Extracting single file from zip using gradle task

I’m trying to extract two jars from a zip file which is available inside the zip as below structure.

test.zip
/web-inf
/lib
/result1.jar

Here is my task:-

task unzip(type: Copy){
group ‘testgroup’
def zipFile = file(’$buildDir/test.zip’)
def tree = zipTree(zipFile)
from(tree.matching { include ‘**/result1.jar’})
into ‘$builddir/lib’
includeEmptyDirs = false
}

But I am getting jar file with folder structure like /web-inf/lib/result1.jar.
I want jar file alone, not the folders (/web-inf/lib/).

Please correct what I am doing wrong in the gradle task.

Hi,

The only way I found to answer your question was to reduce the input to provide to the copy task to a single file ditching in the process all references to a tree structure.

plugins {
    id("base") // to get access to the clean task
}

task unzip(type: Copy) {
    // since your zip is in the buildDir, I suppose you'll need deferred evaluation, hence the from(Closure) variant
    from {
        zipTree("$buildDir/lib/test.zip").matching { include '**/result1.jar' }.singleFile
    }
    into buildDir
}