Questo è un po 'più stile scala nel caso in cui vi piace funzionale:
def compress(zipFilepath: String, files: List[File]) {
def readByte(bufferedReader: BufferedReader): Stream[Int] = {
bufferedReader.read() #:: readByte(bufferedReader)
}
val zip = new ZipOutputStream(new FileOutputStream(zipFilepath))
try {
for (file <- files) {
//add zip entry to output stream
zip.putNextEntry(new ZipEntry(file.getName))
val in = Source.fromFile(file.getCanonicalPath).bufferedReader()
try {
readByte(in).takeWhile(_ > -1).toList.foreach(zip.write(_))
}
finally {
in.close()
}
zip.closeEntry()
}
}
finally {
zip.close()
}
}
e non dimenticate le importazioni:
import java.io.{BufferedReader, FileOutputStream, File}
import java.util.zip.{ZipEntry, ZipOutputStream}
import io.Source
fonte
2012-10-12 14:42:36
Si dovrebbe 'in.close()' quando hai finito con 'tale dicitura. – leedm777
Sì, certo, l'ho risolto ora. –
E so che non è ottimizzato, ma non è necessario avvolgere il byte in un array. Puoi semplicemente 'zip.write (b)'. – leedm777