Skip to content

Commit

Permalink
Added a test to check if a file is a valid .zip. New plublic method i…
Browse files Browse the repository at this point in the history
…sValidZip and unzip now trows an UnzipException if the file is not a valid .zip.

git-svn-id: http://svn.codehaus.org/grails-plugins/grails-grails-ant/trunk@72308 832c1c66-4827-0410-8465-ccb17913cd09
  • Loading branch information
bdrhoa committed Jul 1, 2011
1 parent 86523c1 commit b13be3c
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package org.grails.plugins.grailsant
* Reference: http://ant.apache.org/manual/
*/


class AntUtilsService {

static transactional = false
Expand All @@ -30,34 +31,48 @@ class AntUtilsService {
def unzip(String zipFile, String destDir, String mapperType, Boolean overwrite) {

def ant = new AntBuilder(); // create an antbuilder
if (mapperType) {
ant.unzip( src: zipFile, dest:destDir, overwrite:overwrite) {mapper(type:mapperType)}
} else {
ant.unzip( src: zipFile, dest:destDir, overwrite:overwrite)
if(isValidZip(zipFile)) {
if (mapperType) {
ant.unzip( src: zipFile, dest:destDir, overwrite:overwrite) {mapper(type:mapperType)}
} else {
ant.unzip( src: zipFile, dest:destDir, overwrite:overwrite)
}
}
else {
throw new UnzipException(
message: "Invalid zip file.", fileName: zipFile)
}

}

def zip(String destfile,String basedir) {
return zip(destfile,basedir,"**/*.*", "")
}





def zip(String destfile,String basedir,String includes) {
return zip(destfile,basedir,includes, "")
return zip(destfile,basedir,includes, "")
}


def zip(String destfile,String basedir,String includes,String excludes) {

def ant = new AntBuilder(); // create an antbuilder

ant.zip(destfile: destfile,
basedir: basedir,
includes: includes,
excludes: excludes)


}

def isValidZip(String file){
def theFile = new File(file)
return isValidZip(theFile)
}


def isValidZip(File file) {
return AntUtils.isValid(file)
}

}
18 changes: 0 additions & 18 deletions plugin.xml
Original file line number Diff line number Diff line change
@@ -1,18 +0,0 @@
<plugin name='grails-ant' version='0.1.1' grailsVersion='1.2.3 &gt; *'>
<author>Brad Rhoads</author>
<authorEmail>[email protected]</authorEmail>
<title>Grails Ant Plugin</title>
<description>
Provides ant to Grails applications
</description>
<documentation>http://grails.org/plugin/grails-ant</documentation>
<resources>
<resource>BuildConfig</resource>
<resource>Config</resource>
<resource>DataSource</resource>
<resource>UrlMappings</resource>
<resource>org.grails.plugins.grailsant.AntUtilsService</resource>
</resources>
<dependencies />
<behavior />
</plugin>
8 changes: 8 additions & 0 deletions src/groovy/org/grails/plugins/grailsant/UnzipException.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

package org.grails.plugins.grailsant


class UnzipException extends RuntimeException {
String message
String fileName
}
39 changes: 39 additions & 0 deletions src/java/org/grails/plugins/grailsant/AntUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package org.grails.plugins.grailsant;

import java.io.File;
import java.io.IOException;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;


public class AntUtils {

/**
* Tests if a file is a valid .zip.
*
* Reference: http://stackoverflow.com/questions/2085637/java-how-to-check-if-a-generated-zip-file-is-corrupted
*
* @param file - the file to test
* @return true if file is a valid .zip, false otherwise
*/
static boolean isValid(final File file) {
ZipFile zipfile = null;
try {
zipfile = new ZipFile(file);
return true;
} catch (ZipException e) {
return false;
} catch (IOException e) {
return false;
} finally {
try {
if (zipfile != null) {
zipfile.close();
zipfile = null;
}
} catch (IOException e) {
}
}
}

}

0 comments on commit b13be3c

Please sign in to comment.