Gzip decompression on txt (not tar) files?

I see lots of examples that do something like

tarTree(resources.gzip('archive.tar.gz'))

so Gradle obviously has some support for gzip. However I’m trying to write a Gradle task that simply decompresses a flat gzipped text file, and I’m stuck.

Of course I can do

commandLine 'gunzip', '-k', 'textfile.gz'

and it works (at least under unix) but that’s hardly elegant.

In Ant I can do

<gunzip src="comparison_data/textfile.gz" dest="comparison_data/textfile.txt"/>

. Is there a Gradle equivalent?

As per the Gradle Build Language Reference and Javadoc, ‘project.resources.gzip(‘textfile.gz’).read()’ should give you an ‘InputStream’. Have you tried that?

Ah, beat me to it. Guess I’ll paste my example task in anyway, perhaps it can be instructive:

task gunzip << {
  file('otherFile.txt').text = resources.gzip('test.txt.gz').read().text
}

I was unaware of this .text functionality. However I suspect it may not be a good idea for me because some of the gzip files are huge (some 250MB, some larger.) Does resources.gzip(‘test.txt.gz’).read().text attempt to load the entire thing into memory?

I was expecting there would be some solution of the form

task unzipTheFile(type: Copy) {
 def gzipFile = ...
 def parentDir = ...
     from gzipFile
 into parentDir
}

but perhaps there is not. Everything I’ve tried along those lines has failed, but I’m new at this.

Yes, the file.text pattern would store all of the data in memory. You can always use streams instead to handle large files:

task gunzip << {
  file('targetUncompressedFile.txt').withOutputStream { os ->
    os << resources.gzip('sourceCompressedFile.txt.gz').read()
  }
 }

but as you note, it does not seem like there is a built in gradle way of dealing with single file gzip files.