Last active
March 9, 2016 20:35
Create a zip file using remote sources (S3) and then download that zip file in Scala
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.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