Search code examples
gradlegroovydependenciesunzip

Gradle How to: Unzip a ZIP file within a ZIP file from a dependency, with incremental build functionality maintained


I have a (main) ZIP archive on a Nexus server with several other ZIP archives inside, and I need to a) extract all **/.hpp from a certain ZIP archive b) extract all **/.hpp from all ZIP archives in the main archive

The only solution I found is to have one Copy task to unpack the main archive in some temp directory and have a second Copy task to look into the temp directory.

What bothers me is that I cannot delete the temp directory, or, if I do this, the first Copy task always downloads and creates the temp directory again - although the second Copy task has nothing to do.

What would be a recommended code pattern to unpack a zip in a zip, without having to maintain some intermediate directory or losing the incremental build functionality? Thanks, Klaus


Solution

  • Something like this

    configurations {
       zip
    } 
    dependencies {
       zip 'foo:bar:1.0@zip'
    }
    task unpackHpp {
       inputs.files configurations.zip
       outputs.dir "$buildDir/hpp" 
       doLast {
          FileTree mainTree = zipTree(configurations.zip.singleFile)
          File zip1 = mainTree.matching { include 'file1.zip'}.singleFile
          File zip2 = mainTree.matching { include 'file2.zip'}.singleFile
          copy {
             from zipTree(zip1).matching { include '**/*.hpp' } 
             from zipTree(zip2).matching { include '**/*.hpp' } 
             from mainTree.matching { include '**/*.hpp' } 
             into "$buildDir/hpp" 
          } 
       } 
    } 
    

    For a bit of further reading, you might be interested in my comments here