I am trying to stream a zip file.
The following chunk of code prints line by line as expected:
val inputStream = new GZIPInputStream(new FileInputStream("/some/path"))
val source = Source.fromInputStream(inputStream)
for(line <- source.getLines) {
println(line)
}
But this one doesn't do anyting (it doesn't even exit):
val inputStream = new ZipInputStream(new FileInputStream("/some/path"))
val source = Source.fromInputStream(inputStream)
for(line <- source.getLines) {
println(line)
}
The only difference is the usage of GZIPInputStream instead of ZipInputStream. Both class implements InputStream.
Am I missing something ? Or is there any workaroud ?
Gzip is just a compressed file, that can be de-compressed on the fly as you are reading from
Source. Zip isn't really a stream, it's just one of many java misnomers (take a look at the interface), it's more like a directory, containing several files, that you can traverse viaZipEntry, and read each one separately viaSource. There is no real content at the top level, just a directory listing, so no "lines" to fetch viaSource.In a nutshell, you just iterate through entries, creating a new Source for each one. Something like this:
(This creates a
Mapof name of each file in zip to its entire content n memory, probably, not what you want at all, just an illustration of what you can do to access that content viaSource)