bharathi
bharathi

Reputation: 6271

How can I unzip an uploaded folder in Grails?

Can anyone help me how to unzip a folder in Grails?

Right now I am able to copy the uploaded file into my web-app CSS folder.

    def uploadedFile = request.getFile('payload')
    if(!uploadedFile.empty){
     /* println "Class: ${uploadedFile.class}"
      println "Name: ${uploadedFile.name}"
      println "OriginalFileName: ${uploadedFile.originalFilename}"
      println "Size: ${uploadedFile.size}"
      println "ContentType: ${uploadedFile.contentType}"*/
      def webRootDir = servletContext.getRealPath("/")
      def userDir = new File(webRootDir, "/css")
      userDir.mkdirs()
      uploadedFile.transferTo( new File( userDir, uploadedFile.originalFilename))
      render("Successfully copied to web-app folder")
    }
    else {
        render(view:'config')
    }

But now I need to upload a zip file and from there I need to unzip the files or images into the CSS web-app folder.

Upvotes: 0

Views: 1263

Answers (1)

Szymon Jednac
Szymon Jednac

Reputation: 3007

Using commons-compress is probably the easiest way. Check out this discussion for a minimal example:

org.apache.ant.compress.taskdefs.Unzip u = new Unzip();
u.setSrc(new File("<archive.zip>");
u.setDest(new File("<targetDir>");
u.execute();

Upvotes: 2

Related Questions