|
@@ -15,15 +15,18 @@ package org.apache.hadoop.util;
|
|
|
|
|
|
import com.google.common.base.Preconditions;
|
|
import com.google.common.base.Preconditions;
|
|
|
|
|
|
|
|
+import java.io.BufferedOutputStream;
|
|
import java.io.File;
|
|
import java.io.File;
|
|
import java.io.FileInputStream;
|
|
import java.io.FileInputStream;
|
|
import java.io.FileOutputStream;
|
|
import java.io.FileOutputStream;
|
|
import java.io.IOException;
|
|
import java.io.IOException;
|
|
import java.io.InputStream;
|
|
import java.io.InputStream;
|
|
|
|
+import java.io.OutputStream;
|
|
import java.net.URL;
|
|
import java.net.URL;
|
|
import java.net.URLDecoder;
|
|
import java.net.URLDecoder;
|
|
import java.text.MessageFormat;
|
|
import java.text.MessageFormat;
|
|
import java.util.Enumeration;
|
|
import java.util.Enumeration;
|
|
|
|
+import java.util.jar.JarFile;
|
|
import java.util.jar.JarOutputStream;
|
|
import java.util.jar.JarOutputStream;
|
|
import java.util.jar.Manifest;
|
|
import java.util.jar.Manifest;
|
|
import java.util.zip.ZipEntry;
|
|
import java.util.zip.ZipEntry;
|
|
@@ -37,10 +40,37 @@ import java.util.zip.ZipOutputStream;
|
|
*/
|
|
*/
|
|
public class JarFinder {
|
|
public class JarFinder {
|
|
|
|
|
|
- private static void zipDir(File dir, String relativePath, ZipOutputStream zos)
|
|
|
|
|
|
+ private static void copyToZipStream(InputStream is, ZipEntry entry,
|
|
|
|
+ ZipOutputStream zos) throws IOException {
|
|
|
|
+ zos.putNextEntry(entry);
|
|
|
|
+ byte[] arr = new byte[4096];
|
|
|
|
+ int read = is.read(arr);
|
|
|
|
+ while (read > -1) {
|
|
|
|
+ zos.write(arr, 0, read);
|
|
|
|
+ read = is.read(arr);
|
|
|
|
+ }
|
|
|
|
+ is.close();
|
|
|
|
+ zos.closeEntry();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static void jarDir(File dir, String relativePath, ZipOutputStream zos)
|
|
throws IOException {
|
|
throws IOException {
|
|
Preconditions.checkNotNull(relativePath, "relativePath");
|
|
Preconditions.checkNotNull(relativePath, "relativePath");
|
|
Preconditions.checkNotNull(zos, "zos");
|
|
Preconditions.checkNotNull(zos, "zos");
|
|
|
|
+
|
|
|
|
+ // by JAR spec, if there is a manifest, it must be the first entry in the
|
|
|
|
+ // ZIP.
|
|
|
|
+ File manifestFile = new File(dir, JarFile.MANIFEST_NAME);
|
|
|
|
+ ZipEntry manifestEntry = new ZipEntry(JarFile.MANIFEST_NAME);
|
|
|
|
+ if (!manifestFile.exists()) {
|
|
|
|
+ zos.putNextEntry(manifestEntry);
|
|
|
|
+ new Manifest().write(new BufferedOutputStream(zos));
|
|
|
|
+ zos.closeEntry();
|
|
|
|
+ } else {
|
|
|
|
+ InputStream is = new FileInputStream(manifestFile);
|
|
|
|
+ copyToZipStream(is, manifestEntry, zos);
|
|
|
|
+ }
|
|
|
|
+ zos.closeEntry();
|
|
zipDir(dir, relativePath, zos, true);
|
|
zipDir(dir, relativePath, zos, true);
|
|
zos.close();
|
|
zos.close();
|
|
}
|
|
}
|
|
@@ -62,17 +92,12 @@ public class JarFinder {
|
|
zipDir(file, relativePath + f.getName() + "/", zos, false);
|
|
zipDir(file, relativePath + f.getName() + "/", zos, false);
|
|
}
|
|
}
|
|
else {
|
|
else {
|
|
- ZipEntry anEntry = new ZipEntry(relativePath + f.getName());
|
|
|
|
- zos.putNextEntry(anEntry);
|
|
|
|
- InputStream is = new FileInputStream(f);
|
|
|
|
- byte[] arr = new byte[4096];
|
|
|
|
- int read = is.read(arr);
|
|
|
|
- while (read > -1) {
|
|
|
|
- zos.write(arr, 0, read);
|
|
|
|
- read = is.read(arr);
|
|
|
|
|
|
+ String path = relativePath + f.getName();
|
|
|
|
+ if (!path.equals(JarFile.MANIFEST_NAME)) {
|
|
|
|
+ ZipEntry anEntry = new ZipEntry(path);
|
|
|
|
+ InputStream is = new FileInputStream(f);
|
|
|
|
+ copyToZipStream(is, anEntry, zos);
|
|
}
|
|
}
|
|
- is.close();
|
|
|
|
- zos.closeEntry();
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -88,9 +113,8 @@ public class JarFinder {
|
|
jarDir));
|
|
jarDir));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- JarOutputStream zos = new JarOutputStream(new FileOutputStream(jarFile),
|
|
|
|
- new Manifest());
|
|
|
|
- zipDir(dir, "", zos);
|
|
|
|
|
|
+ JarOutputStream zos = new JarOutputStream(new FileOutputStream(jarFile));
|
|
|
|
+ jarDir(dir, "", zos);
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -142,5 +166,4 @@ public class JarFinder {
|
|
}
|
|
}
|
|
return null;
|
|
return null;
|
|
}
|
|
}
|
|
-
|
|
|
|
}
|
|
}
|