Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a temporary file from a base64 string with rapture-io

So, basically I want to create a temporary file from a base64 string content. Right now, I'm doing this with native java-io functions. But I would like to achieve the same result using the rapture-io library for scala.

So my question would be, is it possible to achieve this with rapture-io, and if so, how?

I already went through the documentation, but is not specific enough:

https://github.com/propensive/rapture-io/blob/master/doc/introduction.md

Here's my actual code:

import org.apache.commons.codec.binary.Base64
import java.io.FileOutputStream
import java.io.File

val data: String = base64StringContent //Base64 String content of the file.
val fileName = myFileName
val fileExt = myFileExt

//It does write the file in my temp folder.
val file: File = File.createTempFile(fileName, fileExt)
val fileByteArray: Array[Byte] = Base64.decodeBase64(data)
val fileOutFile: FileOutputStream = new FileOutputStream(file)
fileOutFile.write(fileByteArray)
fileOutFile.close()
file.deleteOnExit()
file
like image 547
maya.js Avatar asked Nov 03 '14 15:11

maya.js


1 Answers

Does this work for you?

import rapture.fs.platform.posix
import rapture.io._
import rapture.core._
import rapture.fs._
import strategy.throwExceptions

 val tmpFile  = (File / "tmp").tempFile(prefix = "yourfileName",suffix = ".extension")
 "data" >> tmpFile
 tmpFile.deleteOnExit()
 tmpFile.delete()

Untested on Windows. You might have to use a different separator e.g. \\ instead of /

import rapture.fs.platform.windows
import rapture.io._
import rapture.core._
import rapture.fs._
import strategy.throwExceptions

 val tmpFile  = (File / "C:" / "Windows" / "Temp" ).tempFile(prefix = "yourfileName",suffix = ".extension")
 "data" >> tmpFile
 tmpFile.deleteOnExit()
 tmpFile.delete()
like image 123
bearrito Avatar answered Sep 20 '22 22:09

bearrito