Skip to content

Instantly share code, notes, and snippets.

@samuelorji
Last active December 23, 2019 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samuelorji/76a5918d66cb3d2ddb9f25b5426036fc to your computer and use it in GitHub Desktop.
Save samuelorji/76a5918d66cb3d2ddb9f25b5426036fc to your computer and use it in GitHub Desktop.
zip task
lazy val zip = taskKey[Unit]("zip files") //define the key
zip := { // assigning a value to the zip key of type Unit
val logger: Logger = sLog.value //get logger from SBT
//where to zip from
val dirFromZip: File = srcFile.value
//where to zip to
val dirToZip: File = baseDirectory.value / "build.sbt.zip"
logger.info(s"zipping files from ${dirFromZip.absolutePath}")
// actual zip function
val fos = new FileOutputStream(toZip)
val fis = new FileInputStream(dirFromZip)
val zipOutputStream = new ZipOutputStream(fos)
val zipEntry = new ZipEntry(fromZip.getName)
zipOutputStream.putNextEntry(zipEntry)
@scala.annotation.tailrec
def readAgain(c : Int) : Int = {
if(c < 0){
-1
}else{
zipOutputStream.write(c)
readAgain(fis.read())
}
}
readAgain(fis.read())
zipOutputStream.close()
fis.close()
fos.close()
//end of zip function
logger.info(s"zipped files to ${dirToZip.absolutePath}")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment