grailsgroovyzipstream

Send the zip content to response before you complete the zip creation


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()
    }
}

Solution

  • Right, this should do it, let me know if any of it doesn't make sense...

    // A list of things to download and add to the zip
    List<URL> testList = [ 'http://cdn.sstatic.net/stackoverflow/img/sprites.png?v=6',
                           'https://www.google.co.uk/images/srpr/logo4w.png' ]*.toURL()
    response.setHeader( "Content-disposition", "attachment; filename=resources.zip" )
    response.contentType = "application/octet-stream"
    
    // Wrap the response stream in a zipoutputstream
    new ZipOutputStream( response.outputStream ).withStream { zos ->
    
        // Always add a root folder to zip files, not to do so is spiteful
        zos.putNextEntry( new ZipEntry( "resources/" ) )
    
        // For each URL
        testList.each { res ->
    
            // Get a name for this file in the zip 
    
            // This bit might be the tricky bit as I guess you don't know the file
            // names. So instead of this you might need to check the response
            // object from opening a connection to the URL.  However, without a
            // working example URL from you, I can't be sure :-(
            String name = res.path.split( '/' )[ -1 ]
    
            // Create a zip entry for it
            zos.putNextEntry( new ZipEntry( "resources/$name" ) )
    
            // Write the resource stream into our zip
            res.withInputStream { ins ->
                zos << ins
            }
    
            // Close this resource
            zos.closeEntry() 
        }
        // Close the root folder
        zos.closeEntry()
    }