* FileUtil: Make it easier to compile without Pack200, or with

Apache Harmony's Pack200, add unzip to main()
This commit is contained in:
zzz
2010-10-24 16:49:20 +00:00
parent ed399a07d8
commit 4bb902a8b9
3 changed files with 67 additions and 6 deletions

View File

@ -6,6 +6,14 @@
<!--
<property name="javac.compilerargs" value="-warn:-unchecked,raw,unused,serial" />
-->
<!-- Add Apache Harmony's Pack200 library if you don't have java.util.jar.Pack200
See core/java/src/net/i2p/util/FileUtil.java for code changes required
to use this library instead of Sun's version.
Or to comment it all out if you don't have either.
-->
<!--
<property name="javac.classpath" value="/PATH/TO/pack200.jar" />
-->
<!-- You probably don't want to change anything from here down -->
<target name="help" depends="all" />

View File

@ -17,10 +17,11 @@
</target>
<!-- only used if not set by a higher build.xml -->
<property name="javac.compilerargs" value="" />
<property name="javac.classpath" value="" />
<target name="compile" depends="depend">
<mkdir dir="./build" />
<mkdir dir="./build/obj" />
<javac srcdir="./src" debug="true" source="1.5" target="1.5" deprecation="on" destdir="./build/obj" >
<javac srcdir="./src" debug="true" source="1.5" target="1.5" deprecation="on" destdir="./build/obj" classpath="${javac.classpath}" >
<compilerarg line="${javac.compilerargs}" />
</javac>
</target>

View File

@ -13,10 +13,18 @@ import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.jar.JarOutputStream;
import java.util.jar.Pack200;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
// Pack200 import
// you must also uncomment the correct line in unpack() below
// For gcj, gij, etc., comment both out
//
// For Sun, OpenJDK, IcedTea, etc, use this
import java.util.jar.Pack200;
// For Apache Harmony or if you put its pack200.jar in your library directory use this
//import org.apache.harmony.unpack200.Archive;
/**
* General helper methods for messing with files
@ -119,7 +127,7 @@ public class FileUtil {
if (entry.getName().endsWith(".jar.pack") || entry.getName().endsWith(".war.pack")) {
target = new File(targetDir, entry.getName().substring(0, entry.getName().length() - ".pack".length()));
JarOutputStream fos = new JarOutputStream(new FileOutputStream(target));
Pack200.newUnpacker().unpack(in, fos);
unpack(in, fos);
fos.close();
System.err.println("INFO: File [" + entry.getName() + "] extracted and unpacked");
} else {
@ -189,9 +197,7 @@ public class FileUtil {
} else {
if (p200TestRequired &&
(entry.getName().endsWith(".jar.pack") || entry.getName().endsWith(".war.pack"))) {
try {
Class.forName("java.util.jar.Pack200", false, ClassLoader.getSystemClassLoader());
} catch (Exception e) { // ClassNotFoundException but compiler not happy with that
if (!isPack200Supported()) {
System.err.println("ERROR: Zip verify failed, your JVM does not support unpack200");
return false;
}
@ -224,6 +230,40 @@ public class FileUtil {
}
}
/**
* This won't work right if one of the two options in unpack() is commented out.
* @since 0.8.1
*/
private static boolean isPack200Supported() {
try {
Class.forName("java.util.jar.Pack200", false, ClassLoader.getSystemClassLoader());
return true;
} catch (Exception e) {}
try {
Class.forName("org.apache.harmony.pack200.Archive", false, ClassLoader.getSystemClassLoader());
return true;
} catch (Exception e) {}
return false;
}
/**
* Caller must close streams
* @since 0.8.1
*/
private static void unpack(InputStream in, JarOutputStream out) throws Exception {
// For Sun, OpenJDK, IcedTea, etc, use this
Pack200.newUnpacker().unpack(in, out);
// ------------------
// For Apache Harmony or if you put its pack200.jar in your library directory use this
//(new Archive(in, out)).unpack();
// ------------------
// For gcj, gij, etc., use this
//throw new IOException("Pack200 not supported");
}
/**
* Read in the last few lines of a (newline delimited) textfile, or null if
* the file doesn't exist.
@ -352,6 +392,18 @@ public class FileUtil {
boolean copied = FileUtil.copy(args[1], args[2], false);
if (!copied)
System.err.println("Error copying [" + args[1] + "] to [" + args[2] + "]");
} else if ("unzip".equals(args[0])) {
File f = new File(args[1]);
File to = new File("tmp");
to.mkdir();
boolean copied = verifyZip(f);
if (!copied)
System.err.println("Error verifying " + args[1]);
copied = extractZip(f, to);
if (copied)
System.err.println("Unzipped [" + args[1] + "] to [" + to + "]");
else
System.err.println("Error unzipping [" + args[1] + "] to [" + to + "]");
}
}