为什么 readLines 关闭 ZipInputStream?
Why does `readLines` close a ZipInputStream?
在 Groovy (v2.4.1) 中,我试图读取压缩文件中的文件内容,
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
}
读取 zip 中的第一个文件后出现以下错误,
java.io.IOException: Stream closed
为什么会这样? eachLine
和 getText
也一样(没有在 InputStream
中尝试任何其他方法)如何从 ZipInputStream
中读取所有 zip 文件内容16=]?
更新:
虽然我在上面使用了一个文件作为示例,但实际上我只有一个 InputStream
而不是一个物理文件
InputStream
的 readLines()
方法是通过 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 添加的,此方法在读取后关闭流。这就是您的示例失败的原因。
操作方法如下:
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"
}
在 Groovy (v2.4.1) 中,我试图读取压缩文件中的文件内容,
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
}
读取 zip 中的第一个文件后出现以下错误,
java.io.IOException: Stream closed
为什么会这样? eachLine
和 getText
也一样(没有在 InputStream
中尝试任何其他方法)如何从 ZipInputStream
中读取所有 zip 文件内容16=]?
更新:
虽然我在上面使用了一个文件作为示例,但实际上我只有一个 InputStream
而不是一个物理文件
InputStream
的 readLines()
方法是通过 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 添加的,此方法在读取后关闭流。这就是您的示例失败的原因。
操作方法如下:
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"
}