2017-06-15 103 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。

相關問題