Created
April 7, 2010 11:19
-
-
Save loverdos/358760 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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