How to replace the file insize zip in Gradle

I need to replace the file inside zip file, what is the approach i can take. plase advise?

How is the zip created in the first place? Is it created by your gradle build? Or is it created externally?

If the zip is created inside your Gradle build there are opportunities to alter the zip before it’s created. If the zip is created exernally you could do something like:

task alterZip(type: Zip) {
   from zipTree('path/to.zip').matching {
      exclude 'somepath/replaceme.xml'
   }
   into('somepath') {
      from 'overridepath/replaceme.xml'
   } 
} 

You could make the above task more future proof by having a “smarter” exclude (ie exclude everything in the zip where a matching path exists in the override folder)

1 Like
  1. First I have tried to delete the existing file in Zip using exclude as below. But it do not remove the file
    task alterZip(type: Zip) {
    from zipTree(’$buildDir/zip/abc.zip’).matching {
    exclude ‘application/configuration_files/abc.list’
    }
    }

  2. I have a doubt, what is the somepath in the below section.Is it full path to the file inside zip file ? I tried to use relative path inside zip file, but it did not copy any files to the given folder?

into(‘somepath’) {
from ‘overridepath/replaceme.xml’
}