2017-06-15 6 views
-1

私はZipInputStreamを使ってzipファイルを読んでいます。 Zipファイルには4つのcsvファイルがあります。一部のファイルは完全に書き込まれ、一部は部分的に書き込まれます。下記のコードで問題を見つけるのを手伝ってください。 ZipInputStream.readメソッドから読み込みバッファに制限がありますか?ZipInputStreamのZipEntryでの読み取り

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あなたが書き込もうとしたファイルを。

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 
    } 
} 
それはこの(?Scalaの構文を想定し、またはこのKotlin /セイロンである)のようなものでなければなりません

PS:あなたの例では、必要以上に小さいと思われます。}を閉じてください。

関連する問題