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:org.kitodo.serviceloader.KitodoServiceLoader.java

/**
 * Checks, whether a passed jarFile has frontend files or not. Returns true,
 * when the jar contains a folder with the name "resources"
 *
 * @param jarFile//from ww  w  . ja v  a  2  s  . c  om
 *            jarFile that will be checked for frontend files
 *
 * @return boolean
 */
private boolean hasFrontendFiles(JarFile jarFile) {
    Enumeration enums = jarFile.entries();
    while (enums.hasMoreElements()) {
        JarEntry jarEntry = (JarEntry) enums.nextElement();
        if (jarEntry.getName().contains(RESOURCES_FOLDER) && jarEntry.isDirectory()) {
            return true;
        }
    }
    return false;
}

From source file:com.kotcrab.vis.editor.module.editor.PluginLoaderModule.java

private void loadJarClasses(URLClassLoader classLoader, PluginDescriptor descriptor,
        Enumeration<JarEntry> entries) throws ClassNotFoundException {
    while (entries.hasMoreElements()) {
        JarEntry entry = entries.nextElement();
        if (entry.isDirectory() || entry.getName().endsWith(".class") == false)
            continue;

        String className = entry.getName().substring(0, entry.getName().length() - ".class".length());
        className = className.replace('/', '.');
        Class<?> clazz = classLoader.loadClass(className);

        if (clazz.getAnnotation(VisPlugin.class) != null)
            descriptor.pluginClasses.add(clazz);
    }/*from  w  w  w.j a va2  s.  c o  m*/
}

From source file:org.echocat.nodoodle.transport.HandlerUnpacker.java

protected SplitResult splitMultipleJarIntoJars(InputStream inputStream, File targetDirectory)
        throws IOException {
    if (inputStream == null) {
        throw new NullPointerException();
    }/*  ww w.j  a va  2s  . c o  m*/
    if (targetDirectory == null) {
        throw new NullPointerException();
    }
    final Collection<File> jarFiles = new ArrayList<File>();
    final File mainJarFile = getMainJarFile(targetDirectory);
    jarFiles.add(mainJarFile);
    final JarInputStream jarInput = new JarInputStream(inputStream);
    final Manifest manifest = jarInput.getManifest();
    final FileOutputStream mainJarFileStream = new FileOutputStream(mainJarFile);
    try {
        final JarOutputStream jarOutput = new JarOutputStream(mainJarFileStream, manifest);
        try {
            JarEntry entry = jarInput.getNextJarEntry();
            while (entry != null) {
                final String entryName = entry.getName();
                if (!entry.isDirectory() && entryName.startsWith("lib/")) {
                    final File targetFile = new File(targetDirectory, entryName);
                    if (!targetFile.getParentFile().mkdirs()) {
                        throw new IOException("Could not create parent directory of " + targetFile + ".");
                    }
                    final OutputStream outputStream = new FileOutputStream(targetFile);
                    try {
                        IOUtils.copy(jarInput, outputStream);
                    } finally {
                        IOUtils.closeQuietly(outputStream);
                    }
                    jarFiles.add(targetFile);
                } else {
                    if (entryName.startsWith(TransportConstants.CLASSES_PREFIX)
                            && entryName.length() > TransportConstants.CLASSES_PREFIX.length()) {
                        try {
                            ZIP_ENTRY_NAME_FIELD.set(entry, entryName.substring(CLASSES_PREFIX.length()));
                        } catch (IllegalAccessException e) {
                            throw new RuntimeException("Could not set " + ZIP_ENTRY_NAME_FIELD + ".", e);
                        }
                    }
                    jarOutput.putNextEntry(entry);
                    IOUtils.copy(jarInput, jarOutput);
                    jarOutput.closeEntry();
                }
                entry = jarInput.getNextJarEntry();
            }
        } finally {
            IOUtils.closeQuietly(jarOutput);
        }
    } finally {
        IOUtils.closeQuietly(mainJarFileStream);
    }
    return new SplitResult(Collections.unmodifiableCollection(jarFiles), manifest);
}

From source file:io.sightly.tck.TCK.java

private void extract(String extractDir) throws IOException {
    File extractFolder = new File(extractDir);
    if (extractFolder.exists()) {
        if (!extractFolder.isDirectory()) {
            throw new IOException("File entry " + extractFolder.getAbsolutePath()
                    + " already exists and it is not a folder.");
        }//w w  w  . ja  va2  s.c o m
        if (!extractFolder.canWrite()) {
            throw new IOException(
                    "Folder " + extractFolder.getAbsolutePath() + " exists but it is not writable.");
        }
    } else {
        if (!extractFolder.mkdirs()) {
            throw new IOException("Unable to create folder " + extractFolder.getAbsolutePath() + ".");
        }
    }
    Enumeration<JarEntry> entries = jarFile.entries();
    while (entries.hasMoreElements()) {
        JarEntry entry = entries.nextElement();
        String entryName = entry.getName();
        if (entryName.startsWith(TESTFILES)) {
            File file = new File(extractFolder, entryName);
            if (entry.isDirectory()) {
                if (!file.mkdir()) {
                    throw new IOException("Unable to create folder " + file.getAbsolutePath());
                }
                continue;
            }
            InputStream is = null;
            FileOutputStream fos = null;
            try {
                is = jarFile.getInputStream(entry);
                fos = new FileOutputStream(file);
                while (is.available() > 0) {
                    fos.write(is.read());
                }
                fos.close();
                is.close();
            } catch (IOException e) {
                LOG.error("Unable to extract file " + file.getAbsolutePath());
            } finally {
                if (fos != null) {
                    fos.close();
                }
                if (is != null) {
                    is.close();
                }
            }
        }
    }
}

From source file:org.abs_models.backend.erlang.ErlApp.java

private void copyJarDirectory(JarFile jarFile, String inname, String outname) throws IOException {
    InputStream is = null;//from w  ww .j  a  v a  2  s.  c o  m
    for (JarEntry entry : Collections.list(jarFile.entries())) {
        if (entry.getName().startsWith(inname)) {
            String relFilename = entry.getName().substring(inname.length());
            if (!entry.isDirectory()) {
                is = jarFile.getInputStream(entry);
                ByteStreams.copy(is, Files.asByteSink(new File(outname, relFilename)).openStream());
            } else {
                new File(outname, relFilename).mkdirs();
            }
        }
    }
    is.close();
}

From source file:net.sf.mavenjython.JythonMojo.java

private Collection<File> extractAllFiles(File outputDirectory, ZipFile ja, Enumeration<JarEntry> en)
        throws MojoExecutionException {
    List<File> files = new ArrayList<File>();
    while (en.hasMoreElements()) {
        JarEntry el = en.nextElement();
        // getLog().info(" > " + el);
        if (!el.isDirectory()) {
            File destFile = new File(outputDirectory, el.getName());
            // destFile = new File(outputDirectory, destFile.getName());
            if (OVERRIDE || !destFile.exists()) {
                destFile.getParentFile().mkdirs();
                try {
                    FileOutputStream fo = new FileOutputStream(destFile);
                    IOUtils.copy(ja.getInputStream(el), fo);
                    fo.close();//from   w  ww  .  j av  a2  s .  com
                } catch (IOException e) {
                    throw new MojoExecutionException(
                            "extracting " + el.getName() + " from jython artifact jar failed", e);
                }
            }
            files.add(destFile);
        }
    }
    return files;
}

From source file:com.android.build.gradle.integration.application.ExternalBuildPluginTest.java

@Test
public void testBuild() throws ProcessException, IOException, ParserConfigurationException, SAXException {
    FileUtils.write(mProject.getBuildFile(),
            "" + "apply from: \"../commonHeader.gradle\"\n" + "buildscript {\n "
                    + "  apply from: \"../commonBuildScript.gradle\"\n" + "}\n" + "\n"
                    + "apply plugin: 'base'\n" + "apply plugin: 'com.android.external.build'\n" + "\n"
                    + "externalBuild {\n" + "  executionRoot = $/" + mProject.getTestDir().getAbsolutePath()
                    + "/$\n" + "  buildManifestPath = $/" + manifestFile.getAbsolutePath() + "/$\n" + "}\n");

    mProject.executor().withInstantRun(23, ColdswapMode.AUTO).withPackaging(mPackaging).run("clean", "process");

    InstantRunBuildContext instantRunBuildContext = loadFromBuildInfo();
    assertThat(instantRunBuildContext.getPreviousBuilds()).hasSize(1);
    assertThat(instantRunBuildContext.getLastBuild()).isNotNull();
    assertThat(instantRunBuildContext.getLastBuild().getArtifacts()).hasSize(1);
    InstantRunBuildContext.Build fullBuild = instantRunBuildContext.getLastBuild();
    assertThat(fullBuild.getVerifierStatus().get()).isEqualTo(InstantRunVerifierStatus.INITIAL_BUILD);
    assertThat(fullBuild.getArtifacts()).hasSize(1);
    InstantRunBuildContext.Artifact artifact = fullBuild.getArtifacts().get(0);
    assertThat(artifact.getType()).isEqualTo(InstantRunBuildContext.FileType.MAIN);
    assertThat(artifact.getLocation().exists()).isTrue();

    ApkSubject apkSubject = expect.about(ApkSubject.FACTORY).that(artifact.getLocation());
    apkSubject.contains("instant-run.zip");
    assertThat(apkSubject.hasMainDexFile());

    // now perform a hot swap test.
    File mainClasses = new File(mProject.getTestDir(), "jars/main/classes.jar");
    assertThat(mainClasses.exists()).isTrue();

    File originalFile = new File(mainClasses.getParentFile(), "original_classes.jar");
    assertThat(mainClasses.renameTo(originalFile)).isTrue();

    try (JarFile inputJar = new JarFile(originalFile);
            JarOutputStream jarOutputFile = new JarOutputStream(new BufferedOutputStream(
                    new FileOutputStream(new File(mainClasses.getParentFile(), "classes.jar"))))) {
        Enumeration<JarEntry> entries = inputJar.entries();
        while (entries.hasMoreElements()) {
            JarEntry element = entries.nextElement();
            try (InputStream inputStream = new BufferedInputStream(inputJar.getInputStream(element))) {
                if (!element.isDirectory()) {
                    jarOutputFile.putNextEntry(new ZipEntry(element.getName()));
                    try {
                        if (element.getName().contains("MainActivity.class")) {
                            // perform hot swap change
                            byte[] classBytes = new byte[(int) element.getSize()];
                            ByteStreams.readFully(inputStream, classBytes);
                            classBytes = hotswapChange(classBytes);
                            jarOutputFile.write(classBytes);
                        } else {
                            ByteStreams.copy(inputStream, jarOutputFile);
                        }/*from  w  ww .ja v a2 s .c  o m*/
                    } finally {
                        jarOutputFile.closeEntry();
                    }
                }
            }
        }
    }

    mProject.executor().withInstantRun(23, ColdswapMode.AUTO).withPackaging(mPackaging).run("process");

    instantRunBuildContext = loadFromBuildInfo();
    assertThat(instantRunBuildContext.getPreviousBuilds()).hasSize(2);
    InstantRunBuildContext.Build lastBuild = instantRunBuildContext.getLastBuild();
    assertThat(lastBuild).isNotNull();
    assertThat(lastBuild.getVerifierStatus().isPresent());
    assertThat(lastBuild.getVerifierStatus().get()).isEqualTo(InstantRunVerifierStatus.COMPATIBLE);
    assertThat(lastBuild.getArtifacts()).hasSize(1);
    artifact = lastBuild.getArtifacts().get(0);
    assertThat(artifact.getType()).isEqualTo(InstantRunBuildContext.FileType.RELOAD_DEX);
    assertThat(artifact.getLocation()).isNotNull();
    File dexFile = artifact.getLocation();
    assertThat(dexFile.exists()).isTrue();
    DexFileSubject reloadDex = expect.about(DexFileSubject.FACTORY).that(dexFile);
    reloadDex.hasClass("Lcom/android/tools/fd/runtime/AppPatchesLoaderImpl;").that();
    reloadDex.hasClass("Lcom/example/jedo/blazeapp/MainActivity$override;").that();
}

From source file:org.apache.storm.localizer.LocallyCachedTopologyBlob.java

protected void extractDirFromJar(String jarpath, String dir, Path dest) throws IOException {
    LOG.debug("EXTRACTING {} from {} and placing it at {}", dir, jarpath, dest);
    try (JarFile jarFile = new JarFile(jarpath)) {
        String toRemove = dir + '/';
        Enumeration<JarEntry> jarEnums = jarFile.entries();
        while (jarEnums.hasMoreElements()) {
            JarEntry entry = jarEnums.nextElement();
            String name = entry.getName();
            if (!entry.isDirectory() && name.startsWith(toRemove)) {
                String shortenedName = name.replace(toRemove, "");
                Path aFile = dest.resolve(shortenedName);
                LOG.debug("EXTRACTING {} SHORTENED to {} into {}", name, shortenedName, aFile);
                fsOps.forceMkdir(aFile.getParent());
                try (FileOutputStream out = new FileOutputStream(aFile.toFile());
                        InputStream in = jarFile.getInputStream(entry)) {
                    IOUtils.copy(in, out);
                }//from w  ww  . jav a2s . c  om
            }
        }
    }
}

From source file:org.pentaho.pac.server.util.ResolverUtil.java

public void loadImplementationsInJar(String parent, URL jarfile, Test... tests) {
    try {//from  ww w. j  a  v  a2  s. c  o m
        JarEntry entry;
        JarInputStream jarStream = new JarInputStream(jarfile.openStream());
        while ((entry = jarStream.getNextJarEntry()) != null) {
            String name = entry.getName();
            if (!entry.isDirectory() && name.startsWith(parent) && name.endsWith(".class")) { //$NON-NLS-1$
                addIfMatching(name, tests);
            }
        }
    } catch (IOException ioe) {
        log.debug("Could not search jar file \\\'" + jarfile //$NON-NLS-1$
                + "\\\' for classes matching criteria: " + tests //$NON-NLS-1$
                + " due to an IOException", ioe); //$NON-NLS-1$
    }
}

From source file:com.sachviet.bookman.server.util.ResolverUtil.java

public void loadImplementationsInJar(String parent, URL jarfile, Test... tests) {
    JarInputStream jarStream = null;
    try {//from w  ww  .  jav a  2s.co m
        JarEntry entry;
        jarStream = new JarInputStream(jarfile.openStream());
        while ((entry = jarStream.getNextJarEntry()) != null) {
            String name = entry.getName();
            if (!entry.isDirectory() && name.startsWith(parent) && name.endsWith(".class")) { //$NON-NLS-1$
                addIfMatching(name, tests);
            }
        }
    } catch (IOException ioe) {
        LOG.trace("Could not search jar file \\\'" + jarfile //$NON-NLS-1$
                + "\\\' for classes matching criteria: " + tests //$NON-NLS-1$
                + " due to an IOException", ioe); //$NON-NLS-1$
    } finally {
        if (jarStream != null)
            try {
                jarStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
    }
}