Example usage for java.util.jar JarEntry getName

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

Introduction

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

Prototype

public String getName() 

Source Link

Document

Returns the name of the entry.

Usage

From source file:org.jahia.utils.PomUtils.java

public static File extractPomFromJar(JarFile jar, String groupId, String artifactId) throws IOException {
    // deploy artifacts to Maven distribution server
    Enumeration<JarEntry> jarEntries = jar.entries();
    JarEntry jarEntry = null;
    boolean found = false;
    while (jarEntries.hasMoreElements()) {
        jarEntry = jarEntries.nextElement();
        String name = jarEntry.getName();
        if (StringUtils.startsWith(name,
                groupId != null ? ("META-INF/maven/" + groupId + "/") : "META-INF/maven/")
                && StringUtils.endsWith(name, artifactId + "/pom.xml")) {
            found = true;/*w  ww. java2 s  .  c  o  m*/
            break;
        }
    }
    if (!found) {
        throw new IOException("unable to find pom.xml file within while looking for " + artifactId);
    }
    InputStream is = jar.getInputStream(jarEntry);
    File pomFile = File.createTempFile("pom", ".xml");
    FileUtils.copyInputStreamToFile(is, pomFile);
    return pomFile;
}

From source file:org.ebayopensource.turmeric.eclipse.utils.io.IOUtil.java

/**
 * In the normal jar URLs usage in Windows Java puts a lock on it. And the
 * SOA Tool Handler will create a non locking URL by setting the caching
 * off. There is obviously a performance compromise made here by disabling
 * the cache.//from  w w  w.j  a v a 2s  . co  m
 *
 * @param jarFileUrl the jar file url
 * @param jarEntryPath the jar entry path
 * @return the non locking url
 * @throws IOException Signals that an I/O exception has occurred.
 */
public static URL getNonLockingURL(URL jarFileUrl, String jarEntryPath) throws IOException {
    File file = FileUtils.toFile(jarFileUrl);
    JarFile jarFile;
    jarFile = new JarFile(file);
    JarEntry jarEntry = jarFile.getJarEntry(jarEntryPath);
    if (jarEntry != null) {
        SOAToolFileUrlHandler handler = new SOAToolFileUrlHandler(jarFile, jarEntry);
        URL retUrl = new URL("jar", "", -1,
                new File(jarFile.getName()).toURI().toURL() + "!/" + jarEntry.getName(), handler);
        handler.setExpectedUrl(retUrl);
        return retUrl;

    }
    return null;
}

From source file:org.diffkit.common.DKUnjar.java

/**
 * N.B. this method is much less efficient than the similar signature without
 * substitutions, so use that if you don't really need substitutions <br>
 * /*from  w  w  w  . ja v  a2  s  . c o m*/
 * closes inputStream_ at the end
 */
public static void unjar(JarInputStream inputStream_, File outputDir_, Map<String, String> substitutions_)
        throws IOException {
    boolean isDebugEnabled = LOG.isDebugEnabled();
    if (isDebugEnabled)
        LOG.debug("substitutions_->{}", substitutions_);

    if (MapUtils.isEmpty(substitutions_))
        unjar(inputStream_, outputDir_);

    DKValidate.notNull(inputStream_, outputDir_);
    if (!outputDir_.isDirectory())
        throw new RuntimeException(String.format("directory does not exist->%s", outputDir_));

    JarEntry entry = null;
    while ((entry = inputStream_.getNextJarEntry()) != null) {
        String contents = IOUtils.toString(inputStream_);
        if (isDebugEnabled)
            LOG.debug("contents->{}", contents);
        contents = DKStringUtil.replaceEach(contents, substitutions_);
        if (isDebugEnabled)
            LOG.debug("substituted contents->{}", contents);
        File outFile = new File(outputDir_, entry.getName());
        FileUtils.writeStringToFile(outFile, contents);
    }
    inputStream_.close();
}

From source file:org.orbisgis.core.plugin.BundleTools.java

private static void parseJar(File jarFilePath, Set<String> packages) throws IOException {
    JarFile jar = new JarFile(jarFilePath);
    Enumeration<? extends JarEntry> entryEnum = jar.entries();
    while (entryEnum.hasMoreElements()) {
        JarEntry entry = entryEnum.nextElement();
        if (!entry.isDirectory()) {
            final String path = entry.getName();
            if (path.endsWith(".class")) {
                // Extract folder
                String parentPath = (new File(path)).getParent();
                if (parentPath != null) {
                    packages.add(parentPath.replace(File.separator, "."));
                }/*from  ww w .  j a va 2  s .c  o m*/
            }
        }
    }
}

From source file:com.photon.phresco.service.util.ServerUtil.java

public static InputStream getArtifactPomStream(File artifactFile) throws PhrescoException {
    String pomFile = null;//  w  w  w  .j  a  v  a  2 s .c  om
    if (getFileExtension(artifactFile.getName()).equals(ServerConstants.JAR_FILE)) {
        try {
            JarFile jarfile = new JarFile(artifactFile);
            for (Enumeration<JarEntry> em = jarfile.entries(); em.hasMoreElements();) {
                JarEntry jarEntry = em.nextElement();
                if (jarEntry.getName().endsWith("pom.xml")) {
                    pomFile = jarEntry.getName();
                }
            }
            if (pomFile != null) {
                ZipEntry entry = jarfile.getEntry(pomFile);
                return jarfile.getInputStream(entry);
            }
        } catch (Exception e) {
            throw new PhrescoException(e);
        }
    }
    return null;
}

From source file:org.apache.pluto.util.assemble.io.JarStreamingAssembly.java

/**
 * Reads the source JarInputStream, copying entries to the destination JarOutputStream. 
 * The web.xml and portlet.xml are cached, and after the entire archive is copied 
 * (minus the web.xml) a re-written web.xml is generated and written to the 
 * destination JAR.// w  w w .  j  a va  2  s.co  m
 * 
 * @param source the WAR source input stream
 * @param dest the WAR destination output stream
 * @param dispatchServletClass the name of the dispatch class
 * @throws IOException
 */
public static void assembleStream(JarInputStream source, JarOutputStream dest, String dispatchServletClass)
        throws IOException {

    try {
        //Need to buffer the web.xml and portlet.xml files for the rewritting
        JarEntry servletXmlEntry = null;
        byte[] servletXmlBuffer = null;
        byte[] portletXmlBuffer = null;

        JarEntry originalJarEntry;

        //Read the source archive entry by entry
        while ((originalJarEntry = source.getNextJarEntry()) != null) {

            final JarEntry newJarEntry = smartClone(originalJarEntry);
            originalJarEntry = null;

            //Capture the web.xml JarEntry and contents as a byte[], don't write it out now or
            //update the CRC or length of the destEntry.
            if (Assembler.SERVLET_XML.equals(newJarEntry.getName())) {
                servletXmlEntry = newJarEntry;
                servletXmlBuffer = IOUtils.toByteArray(source);
            }

            //Capture the portlet.xml contents as a byte[]
            else if (Assembler.PORTLET_XML.equals(newJarEntry.getName())) {
                portletXmlBuffer = IOUtils.toByteArray(source);
                dest.putNextEntry(newJarEntry);
                IOUtils.write(portletXmlBuffer, dest);
            }

            //Copy all other entries directly to the output archive
            else {
                dest.putNextEntry(newJarEntry);
                IOUtils.copy(source, dest);
            }

            dest.closeEntry();
            dest.flush();

        }

        // If no portlet.xml was found in the archive, skip the assembly step.
        if (portletXmlBuffer != null) {
            // container for assembled web.xml bytes
            final byte[] webXmlBytes;

            // Checks to make sure the web.xml was found in the archive
            if (servletXmlBuffer == null) {
                throw new FileNotFoundException(
                        "File '" + Assembler.SERVLET_XML + "' could not be found in the source input stream.");
            }

            //Create streams of the byte[] data for the updater method
            final InputStream webXmlIn = new ByteArrayInputStream(servletXmlBuffer);
            final InputStream portletXmlIn = new ByteArrayInputStream(portletXmlBuffer);
            final ByteArrayOutputStream webXmlOut = new ByteArrayOutputStream(servletXmlBuffer.length);

            //Update the web.xml
            WebXmlStreamingAssembly.assembleStream(webXmlIn, portletXmlIn, webXmlOut, dispatchServletClass);
            IOUtils.copy(webXmlIn, webXmlOut);
            webXmlBytes = webXmlOut.toByteArray();

            //If no compression is being used (STORED) we have to manually update the size and crc
            if (servletXmlEntry.getMethod() == ZipEntry.STORED) {
                servletXmlEntry.setSize(webXmlBytes.length);
                final CRC32 webXmlCrc = new CRC32();
                webXmlCrc.update(webXmlBytes);
                servletXmlEntry.setCrc(webXmlCrc.getValue());
            }

            //write out the assembled web.xml entry and contents
            dest.putNextEntry(servletXmlEntry);
            IOUtils.write(webXmlBytes, dest);

            if (LOG.isDebugEnabled()) {
                LOG.debug("Jar stream " + source + " successfully assembled.");
            }
        } else {
            if (LOG.isDebugEnabled()) {
                LOG.debug("No portlet XML file was found, assembly was not required.");
            }

            //copy the original, unmodified web.xml entry to the destination
            dest.putNextEntry(servletXmlEntry);
            IOUtils.write(servletXmlBuffer, dest);

            if (LOG.isDebugEnabled()) {
                LOG.debug("Jar stream " + source + " successfully assembled.");
            }
        }

    } finally {

        dest.flush();
        dest.close();

    }
}

From source file:com.jrummyapps.busybox.signing.ZipSigner.java

/** Add the SHA1 of every file to the manifest, creating it if necessary. */
private static Manifest addDigestsToManifest(final JarFile jar) throws IOException, GeneralSecurityException {
    final Manifest input = jar.getManifest();
    final Manifest output = new Manifest();

    final Attributes main = output.getMainAttributes();
    main.putValue("Manifest-Version", MANIFEST_VERSION);
    main.putValue("Created-By", CREATED_BY);

    // We sort the input entries by name, and add them to the output manifest in sorted order.
    // We expect that the output map will be deterministic.
    final TreeMap<String, JarEntry> byName = new TreeMap<>();
    for (Enumeration<JarEntry> e = jar.entries(); e.hasMoreElements();) {
        JarEntry entry = e.nextElement();
        byName.put(entry.getName(), entry);
    }/* w w  w  .ja  v a  2 s . co m*/

    final MessageDigest md = MessageDigest.getInstance("SHA1");
    final byte[] buffer = new byte[4096];
    int num;

    for (JarEntry entry : byName.values()) {
        final String name = entry.getName();
        if (!entry.isDirectory() && !name.equals(JarFile.MANIFEST_NAME) && !name.equals(CERT_SF_NAME)
                && !name.equals(CERT_RSA_NAME)) {
            InputStream data = jar.getInputStream(entry);
            while ((num = data.read(buffer)) > 0) {
                md.update(buffer, 0, num);
            }

            Attributes attr = null;
            if (input != null) {
                attr = input.getAttributes(name);
            }
            attr = attr != null ? new Attributes(attr) : new Attributes();
            attr.putValue("SHA1-Digest", base64encode(md.digest()));
            output.getEntries().put(name, attr);
        }
    }

    return output;
}

From source file:io.joynr.util.JoynrUtil.java

public static void copyDirectoryFromJar(String jarName, String srcDir, File tmpDir) throws IOException {

    JarFile jf = null;//from w  w w  .j  a v a  2  s  .  co m
    JarInputStream jarInputStream = null;

    try {
        jf = new JarFile(jarName);
        JarEntry je = jf.getJarEntry(srcDir);
        if (je.isDirectory()) {
            FileInputStream fis = new FileInputStream(jarName);
            BufferedInputStream bis = new BufferedInputStream(fis);
            jarInputStream = new JarInputStream(bis);
            JarEntry ze = null;
            while ((ze = jarInputStream.getNextJarEntry()) != null) {
                if (ze.isDirectory()) {
                    continue;
                }
                if (ze.getName().contains(je.getName())) {
                    InputStream is = jf.getInputStream(ze);
                    String name = ze.getName().substring(ze.getName().lastIndexOf("/") + 1);
                    File tmpFile = new File(tmpDir + "/" + name); //File.createTempFile(file.getName(), "tmp");
                    tmpFile.deleteOnExit();
                    OutputStream outputStreamRuntime = new FileOutputStream(tmpFile);
                    copyStream(is, outputStreamRuntime);
                }
            }
        }
    } finally {
        if (jf != null) {
            jf.close();
        }
        if (jarInputStream != null) {
            jarInputStream.close();
        }
    }
}

From source file:org.infoglue.deliver.portal.deploy.Deploy.java

private static int expandArchive(File warFile, File destDir) throws IOException {
    int numEntries = 0;

    if (!destDir.exists()) {
        destDir.mkdirs();/*from www .j a v  a2 s .c  om*/
    }
    JarFile jarFile = new JarFile(warFile);
    Enumeration files = jarFile.entries();
    while (files.hasMoreElements()) {
        JarEntry entry = (JarEntry) files.nextElement();
        String fileName = entry.getName();

        File file = new File(destDir, fileName);
        File dirF = new File(file.getParent());
        dirF.mkdirs();

        if (entry.isDirectory()) {
            file.mkdirs();
        } else {
            InputStream fis = jarFile.getInputStream(entry);
            FileOutputStream fos = new FileOutputStream(file);
            copyStream(fis, fos);
            fos.close();
        }
        numEntries++;
    }
    return numEntries;
}

From source file:org.bimserver.plugins.VirtualFile.java

public static VirtualFile fromJar(InputStream inputStream) throws IOException {
    VirtualFile result = new VirtualFile();
    JarInputStream jarInputStream = new JarInputStream(inputStream);
    JarEntry jarEntry = jarInputStream.getNextJarEntry();
    while (jarEntry != null) {
        String n = jarEntry.getName();
        n = n.replace("/", File.separator);
        n = n.replace("\\", File.separator);
        VirtualFile newFile = result.createFile(n);
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        IOUtils.copy(jarInputStream, byteArrayOutputStream);
        newFile.setData(byteArrayOutputStream.toByteArray());
        jarEntry = jarInputStream.getNextJarEntry();
    }//from  ww w.  j  ava 2s .c om
    return result;
}