我正在使用ZipInputStream读取zip文件。Zip文件有4个csv文件。有些文件是完全写的,有些是部分写的。请帮我找出下面的代码问题。从ZipInputStream.read方法读取缓冲区有任何限制吗?
val zis = new ZipInputStream(inputStream)
Stream.continually(zis.getNextEntry).takeWhile(_ != null).foreach { file =>
if (!file.isDirectory && file.getName.endsWith(".csv")) {
val buffer = new Array[Byte](file.getSize.toInt)
zis.read(buffer)
val fo = new FileOutputStream("c:\\temp\\input\\" + file.getName)
fo.write(buffer)
}发布于 2017-06-16 05:54:23
您没有closed/flush编辑您试图写入的文件。应该是这样的(假设Scala语法,或者这是Kotlin/锡兰?):
val fo = new FileOutputStream("c:\\temp\\input\\" + file.getName)
try {
fo.write(buffer)
} finally {
fo.close
}此外,如果有必要,您应该检查读取计数,并阅读更多内容,如下所示:
var readBytes = 0
while (readBytes < buffer.length) {
val r = zis.read(buffer, readBytes, buffer.length - readBytes)
r match {
case -1 => throw new IllegalStateException("Read terminated before reading everything")
case _ => readBytes += r
}
}PS:在您的示例中,它似乎低于关闭}的要求。
https://stackoverflow.com/questions/44578574
复制相似问题