Esto es un poco más estilo Scala en caso de que como funcional:
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()
}
}
y no se olvide de las importaciones:
import java.io.{BufferedReader, FileOutputStream, File}
import java.util.zip.{ZipEntry, ZipOutputStream}
import io.Source
Usted debe 'in.close()' cuando haya terminado con 'in'. – leedm777
Sí, por supuesto, lo he arreglado ahora. –
Y sé que no está optimizado, pero no hay necesidad de envolver el byte en una matriz. Simplemente puede 'zip.write (b)'. – leedm777