2017-06-15 28 views
-1

我正在使用ZipInputStream读取zip文件。 Zip文件有4个csv文件。有些文件是完全写入的,有些是部分写入的。请用下面的代码帮我找到问题。从ZipInputStream.read方法读取缓冲区有没有限制?ZipEntry中的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) 
} 

回答

0

你没有close d/flush ED你试图写入文件。它应该是这样的(假设Scala的语法,或者是这个科特林/锡兰):

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:在您的示例中,它似乎低于要求关闭} s。