Skip to content

Instantly share code, notes, and snippets.

@loverdos
Created April 7, 2010 11:19
Show Gist options
  • Save loverdos/358760 to your computer and use it in GitHub Desktop.
Save loverdos/358760 to your computer and use it in GitHub Desktop.
import java.util.zip._
import java.io._
class InMemZip(compressionLevel: Int) {
private val _outBytes = new ByteArrayOutputStream
private val _zipOutStream = {
val s = new ZipOutputStream(_outBytes)
s.setLevel(compressionLevel)
s
}
private var _finished = false
def finish(): Unit = {
_zipOutStream.close
_finished = true
}
def getBytes = {
require(_finished, "Must call finish() first")
_outBytes.toByteArray
}
def addEntry(filename: String, data: Array[Byte]): Unit = {
val entry = new ZipEntry(filename)
_zipOutStream.putNextEntry(entry)
_zipOutStream.write(data, 0, data.length)
_zipOutStream.closeEntry
}
def writeToFile(filename: String): File = {
val file = new File(filename)
val fos = new FileOutputStream(file)
fos.write(getBytes)
fos.close
file
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment