I have successfully, on my "local-build", been able to create a temp-folder and add image files inside of it to be zipped and downloaded by a user. Unfortunately after deploying to my Test-Server, I am unable to create such temp folder and thusly cannot zip and stream it, I believe, due to permission errors. Basically I am at a pass. I cannot gain access to create folders on my Test-Server, and will either need to store this folder and files on my S3 bucket and then create a zipOutputStream from here -or- which I think may be a better solution if it is possible, is to just "On-The-Fly" send the zip content to response before I complete the zip creation. Is this possible? And if so how would go about doing so? Is there a benefit to this method over storing the files temporarily on S3 to be zipped and streamed.
Current Code for Folder Creation and Zipping and Streaming
def downloadZip(){
def fName = params.fName // ZipFile Name passed in 'example.zip'
def fLoc = params.fLoc //Folder Location passed in '/example'
def user = User.get( fLoc as Long ) //Get the Users files to be zipped
def urlList = []
List ownedIds
//Create a temporary directory to place files inside before zipping
new File(fLoc).mkdir()
//Dynamic Resource 'http://example.com/' -or- 'http://localhost:8080/'
def location = "${resource( dir:'/', absolute:true )}"
//Collect and Download QR-Codes image files
ownedIds = user.geolinks.collect {
//Define Url for Download
def urls = (location+"qrCode/download?u=http%3A%2F%2Fqr.ageoa.com%2F" +it.linkAddress+ "&s=150&n=" +it.linkAddress)
//Download each QR-Code
download2(urls,fLoc)
}
//ZIP the directory that was created and filled with the QR codes
String zipFileName = fName
String inputDir = fLoc
ZipOutputStream zipFile = new ZipOutputStream(new FileOutputStream(zipFileName))
new File(inputDir).eachFile() { file ->
zipFile.putNextEntry(new ZipEntry(file.getName()))
def buffer = new byte[1024]
file.withInputStream { i ->
def l = i.read(buffer)
// check whether the file is empty
if (l > 0) {
zipFile.write(buffer, 0, l)
}
}
zipFile.closeEntry()
}
zipFile.close()
//Download QR-Code Zip-File
try {
def file = new File(fName)
response.setContentType("application/octet-stream")
response.setHeader("Content-disposition", "attachment;filename=${file.getName()}")
response.outputStream << file.newInputStream() // Performing a binary stream copy
}
catch(Exception e){
e.printStackTrace()
}
//Delete Temporary Folder
def dir2 = new File(fLoc)
dir2.deleteDir()
}
//Download All QR-Codes images to folder [userID]
def download2(address, dir){
def file = new FileOutputStream(dir+"/"+address.tokenize("&n=")[-1]+".png")
if(file){
def out = new BufferedOutputStream(file)
out << new URL(address).openStream()
out.close()
}
}
Right, this should do it, let me know if any of it doesn't make sense...