Reputation: 4572
In Groovy (v2.4.1), I'm trying to read the contents of the files within a zipped file,
import java.util.zip.*
ZipInputStream zis = new ZipInputStream(new FileInputStream("d:\\temp\\small.zip"))
while (zipEntry = zis.nextEntry) {
println "Reading ${zipEntry.name}..."
def filedata = zis.readLines()
println filedata
}
gives the following error after reading the first file in the zip,
java.io.IOException: Stream closed
Why does that happen? It is same with eachLine
and getText
too ( haven't tried any other methods in InputStream
) How can I read all the zip file contents from a ZipInputStream
in groovy
?
Update:
Though I've used a file as an example above, I actually only have an InputStream
and not a physical file
Upvotes: 1
Views: 813
Reputation: 84784
InputStream
's readLines()
method is added via this class. When you look at line no 791 you will see that readLines()
is delegated to the overridden version with Reader
as an argument. As it can be seen in the docs this method closes the stream after reading it. That's why the your example fails.
Here's how it can be done:
import java.util.zip.*
def zis = new ZipInputStream(new FileInputStream('lol.zip'))
while (zipEntry = zis.nextEntry) {
println "Reading $zipEntry.name"
def output = new ByteArrayOutputStream()
output << zis
println "Output: $output"
}
Upvotes: 4