Skip to content
Advertisement

Guava Resources.readLines() for Zip/Gzip files

I’ve found the Resources.readLines() and Files.readLines() to be helpfull in simplifiying my code.
The problem is that I often read gzip-compressed txt-files or txt-files in zip archives from URL’s (HTTP and FTP).
Is there a way to use Guava’s methods to read from these URL’s too? Or is that only possible with Java’s GZIPInputStream/ZipInputStream?

Advertisement

Answer

You can create your own ByteSources:

For GZip:

JavaScript

Then use it:

JavaScript

Here is the implementation for the Zip. This assumes that you read only one entry.

JavaScript

And you can use it like this:

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement