Skip to content

Instantly share code, notes, and snippets.

@mashhoodr
Last active March 9, 2016 20:35
Create a zip file using remote sources (S3) and then download that zip file in Scala
import java.io.{File}
import java.util.zip.{ZipEntry, ZipOutputStream}
val zipFilePath = "/tmp/allLogs.zip"
val zip = new ZipOutputStream(new FileOutputStream(zipFilePath))
for (log <- logs) {
val link = file.link.get
val fileName = link.substring(link.lastIndexOf('/') + 1, link.lastIndexOf(".zip") + 4) // getting file name and removing the AWS params from the link
val source = scala.io.Source.fromURL(link, "ISO-8859-1") // this is because I am zipping binary files, if your encoding is different please pass likewise
zip.putNextEntry(new ZipEntry(fileName))
zip.write(source.map(_.toByte).toArray)
source.close()
zip.closeEntry()
}
zip.close()
Ok.sendFile(content= new File(zipFilePath))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment