Search in sources :

Example 46 with JarEntry

use of java.util.jar.JarEntry in project spring-boot by spring-projects.

the class PropertiesMergingResourceTransformer method modifyOutputStream.

@Override
public void modifyOutputStream(JarOutputStream os) throws IOException {
    os.putNextEntry(new JarEntry(this.resource));
    this.data.store(os, "Merged by PropertiesMergingResourceTransformer");
    os.flush();
    this.data.clear();
}
Also used : JarEntry(java.util.jar.JarEntry)

Example 47 with JarEntry

use of java.util.jar.JarEntry in project spring-boot by spring-projects.

the class JarFileTests method verifySignedJar.

@Test
public void verifySignedJar() throws Exception {
    String classpath = System.getProperty("java.class.path");
    String[] entries = classpath.split(System.getProperty("path.separator"));
    String signedJarFile = null;
    for (String entry : entries) {
        if (entry.contains("bcprov")) {
            signedJarFile = entry;
        }
    }
    assertThat(signedJarFile).isNotNull();
    java.util.jar.JarFile jarFile = new JarFile(new File(signedJarFile));
    jarFile.getManifest();
    Enumeration<JarEntry> jarEntries = jarFile.entries();
    while (jarEntries.hasMoreElements()) {
        JarEntry jarEntry = jarEntries.nextElement();
        InputStream inputStream = jarFile.getInputStream(jarEntry);
        inputStream.skip(Long.MAX_VALUE);
        inputStream.close();
        if (!jarEntry.getName().startsWith("META-INF") && !jarEntry.isDirectory() && !jarEntry.getName().endsWith("TigerDigest.class")) {
            assertThat(jarEntry.getCertificates()).isNotNull();
        }
    }
    jarFile.close();
}
Also used : JarInputStream(java.util.jar.JarInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) JarEntry(java.util.jar.JarEntry) RandomAccessDataFile(org.springframework.boot.loader.data.RandomAccessDataFile) File(java.io.File) Test(org.junit.Test)

Example 48 with JarEntry

use of java.util.jar.JarEntry in project camel by apache.

the class FatJarPackageScanClassResolver method doLoadJarClassEntries.

protected List<String> doLoadJarClassEntries(InputStream stream, String urlPath, boolean inspectNestedJars, boolean closeStream) {
    List<String> entries = new ArrayList<String>();
    JarInputStream jarStream = null;
    try {
        jarStream = new JarInputStream(stream);
        JarEntry entry;
        while ((entry = jarStream.getNextJarEntry()) != null) {
            String name = entry.getName();
            if (name != null) {
                name = name.trim();
                if (!entry.isDirectory() && name.endsWith(".class")) {
                    entries.add(cleanupSpringbootClassName(name));
                } else if (inspectNestedJars && !entry.isDirectory() && isSpringbootNestedJar(name)) {
                    String nestedUrl = urlPath + "!/" + name;
                    log.trace("Inspecting nested jar: {}", nestedUrl);
                    List<String> nestedEntries = doLoadJarClassEntries(jarStream, nestedUrl, false, false);
                    entries.addAll(nestedEntries);
                }
            }
        }
    } catch (IOException ioe) {
        log.warn("Cannot search jar file '" + urlPath + " due to an IOException: " + ioe.getMessage(), ioe);
    } finally {
        if (closeStream) {
            // stream is left open when scanning nested jars, otherwise the fat jar stream gets closed
            IOHelper.close(jarStream, urlPath, log);
        }
    }
    return entries;
}
Also used : JarInputStream(java.util.jar.JarInputStream) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) IOException(java.io.IOException) JarEntry(java.util.jar.JarEntry)

Example 49 with JarEntry

use of java.util.jar.JarEntry in project flink by apache.

the class JarFileCreator method createJarFile.

/**
	 * Creates a jar file which contains the previously added class. The content of the jar file is written to
	 * <code>outputFile</code> which has been provided to the constructor. If <code>outputFile</code> already exists, it
	 * is overwritten by this operation.
	 * 
	 * @throws IOException
	 *         thrown if an error occurs while writing to the output file
	 */
public synchronized void createJarFile() throws IOException {
    //Retrieve dependencies automatically
    addDependencies();
    // Temporary buffer for the stream copy
    final byte[] buf = new byte[128];
    // Check if output file is valid
    if (this.outputFile == null) {
        throw new IOException("Output file is null");
    }
    // If output file already exists, delete it
    if (this.outputFile.exists()) {
        this.outputFile.delete();
    }
    try (JarOutputStream jos = new JarOutputStream(new FileOutputStream(this.outputFile), new Manifest())) {
        final Iterator<Class<?>> it = this.classSet.iterator();
        while (it.hasNext()) {
            final Class<?> clazz = it.next();
            final String entry = clazz.getName().replace('.', '/') + CLASS_EXTENSION;
            jos.putNextEntry(new JarEntry(entry));
            String name = clazz.getName();
            int n = name.lastIndexOf('.');
            String className = null;
            if (n > -1) {
                className = name.substring(n + 1, name.length());
            }
            //Using the part after last dot instead of class.getSimpleName() could resolve the problem of inner class.
            final InputStream classInputStream = clazz.getResourceAsStream(className + CLASS_EXTENSION);
            int num = classInputStream.read(buf);
            while (num != -1) {
                jos.write(buf, 0, num);
                num = classInputStream.read(buf);
            }
            classInputStream.close();
            jos.closeEntry();
        }
    }
}
Also used : InputStream(java.io.InputStream) FileOutputStream(java.io.FileOutputStream) JarOutputStream(java.util.jar.JarOutputStream) IOException(java.io.IOException) Manifest(java.util.jar.Manifest) JarEntry(java.util.jar.JarEntry)

Example 50 with JarEntry

use of java.util.jar.JarEntry in project camel by apache.

the class DefaultPackageScanClassResolver method doLoadJarClassEntries.

/**
     * Loads all the class entries from the JAR.
     *
     * @param stream  the inputstream of the jar file to be examined for classes
     * @param urlPath the url of the jar file to be examined for classes
     * @return all the .class entries from the JAR
     */
protected List<String> doLoadJarClassEntries(InputStream stream, String urlPath) {
    List<String> entries = new ArrayList<String>();
    JarInputStream jarStream = null;
    try {
        jarStream = new JarInputStream(stream);
        JarEntry entry;
        while ((entry = jarStream.getNextJarEntry()) != null) {
            String name = entry.getName();
            if (name != null) {
                name = name.trim();
                if (!entry.isDirectory() && name.endsWith(".class")) {
                    entries.add(name);
                }
            }
        }
    } catch (IOException ioe) {
        log.warn("Cannot search jar file '" + urlPath + " due to an IOException: " + ioe.getMessage(), ioe);
    } finally {
        IOHelper.close(jarStream, urlPath, log);
    }
    return entries;
}
Also used : JarInputStream(java.util.jar.JarInputStream) ArrayList(java.util.ArrayList) IOException(java.io.IOException) JarEntry(java.util.jar.JarEntry)

Aggregations

JarEntry (java.util.jar.JarEntry)594 JarFile (java.util.jar.JarFile)290 File (java.io.File)217 IOException (java.io.IOException)187 InputStream (java.io.InputStream)134 JarOutputStream (java.util.jar.JarOutputStream)112 FileOutputStream (java.io.FileOutputStream)109 FileInputStream (java.io.FileInputStream)92 URL (java.net.URL)87 JarInputStream (java.util.jar.JarInputStream)87 ArrayList (java.util.ArrayList)67 Manifest (java.util.jar.Manifest)58 JarURLConnection (java.net.JarURLConnection)53 Test (org.junit.Test)39 HashSet (java.util.HashSet)31 ZipEntry (java.util.zip.ZipEntry)31 ZipFile (java.util.zip.ZipFile)30 OutputStream (java.io.OutputStream)29 BufferedInputStream (java.io.BufferedInputStream)26 Enumeration (java.util.Enumeration)26