Example usage for java.util.jar JarEntry isDirectory

List of usage examples for java.util.jar JarEntry isDirectory

Introduction

In this page you can find the example usage for java.util.jar JarEntry isDirectory.

Prototype

public boolean isDirectory() 

Source Link

Document

Returns true if this is a directory entry.

Usage

From source file:com.jhash.oimadmin.Utils.java

public static void extractJarFile(String directory, String jarFileName) {
    File baseDir = new File(directory);
    if (baseDir.exists()) {
        if (!baseDir.isDirectory()) {
            throw new InvalidParameterException("Destination directory " + directory + " to expand Jar file "
                    + jarFileName + " is not a directory");
        }/*from w  w  w .  j  av  a 2  s  .  co  m*/
        if (!baseDir.canWrite() || !baseDir.canWrite() || !baseDir.canExecute()) {
            throw new InvalidParameterException("Destination directory " + directory + " to expand Jar file "
                    + jarFileName + " does not have rwx access for user");
        }
    } else {
        baseDir.mkdirs();
    }
    try (JarFile jar = new JarFile(jarFileName)) {
        Enumeration enumEntries = jar.entries();
        while (enumEntries.hasMoreElements()) {
            JarEntry file = (JarEntry) enumEntries.nextElement();
            File f = new File(directory + File.separator + file.getName());
            if (file.isDirectory()) { // if its a directory, create it
                f.mkdirs();
                continue;
            }
            try (java.io.InputStream is = jar.getInputStream(file);
                    java.io.FileOutputStream fos = new java.io.FileOutputStream(f)) {
                // get the input stream
                while (is.available() > 0) { // write contents of 'is' to 'fos'
                    fos.write(is.read());
                }
                fos.close();
                is.close();
            } catch (Exception exception) {
                throw new OIMAdminException("Failed to write the jar file entry " + file + " to location " + f,
                        exception);
            }
        }
    } catch (Exception exception) {
        throw new OIMAdminException("Failed to extract jar file " + jarFileName + " to directory " + directory,
                exception);
    }
}

From source file:org.ngrinder.common.util.CompressionUtils.java

/**
 * Process each jar entry in the  given jar file.
 *
 * @param jarFile   jar file/*from   w  w w. j  a  v a 2  s  .  c  o m*/
 * @param processor jar file entry predicate
 * @throws IOException thrown when having IO problem.
 */
public static void processJarEntries(File jarFile, ZipEntryProcessor processor) {
    try {
        JarFile jarfile = new JarFile(jarFile);
        Enumeration<java.util.jar.JarEntry> enu = jarfile.entries();
        while (enu.hasMoreElements()) {
            JarEntry je = enu.nextElement();
            if (je.isDirectory()) {
                continue;
            }
            processor.process(jarfile, je);
        }
    } catch (IOException e) {
        throw processException("Error while extracting jar file", e);
    }
}

From source file:com.photon.maven.plugins.android.common.JarHelper.java

/** Unjars the specified jar file into the the specified directory
 *
 * @param jarFile//from  www  .j  a  v  a 2 s  .c  o  m
 * @param outputDirectory
 * @param unjarListener
 * @throws IOException
 */
public static void unjar(JarFile jarFile, File outputDirectory, UnjarListener unjarListener)
        throws IOException {
    for (Enumeration en = jarFile.entries(); en.hasMoreElements();) {
        JarEntry entry = (JarEntry) en.nextElement();
        File entryFile = new File(outputDirectory, entry.getName());
        if (unjarListener.include(entry)) {
            if (!entryFile.getParentFile().exists() && !entryFile.getParentFile().mkdirs()) {
                throw new IOException("Error creating output directory: " + entryFile.getParentFile());
            }

            // If the entry is an actual file, unzip that too
            if (!entry.isDirectory()) {
                final InputStream in = jarFile.getInputStream(entry);
                try {
                    final OutputStream out = new FileOutputStream(entryFile);
                    try {
                        IOUtil.copy(in, out);
                    } finally {
                        IOUtils.closeQuietly(out);
                    }
                } finally {
                    IOUtils.closeQuietly(in);
                }
            }
        }
    }
}

From source file:org.apache.hadoop.streaming.StreamUtil.java

static void unJar(File jarFile, File toDir) throws IOException {
    JarFile jar = new JarFile(jarFile);
    try {//w ww. ja va  2s  .  co  m
        Enumeration entries = jar.entries();
        while (entries.hasMoreElements()) {
            JarEntry entry = (JarEntry) entries.nextElement();
            if (!entry.isDirectory()) {
                InputStream in = jar.getInputStream(entry);
                try {
                    File file = new File(toDir, entry.getName());
                    file.getParentFile().mkdirs();
                    OutputStream out = new FileOutputStream(file);
                    try {
                        byte[] buffer = new byte[8192];
                        int i;
                        while ((i = in.read(buffer)) != -1) {
                            out.write(buffer, 0, i);
                        }
                    } finally {
                        out.close();
                    }
                } finally {
                    in.close();
                }
            }
        }
    } finally {
        jar.close();
    }
}

From source file:org.springframework.data.hadoop.mapreduce.ExecutionUtils.java

private static void unjar(Resource jar, File baseDir) throws IOException {
    JarInputStream jis = new JarInputStream(jar.getInputStream());
    JarEntry entry = null;
    try {//from  w ww .j  a  v a2  s .  c om
        while ((entry = jis.getNextJarEntry()) != null) {
            if (!entry.isDirectory()) {
                File file = new File(baseDir, entry.getName());
                if (!file.getParentFile().mkdirs()) {
                    if (!file.getParentFile().isDirectory()) {
                        throw new IOException("Mkdirs failed to create " + file.getParentFile().toString());
                    }
                }
                OutputStream out = new FileOutputStream(file);
                try {
                    byte[] buffer = new byte[8192];
                    int i;
                    while ((i = jis.read(buffer)) != -1) {
                        out.write(buffer, 0, i);
                    }
                } finally {
                    IOUtils.closeStream(out);
                }
            }
        }
    } finally {
        IOUtils.closeStream(jis);
    }
}

From source file:org.schemaspy.util.ResourceWriter.java

/**
 * Copies resources from the jar file of the current thread and extract it
 * to the destination path./*  w  ww .j ava  2  s. c o m*/
 *
 * @param jarConnection
 * @param destPath destination file or directory
 */
private static void copyJarResourceToPath(JarURLConnection jarConnection, File destPath, FileFilter filter) {
    try {
        JarFile jarFile = jarConnection.getJarFile();
        String jarConnectionEntryName = jarConnection.getEntryName();

        /**
         * Iterate all entries in the jar file.
         */
        for (Enumeration<JarEntry> e = jarFile.entries(); e.hasMoreElements();) {
            JarEntry jarEntry = e.nextElement();
            String jarEntryName = jarEntry.getName();

            /**
             * Extract files only if they match the path.
             */
            if (jarEntryName.startsWith(jarConnectionEntryName + "/")) {
                String filename = jarEntryName.substring(jarConnectionEntryName.length());
                File currentFile = new File(destPath, filename);

                if (jarEntry.isDirectory()) {
                    FileUtils.forceMkdir(currentFile);
                } else {
                    if (filter == null || filter.accept(currentFile)) {
                        InputStream is = jarFile.getInputStream(jarEntry);
                        OutputStream out = FileUtils.openOutputStream(currentFile);
                        IOUtils.copy(is, out);
                        is.close();
                        out.close();
                    }
                }
            }
        }
    } catch (IOException e) {
        LOGGER.warn(e.getMessage(), e);
    }
}

From source file:org.apache.cocoon.portal.pluto.Deploy.java

/**
 * Deploy the archive//from  w w  w .jav a 2s.  co m
 * Unpack the archive in the servlet engine context directory
 */
public static void deployArchive(final String webAppsDir, final String warFile, final String warFileName)
        throws IOException {
    System.out.println("Deploying '" + warFileName + "' ...");

    final String destination = webAppsDir + warFileName;

    if (debug) {
        System.out.println("  unpacking '" + warFile + "' ...");
    }
    final JarFile jarFile = new JarFile(warFile);
    final Enumeration files = jarFile.entries();
    while (files.hasMoreElements()) {
        JarEntry entry = (JarEntry) files.nextElement();

        File file = new File(destination, entry.getName());
        File dirF = new File(file.getParent());
        dirF.mkdirs();
        if (entry.isDirectory()) {
            file.mkdirs();
        } else {
            byte[] buffer = new byte[1024];
            int length = 0;
            InputStream fis = jarFile.getInputStream(entry);
            FileOutputStream fos = new FileOutputStream(file);
            while ((length = fis.read(buffer)) >= 0) {
                fos.write(buffer, 0, length);
            }
            fos.close();
        }

    }

    if (debug) {
        System.out.println("Finished!");
    }
}

From source file:com.athomas.androidkickstartr.util.ResourcesUtils.java

private static void copyResourcesToFromJar(File target, URL url) throws IOException {
    JarURLConnection connection = (JarURLConnection) url.openConnection();
    JarFile jarFile = connection.getJarFile();

    Enumeration<JarEntry> entries = jarFile.entries();

    while (entries.hasMoreElements()) {
        JarEntry jarEntry = entries.nextElement();
        InputStream is = jarFile.getInputStream(jarEntry);
        String entryPath = jarEntry.getName();

        File file = null;// w  w  w.j  a  v  a 2  s. c  o  m
        String dirs = "";
        if (entryPath.contains("/")) {
            int lastIndexOf = entryPath.lastIndexOf("/");
            dirs = (String) entryPath.subSequence(0, lastIndexOf + 1);
        }

        File parent = new File(target, dirs);
        parent.mkdirs();

        if (!jarEntry.isDirectory()) {
            String[] splitedPath = entryPath.split("/");
            String fileName = splitedPath[splitedPath.length - 1];
            file = new File(parent, fileName);
            FileUtils.copyInputStreamToFile(is, file);
        }
    }
}

From source file:com.zb.jcseg.core.JcsegTaskConfig.java

public static void unJar(JarFile jar, File toDir) throws IOException {
    try {/* w  w  w .  ja va2  s. c o  m*/
        Enumeration<JarEntry> entries = jar.entries();
        while (entries.hasMoreElements()) {
            JarEntry entry = (JarEntry) entries.nextElement();
            if (!entry.isDirectory()) {
                InputStream in = jar.getInputStream(entry);
                try {
                    File file = new File(toDir, entry.getName());
                    if (!file.getParentFile().mkdirs()) {
                        if (!file.getParentFile().isDirectory()) {
                            throw new IOException("Mkdirs failed to create " + file.getParentFile().toString());
                        }
                    }
                    OutputStream out = new FileOutputStream(file);
                    try {
                        byte[] buffer = new byte[8192];
                        int i;
                        while ((i = in.read(buffer)) != -1) {
                            out.write(buffer, 0, i);
                        }
                    } finally {
                        out.close();
                    }
                } finally {
                    in.close();
                }
            }
        }
    } finally {
        jar.close();
    }
}

From source file:eu.sisob.uma.footils.File.FileFootils.java

public static boolean copyJarResourcesRecursively(final File destDir, final JarURLConnection jarConnection)
        throws IOException {

    final JarFile jarFile = jarConnection.getJarFile();

    for (final Enumeration<JarEntry> e = jarFile.entries(); e.hasMoreElements();) {
        final JarEntry entry = e.nextElement();
        if (entry.getName().startsWith(jarConnection.getEntryName())) {
            final String filename = StringUtils.removeStart(entry.getName(), //
                    jarConnection.getEntryName());

            final File f = new File(destDir, filename);
            if (!entry.isDirectory()) {
                final InputStream entryInputStream = jarFile.getInputStream(entry);
                if (!FileFootils.copyStream(entryInputStream, f)) {
                    return false;
                }//from w w  w.jav a2s.  c o  m
                entryInputStream.close();
            } else {
                if (!FileFootils.ensureDirectoryExists(f)) {
                    throw new IOException("Could not create directory: " + f.getAbsolutePath());
                }
            }
        }
    }
    return true;
}