Skip to content

Instantly share code, notes, and snippets.

/Otsos.scala Secret

Created August 21, 2016 23:40
Show Gist options
  • Save anonymous/bb1eed2c4a15a35949741b4c34639e55 to your computer and use it in GitHub Desktop.
Save anonymous/bb1eed2c4a15a35949741b4c34639e55 to your computer and use it in GitHub Desktop.
def zip(name: String, postType: String) = Action {
val enumerator = Enumerator.outputStream { os =>
val zip = new ZipOutputStream(os)
getAllBlogPosts(name, getPostType(postType)).foreach { x =>
val url = new URL(x)
val con = url.openConnection()
val cc = con.asInstanceOf[HttpURLConnection].getResponseCode
println(x + " | status: " + cc)
if (cc == 200) {
val in = new BufferedInputStream(url.openStream())
zip.putNextEntry(new ZipEntry(x.reverse.takeWhile('/' !=).reverse))
var b = in.read()
while (b > -1) {
zip.write(b)
b = in.read()
}
in.close()
zip.closeEntry()
}
}
zip.close()
}
Ok.chunked(enumerator >>> Enumerator.eof).withHeaders(
"Content-Type" -> "application/zip",
"Content-Disposition" -> "attachment; filename=archive.zip"
)
}
def outputStream(a: java.io.OutputStream => Unit): Enumerator[Array[Byte]] = {
Concurrent.unicast[Array[Byte]] { channel =>
val outputStream = new java.io.OutputStream() {
override def close() = channel.end()
override def flush() {}
override def write(value: Int) = channel.push(Array(value.toByte))
override def write(buffer: Array[Byte]) = write(buffer, 0, buffer.length)
override def write(buffer: Array[Byte], start: Int, count: Int) =
channel.push(buffer.slice(start, start + count))
}
a(outputStream)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment