Search in sources :

Example 56 with JarEntry

use of java.util.jar.JarEntry in project robovm by robovm.

the class OldJarFileTest method test_getInputStreamLjava_util_jar_JarEntry.

public void test_getInputStreamLjava_util_jar_JarEntry() throws IOException {
    Support_Resources.copyFile(resources, null, jarName);
    File localFile = new File(resources, jarName);
    byte[] b = new byte[1024];
    JarFile jf = new JarFile(localFile);
    InputStream is = jf.getInputStream(jf.getEntry(entryName));
    assertTrue("Returned invalid stream", is.available() > 0);
    int r = is.read(b, 0, 1024);
    is.close();
    StringBuilder stringBuffer = new StringBuilder(r);
    for (int i = 0; i < r; i++) {
        stringBuffer.append((char) (b[i] & 0xff));
    }
    String contents = stringBuffer.toString();
    assertTrue("Incorrect stream read", contents.indexOf("bar") > 0);
    jf.close();
    jf = new JarFile(localFile);
    InputStream in = jf.getInputStream(new JarEntry("invalid"));
    assertNull("Got stream for non-existent entry", in);
    try {
        Support_Resources.copyFile(resources, null, jarName);
        File signedFile = new File(resources, jarName);
        jf = new JarFile(signedFile);
        JarEntry jre = new JarEntry("foo/bar/A.class");
        jf.getInputStream(jre);
    // InputStream returned in any way, exception can be thrown in case
    // of reading from this stream only.
    // fail("Should throw ZipException");
    } catch (ZipException expected) {
    }
    try {
        Support_Resources.copyFile(resources, null, jarName);
        File signedFile = new File(resources, jarName);
        jf = new JarFile(signedFile);
        JarEntry jre = new JarEntry("foo/bar/A.class");
        jf.close();
        jf.getInputStream(jre);
        // InputStream returned in any way, exception can be thrown in case
        // of reading from this stream only.
        // The same for IOException
        fail("Should throw IllegalStateException");
    } catch (IllegalStateException expected) {
    }
}
Also used : InputStream(java.io.InputStream) ZipException(java.util.zip.ZipException) JarFile(java.util.jar.JarFile) JarEntry(java.util.jar.JarEntry) JarFile(java.util.jar.JarFile) ZipFile(java.util.zip.ZipFile) File(java.io.File)

Example 57 with JarEntry

use of java.util.jar.JarEntry in project jetty.project by eclipse.

the class JarScanner method matched.

public void matched(URI uri) throws Exception {
    LOG.debug("Search of {}", uri);
    if (uri.toString().toLowerCase(Locale.ENGLISH).endsWith(".jar")) {
        InputStream in = Resource.newResource(uri).getInputStream();
        if (in == null)
            return;
        JarInputStream jar_in = new JarInputStream(in);
        try {
            JarEntry entry = jar_in.getNextJarEntry();
            while (entry != null) {
                processEntry(uri, entry);
                entry = jar_in.getNextJarEntry();
            }
        } finally {
            jar_in.close();
        }
    }
}
Also used : JarInputStream(java.util.jar.JarInputStream) JarInputStream(java.util.jar.JarInputStream) InputStream(java.io.InputStream) JarEntry(java.util.jar.JarEntry)

Example 58 with JarEntry

use of java.util.jar.JarEntry in project jetty.project by eclipse.

the class MetaInfConfiguration method getTlds.

/**
     * Find all .tld files in the given jar.
     * 
     * @param uri the uri to jar file
     * @return the collection of tlds as url references  
     * @throws IOException if unable to scan the jar file
     */
public Collection<URL> getTlds(URI uri) throws IOException {
    HashSet<URL> tlds = new HashSet<URL>();
    String jarUri = uriJarPrefix(uri, "!/");
    URL url = new URL(jarUri);
    JarURLConnection jarConn = (JarURLConnection) url.openConnection();
    jarConn.setUseCaches(Resource.getDefaultUseCaches());
    JarFile jarFile = jarConn.getJarFile();
    Enumeration<JarEntry> entries = jarFile.entries();
    while (entries.hasMoreElements()) {
        JarEntry e = entries.nextElement();
        String name = e.getName();
        if (name.startsWith("META-INF") && name.endsWith(".tld")) {
            tlds.add(new URL(jarUri + name));
        }
    }
    if (!Resource.getDefaultUseCaches())
        jarFile.close();
    return tlds;
}
Also used : JarURLConnection(java.net.JarURLConnection) JarFile(java.util.jar.JarFile) JarEntry(java.util.jar.JarEntry) URL(java.net.URL) HashSet(java.util.HashSet)

Example 59 with JarEntry

use of java.util.jar.JarEntry in project buck by facebook.

the class ClassNodeListSupplierTest method testOneJar.

@Test
public void testOneJar() throws IOException {
    File jar = new File(tmpDir.getRoot(), "primary.jar");
    ZipOutputStream jarOut = new JarOutputStream(new FileOutputStream(jar));
    jarOut.putNextEntry(new JarEntry("com/facebook/buck/android/ClassNodeListSupplierTest.class"));
    writeClassBytes(ClassNodeListSupplierTest.class, jarOut);
    jarOut.close();
    Supplier<ImmutableList<ClassNode>> supplier = ClassNodeListSupplier.createMemoized(ImmutableList.of(jar.toPath()));
    ImmutableList<ClassNode> classNodes = supplier.get();
    assertEquals(1, classNodes.size());
    assertEquals(Type.getType(ClassNodeListSupplierTest.class).getInternalName(), classNodes.get(0).name);
    // Memoized should always return the same object
    assertSame(classNodes, supplier.get());
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode) ZipOutputStream(java.util.zip.ZipOutputStream) ImmutableList(com.google.common.collect.ImmutableList) FileOutputStream(java.io.FileOutputStream) JarOutputStream(java.util.jar.JarOutputStream) JarEntry(java.util.jar.JarEntry) File(java.io.File) Test(org.junit.Test)

Example 60 with JarEntry

use of java.util.jar.JarEntry in project jetty.project by eclipse.

the class SelectiveJarResource method copyTo.

/** 
     * @see org.eclipse.jetty.util.resource.JarResource#copyTo(java.io.File)
     */
@Override
public void copyTo(File directory) throws IOException {
    if (_includes == null)
        _includes = DEFAULT_INCLUDES;
    if (_excludes == null)
        _excludes = DEFAULT_EXCLUDES;
    //parts of the jar file are copied
    if (!exists())
        return;
    String urlString = this.getURL().toExternalForm().trim();
    int endOfJarUrl = urlString.indexOf("!/");
    int startOfJarUrl = (endOfJarUrl >= 0 ? 4 : 0);
    if (endOfJarUrl < 0)
        throw new IOException("Not a valid jar url: " + urlString);
    URL jarFileURL = new URL(urlString.substring(startOfJarUrl, endOfJarUrl));
    try (InputStream is = jarFileURL.openConnection().getInputStream();
        JarInputStream jin = new JarInputStream(is)) {
        JarEntry entry;
        while ((entry = jin.getNextJarEntry()) != null) {
            String entryName = entry.getName();
            LOG.debug("Looking at " + entryName);
            String dotCheck = entryName.replace('\\', '/');
            dotCheck = URIUtil.canonicalPath(dotCheck);
            if (dotCheck == null) {
                LOG.info("Invalid entry: " + entryName);
                continue;
            }
            File file = new File(directory, entryName);
            if (entry.isDirectory()) {
                if (isIncluded(entryName)) {
                    if (!isExcluded(entryName)) {
                        // Make directory
                        if (!file.exists())
                            file.mkdirs();
                    } else
                        LOG.debug("{} dir is excluded", entryName);
                } else
                    LOG.debug("{} dir is NOT included", entryName);
            } else {
                //entry is a file, is it included?
                if (isIncluded(entryName)) {
                    if (!isExcluded(entryName)) {
                        // make directory (some jars don't list dirs)
                        File dir = new File(file.getParent());
                        if (!dir.exists())
                            dir.mkdirs();
                        // Make file
                        try (OutputStream fout = new FileOutputStream(file)) {
                            IO.copy(jin, fout);
                        }
                        // touch the file.
                        if (entry.getTime() >= 0)
                            file.setLastModified(entry.getTime());
                    } else
                        LOG.debug("{} file is excluded", entryName);
                } else
                    LOG.debug("{} file is NOT included", entryName);
            }
        }
        Manifest manifest = jin.getManifest();
        if (manifest != null) {
            if (isIncluded("META-INF") && !isExcluded("META-INF")) {
                File metaInf = new File(directory, "META-INF");
                metaInf.mkdir();
                File f = new File(metaInf, "MANIFEST.MF");
                try (OutputStream fout = new FileOutputStream(f)) {
                    manifest.write(fout);
                }
            }
        }
    }
}
Also used : JarInputStream(java.util.jar.JarInputStream) JarInputStream(java.util.jar.JarInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) JarEntry(java.util.jar.JarEntry) Manifest(java.util.jar.Manifest) File(java.io.File) URL(java.net.URL)

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