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:com.photon.phresco.service.util.ServerUtil.java

/**
 * Validate the given jar is valid maven archetype jar
 * /* w w  w.  j a  v  a 2  s .co m*/
 * @return
 * @throws PhrescoException
 */
public static boolean validateArchetypeJar(InputStream inputJar) throws PhrescoException {
    try {
        jarInputStream = new JarInputStream(inputJar);
        JarEntry nextJarEntry = jarInputStream.getNextJarEntry();
        while ((nextJarEntry = jarInputStream.getNextJarEntry()) != null) {
            if (nextJarEntry.getName().equals(ServerConstants.ARCHETYPE_METADATA_FILE)
                    || nextJarEntry.getName().equals(ServerConstants.ARCHETYPE_FILE)) {
                return true;
            }
        }
    } catch (Exception e) {
        throw new PhrescoException(e);
    }
    return false;
}

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

/**
 * Validate the given jar is valid maven plugin jar
 * //from  w  w  w. j  a  v a2s  . c o  m
 * @return
 * @throws PhrescoException
 */
public static boolean validatePluginJar(InputStream inputJar) throws PhrescoException {
    try {
        jarInputStream = new JarInputStream(inputJar);
        JarEntry nextJarEntry = jarInputStream.getNextJarEntry();
        while ((nextJarEntry = jarInputStream.getNextJarEntry()) != null) {
            if (nextJarEntry.getName().equals(ServerConstants.PLUGIN_COMPONENTS_XML_FILE)
                    || nextJarEntry.getName().equals(ServerConstants.PLUGIN_XML_FILE)) {
                return true;
            }
        }
    } catch (Exception e) {
        throw new PhrescoException(e);
    }
    return false;
}

From source file:com.taobao.android.builder.tools.classinject.CodeInjectByJavassist.java

/**
 * jar?// w  ww  . ja va 2 s.c o m
 *
 * @param inJar
 * @param outJar
 * @throws IOException
 * @throws NotFoundException
 * @throws CannotCompileException
 */
public static List<String> inject(ClassPool pool, File inJar, File outJar, InjectParam injectParam)
        throws Exception {
    List<String> errorFiles = new ArrayList<String>();
    JarFile jarFile = newJarFile(inJar);

    outJar.getParentFile().mkdirs();
    FileOutputStream fileOutputStream = new FileOutputStream(outJar);
    JarOutputStream jos = new JarOutputStream(new BufferedOutputStream(fileOutputStream));
    Enumeration<JarEntry> jarFileEntries = jarFile.entries();
    JarEntry jarEntry = null;
    while (jarFileEntries.hasMoreElements()) {
        jarEntry = jarFileEntries.nextElement();
        String name = jarEntry.getName();
        String className = StringUtils.replace(name, File.separator, "/");
        className = StringUtils.replace(className, "/", ".");
        className = StringUtils.substring(className, 0, className.length() - 6);
        if (!StringUtils.endsWithIgnoreCase(name, ".class")) {
            InputStream inputStream = jarFile.getInputStream(jarEntry);
            copyStreamToJar(inputStream, jos, name, jarEntry.getTime(), jarEntry);
            IOUtils.closeQuietly(inputStream);
        } else {
            byte[] codes;

            codes = inject(pool, className, injectParam);
            InputStream classInstream = new ByteArrayInputStream(codes);
            copyStreamToJar(classInstream, jos, name, jarEntry.getTime(), jarEntry);

        }
    }
    jarFile.close();
    IOUtils.closeQuietly(jos);
    return errorFiles;
}

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

/**
 * Validate the given jar is valid maven jar
 * //from w w w.j  av a  2 s .com
 * @return
 * @throws PhrescoException
 */
public static boolean validateMavenJar(InputStream inputJar) throws PhrescoException {
    boolean returnValue = false;
    try {
        jarInputStream = new JarInputStream(inputJar);
        JarEntry nextJarEntry = jarInputStream.getNextJarEntry();
        while ((nextJarEntry = jarInputStream.getNextJarEntry()) != null) {
            if (nextJarEntry.getName().contains("pom.xml")) {
                returnValue = true;
                break;
            }
        }
    } catch (Exception e) {
        throw new PhrescoException(e);
    }

    return returnValue;
}

From source file:edu.uci.ics.asterix.event.service.AsterixEventServiceUtil.java

private static void replaceInJar(File sourceJar, String origFile, File replacementFile) throws IOException {
    String srcJarAbsPath = sourceJar.getAbsolutePath();
    String srcJarSuffix = srcJarAbsPath.substring(srcJarAbsPath.lastIndexOf(File.separator) + 1);
    String srcJarName = srcJarSuffix.split(".jar")[0];

    String destJarName = srcJarName + "-managix";
    String destJarSuffix = destJarName + ".jar";
    File destJar = new File(sourceJar.getParentFile().getAbsolutePath() + File.separator + destJarSuffix);
    //  File destJar = new File(sourceJar.getAbsolutePath() + ".modified");
    JarFile sourceJarFile = new JarFile(sourceJar);
    Enumeration<JarEntry> entries = sourceJarFile.entries();
    JarOutputStream jos = new JarOutputStream(new FileOutputStream(destJar));
    byte[] buffer = new byte[2048];
    int read;//from   w w w .  j  a v a2 s .  c  om
    while (entries.hasMoreElements()) {
        JarEntry entry = (JarEntry) entries.nextElement();
        String name = entry.getName();
        if (name.equals(origFile)) {
            continue;
        }
        InputStream jarIs = sourceJarFile.getInputStream(entry);
        jos.putNextEntry(entry);
        while ((read = jarIs.read(buffer)) != -1) {
            jos.write(buffer, 0, read);
        }
        jarIs.close();
    }
    sourceJarFile.close();
    JarEntry entry = new JarEntry(origFile);
    jos.putNextEntry(entry);
    FileInputStream fis = new FileInputStream(replacementFile);
    while ((read = fis.read(buffer)) != -1) {
        jos.write(buffer, 0, read);
    }
    fis.close();
    jos.close();
    sourceJar.delete();
    destJar.renameTo(sourceJar);
    destJar.setExecutable(true);
}

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 w w. ja v a  2 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:com.ms.commons.test.classloader.IntlTestURLClassPath.java

private static void expandJarTo(JarFile jarFile, String outputPath) throws IOException {
    List<JarEntry> entries = CollectionUtil.toCollection(ArrayList.class, jarFile.entries());
    for (JarEntry entry : entries) {
        if (entry.isDirectory()) {
            // ignore directory
        } else {//  w  w w.  ja va 2  s .  c  o m
            // 
            File efile = new File(outputPath, entry.getName());
            efile.getParentFile().mkdirs();
            InputStream in = new BufferedInputStream(jarFile.getInputStream(entry));
            OutputStream out = new BufferedOutputStream(new FileOutputStream(efile));
            IOUtils.copy(in, out);
            IOUtils.closeQuietly(out);
            IOUtils.closeQuietly(in);
        }
    }
}

From source file:org.apache.sling.maven.slingstart.PreparePackageMojoTest.java

private static void compareJarContents(File orgJar, File actualJar) throws IOException {
    try (JarInputStream jis1 = new JarInputStream(new FileInputStream(orgJar));
            JarInputStream jis2 = new JarInputStream(new FileInputStream(actualJar))) {
        JarEntry je1 = null;
        while ((je1 = jis1.getNextJarEntry()) != null) {
            if (je1.isDirectory())
                continue;

            JarEntry je2 = null;/*from  www.j  ava 2s  .c om*/
            while ((je2 = jis2.getNextJarEntry()) != null) {
                if (!je2.isDirectory())
                    break;
            }

            assertEquals(je1.getName(), je2.getName());
            assertEquals(je1.getSize(), je2.getSize());

            try {
                byte[] buf1 = IOUtils.toByteArray(jis1);
                byte[] buf2 = IOUtils.toByteArray(jis2);

                assertArrayEquals("Contents not equal: " + je1.getName(), buf1, buf2);
            } finally {
                jis1.closeEntry();
                jis2.closeEntry();
            }
        }
    }
}

From source file:org.primeframework.mvc.util.ClassClasspathResolver.java

/**
 * Attempts to load the JarEntry into a ClassReader.
 *
 * @param jar      The JAR file that the entry is in.
 * @param jarFile  The JAR file used to get the InputStream to the entry.
 * @param jarEntry The JAR entry to load.
 * @return The ClassReader and never null.
 * @throws IOException If the JarEntry doesn't point to a valid class.
 */// w  w  w .  j  a v  a2 s. co  m
public static ClassReader load(File jar, JarFile jarFile, JarEntry jarEntry) throws IOException {
    try {
        return new ClassReader(jarFile.getInputStream(jarEntry));
    } catch (IOException e) {
        throw new IOException(
                "Error parsing class file at [" + jar.getAbsolutePath() + "!/" + jarEntry.getName() + "]", e);
    }
}

From source file:org.omegat.util.StaticUtils.java

public static void extractFileFromJar(InputStream in, String destination, String... filenames)
        throws IOException {
    if (filenames == null || filenames.length == 0) {
        throw new IllegalArgumentException("Caller must provide non-empty list of files to extract.");
    }//ww w .  j av  a2s  .  c  o m
    List<String> toExtract = new ArrayList<>(Arrays.asList(filenames));
    try (JarInputStream jis = new JarInputStream(in)) {
        // parse the entries
        JarEntry entry;
        while ((entry = jis.getNextJarEntry()) != null) {
            if (!toExtract.contains(entry.getName())) {
                continue;
            }
            // match found
            File f = new File(destination, entry.getName());
            f.getParentFile().mkdirs();

            try (FileOutputStream fos = new FileOutputStream(f);
                    BufferedOutputStream out = new BufferedOutputStream(fos)) {
                byte[] byteBuffer = new byte[1024];
                int numRead;
                while ((numRead = jis.read(byteBuffer)) != -1) {
                    out.write(byteBuffer, 0, numRead);
                }
            }
            toExtract.remove(entry.getName());
        }
    }
    if (!toExtract.isEmpty()) {
        throw new FileNotFoundException("Failed to extract all of the specified files.");
    }
}