Java Utililty Methods Jar Zip File

List of utility methods to do Jar Zip File

Description

The list of methods to do Jar Zip File are organized into topic(s).

Method

voidaddFileToJar(File baseDir, File src, JarOutputStream zOut)
add File To Jar
String name = src.getAbsolutePath();
String prefix = baseDir.getAbsolutePath();
if (prefix.endsWith("/") || prefix.endsWith("\\")) {
    prefix = prefix.substring(0, prefix.length() - 1);
name = name.substring(prefix.length() + 1);
name = name.replace('\\', '/');
long timestamp = src.lastModified();
...
voidaddFileToJar(File file, ZipOutputStream zipStream, String path)
add File To Jar
FileInputStream fin = new FileInputStream(file);
int bufSize = 1024;
byte ipBuf[] = new byte[bufSize];
int lenRead = 0;
String filename = path + file.getName();
zipStream.putNextEntry(new ZipEntry(filename));
while ((lenRead = fin.read(ipBuf)) > 0) {
    zipStream.write(ipBuf, 0, lenRead);
...
voidaddFileToJar(File fileOrDirectoryToAdd, File extractedJarPath, JarOutputStream target)
add File To Jar
String relPath = "";
if (!fileOrDirectoryToAdd.getAbsolutePath().equals(extractedJarPath.getAbsolutePath())) {
    String fileToAddCanonicalPath = fileOrDirectoryToAdd.getCanonicalPath();
    int relStart = extractedJarPath.getCanonicalPath().length() + 1;
    int relEnd = fileOrDirectoryToAdd.getCanonicalPath().length();
    String d = fileToAddCanonicalPath.substring(relStart, relEnd);
    relPath = d.replace("\\", "/");
BufferedInputStream in = null;
try {
    if (fileOrDirectoryToAdd.isDirectory()) {
        if (!relPath.isEmpty()) {
            if (!relPath.endsWith("/")) {
                relPath += "/";
            JarEntry entry = new JarEntry(relPath);
            entry.setTime(fileOrDirectoryToAdd.lastModified());
            target.putNextEntry(entry);
            target.closeEntry();
        for (File nestedFile : fileOrDirectoryToAdd.listFiles()) {
            addFileToJar(nestedFile, extractedJarPath, target);
        return;
    JarEntry entry = new JarEntry(relPath);
    entry.setTime(fileOrDirectoryToAdd.lastModified());
    target.putNextEntry(entry);
    in = new BufferedInputStream(new FileInputStream(fileOrDirectoryToAdd));
    byte[] buffer = new byte[1024];
    while (true) {
        int count = in.read(buffer);
        if (count == -1) {
            break;
        target.write(buffer, 0, count);
    target.closeEntry();
} finally {
    if (in != null) {
        in.close();
voidaddFileToJar(JarOutputStream jar, InputStream file, String path)
Add file to jar with given path
byte[] buffer = new byte[8192];
JarEntry entry = new JarEntry(path);
jar.putNextEntry(entry);
int n;
while ((n = file.read(buffer)) > 0) {
    jar.write(buffer, 0, n);
voidaddJar(File path)
Add a file or directory to the classpath.
try {
    Method method = URLClassLoader.class.getDeclaredMethod("addURL", new Class[] { URL.class });
    method.setAccessible(true);
    method.invoke(classLoader(), new Object[] { path.toURL() });
} catch (Throwable e) {
    throw new RuntimeException(String.format("Error adding %s to classpath", path), e);
voidaddJarLibralyClassPath(Object classLoaderMakedObject, File jarPath)
add Jar Libraly Class Path
URLClassLoader classLoader = (URLClassLoader) classLoaderMakedObject.getClass().getClassLoader();
@SuppressWarnings("rawtypes")
Class classClassLoader = URLClassLoader.class;
Method methodAddUrl = classClassLoader.getDeclaredMethod("addURL", URL.class);
methodAddUrl.setAccessible(true);
methodAddUrl.invoke(classLoader, jarPath.toURI().toURL());
voidaddJarLibralySystemClassPath(File jarPath)
add Jar Libraly System Class Path
URLClassLoader classLoader = (URLClassLoader) ClassLoader.getSystemClassLoader();
@SuppressWarnings("rawtypes")
Class classClassLoader = URLClassLoader.class;
Method methodAddUrl = classClassLoader.getDeclaredMethod("addURL", URL.class);
methodAddUrl.setAccessible(true);
methodAddUrl.invoke(classLoader, jarPath.toURI().toURL());
voidaddJARs(File dir)
Add all the jar files in a directory tree to the classpath.
if (dir.exists() && dir.isDirectory()) {
    File[] files = dir.listFiles();
    for (File file : files) {
        if (file.isFile()) {
            if (file.getName().toLowerCase().endsWith(".jar") && !addFile(file)) {
                System.out.println("Unable to add " + file + " to the classpath.");
        } else if (file.isDirectory())
...
booleanaddJars(File directory, boolean recursive)
add Jars
return addJars(directory, recursive, null);
voidaddJarsToClassPath(String jarPath)
add Jars To Class Path
if (jarPath != null) {
    File f = new File(jarPath);
    try {
        URL u = f.toURI().toURL();
        URLClassLoader urlClassLoader = (URLClassLoader) ClassLoader.getSystemClassLoader();
        Class<?> urlClass = URLClassLoader.class;
        Method method = urlClass.getDeclaredMethod("addURL", new Class[] { URL.class });
        method.setAccessible(true);
...