Search in sources :

Example 1 with JarFile

use of java.util.jar.JarFile 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 2 with JarFile

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

the class AndroidAarIntegrationTest method testBuildAndroidAar.

@Test
public void testBuildAndroidAar() throws IOException {
    ProjectWorkspace workspace = TestDataHelper.createProjectWorkspaceForScenario(this, "android_aar_build/caseA", tmp);
    workspace.setUp();
    String target = "//:app";
    workspace.runBuckBuild(target).assertSuccess();
    Path aar = workspace.getPath(BuildTargets.getGenPath(filesystem, BuildTargetFactory.newInstance(target), AndroidAar.AAR_FORMAT));
    ZipInspector zipInspector = new ZipInspector(aar);
    zipInspector.assertFileExists("AndroidManifest.xml");
    zipInspector.assertFileExists("classes.jar");
    zipInspector.assertFileExists("R.txt");
    zipInspector.assertFileExists("assets/a.txt");
    zipInspector.assertFileExists("assets/b.txt");
    zipInspector.assertFileExists("res/raw/helloworld.txt");
    zipInspector.assertFileExists("res/values/values.xml");
    zipInspector.assertFileContents("res/values/values.xml", workspace.getFileContents("res/values/A.xml").trim());
    Path contents = tmp.getRoot().resolve("aar-contents");
    Unzip.extractZipFile(aar, contents, Unzip.ExistingFileMode.OVERWRITE);
    try (JarFile classes = new JarFile(contents.resolve("classes.jar").toFile())) {
        assertThat(classes.getJarEntry("com/example/HelloWorld.class"), Matchers.notNullValue());
    }
}
Also used : Path(java.nio.file.Path) ProjectWorkspace(com.facebook.buck.testutil.integration.ProjectWorkspace) ZipInspector(com.facebook.buck.testutil.integration.ZipInspector) JarFile(java.util.jar.JarFile) Test(org.junit.Test)

Example 3 with JarFile

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

the class JarFileResource method exists.

/* ------------------------------------------------------------ */
/**
     * Returns true if the represented resource exists.
     */
@Override
public boolean exists() {
    if (_exists)
        return true;
    if (_urlString.endsWith("!/")) {
        String file_url = _urlString.substring(4, _urlString.length() - 2);
        try {
            return newResource(file_url).exists();
        } catch (Exception e) {
            LOG.ignore(e);
            return false;
        }
    }
    boolean check = checkConnection();
    // Is this a root URL?
    if (_jarUrl != null && _path == null) {
        // Then if it exists it is a directory
        _directory = check;
        return true;
    } else {
        // Can we find a file for it?
        boolean close_jar_file = false;
        JarFile jar_file = null;
        if (check)
            // Yes
            jar_file = _jarFile;
        else {
            // No - so lets look if the root entry exists.
            try {
                JarURLConnection c = (JarURLConnection) ((new URL(_jarUrl)).openConnection());
                c.setUseCaches(getUseCaches());
                jar_file = c.getJarFile();
                close_jar_file = !getUseCaches();
            } catch (Exception e) {
                LOG.ignore(e);
            }
        }
        // Do we need to look more closely?
        if (jar_file != null && _entry == null && !_directory) {
            // OK - we have a JarFile, lets look for the entry
            JarEntry entry = jar_file.getJarEntry(_path);
            if (entry == null) {
                // the entry does not exist
                _exists = false;
            } else if (entry.isDirectory()) {
                _directory = true;
                _entry = entry;
            } else {
                // Let's confirm is a file
                JarEntry directory = jar_file.getJarEntry(_path + '/');
                if (directory != null) {
                    _directory = true;
                    _entry = directory;
                } else {
                    // OK is a file
                    _directory = false;
                    _entry = entry;
                }
            }
        }
        if (close_jar_file && jar_file != null) {
            try {
                jar_file.close();
            } catch (IOException ioe) {
                LOG.ignore(ioe);
            }
        }
    }
    _exists = (_directory || _entry != null);
    return _exists;
}
Also used : JarURLConnection(java.net.JarURLConnection) IOException(java.io.IOException) JarFile(java.util.jar.JarFile) JarEntry(java.util.jar.JarEntry) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) URL(java.net.URL)

Example 4 with JarFile

use of java.util.jar.JarFile in project storm by apache.

the class Utils method extractDirFromJarImpl.

public void extractDirFromJarImpl(String jarpath, String dir, File destdir) {
    try (JarFile jarFile = new JarFile(jarpath)) {
        Enumeration<JarEntry> jarEnums = jarFile.entries();
        while (jarEnums.hasMoreElements()) {
            JarEntry entry = jarEnums.nextElement();
            if (!entry.isDirectory() && entry.getName().startsWith(dir)) {
                File aFile = new File(destdir, entry.getName());
                aFile.getParentFile().mkdirs();
                try (FileOutputStream out = new FileOutputStream(aFile);
                    InputStream in = jarFile.getInputStream(entry)) {
                    IOUtils.copy(in, out);
                }
            }
        }
    } catch (IOException e) {
        LOG.info("Could not extract {} from {}", dir, jarpath);
    }
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) BufferedInputStream(java.io.BufferedInputStream) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) ObjectInputStream(java.io.ObjectInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) ClassLoaderObjectInputStream(org.apache.commons.io.input.ClassLoaderObjectInputStream) InputStream(java.io.InputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) JarFile(java.util.jar.JarFile) JarEntry(java.util.jar.JarEntry) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) JarFile(java.util.jar.JarFile) ZipFile(java.util.zip.ZipFile)

Example 5 with JarFile

use of java.util.jar.JarFile in project enumerable by hraberg.

the class LambdaCompiler method compilieJar.

void compilieJar(File jar) throws Exception {
    try {
        JarFile jarFile = new JarFile(jar);
        if (jarFile.getEntry(AOT_COMPILED_MARKER) != null) {
            out.println(jar + " is already compiled, skipping.");
            return;
        }
        File tempDir = unjar(jarFile);
        File aotCompiledMarker = new File(tempDir, AOT_COMPILED_MARKER);
        aotCompiledMarker.getParentFile().mkdir();
        aotCompiledMarker.createNewFile();
        ensureCreated(aotCompiledMarker);
        compileDirectory(tempDir);
        debug("writing generated lambdas in " + jar);
        writeGeneratedLambdas(tempDir);
        File newJar = jar(tempDir);
        File bak = new File(jar.getAbsolutePath() + ".bak");
        rename(jar, bak);
        ensureCreated(bak);
        rename(newJar, jar);
        ensureCreated(jar);
        delete(bak);
        delete(tempDir);
    } catch (Exception e) {
        throw uncheck(e);
    }
}
Also used : JarFile(java.util.jar.JarFile) JarFile(java.util.jar.JarFile)

Aggregations

JarFile (java.util.jar.JarFile)1357 File (java.io.File)720 JarEntry (java.util.jar.JarEntry)615 IOException (java.io.IOException)586 InputStream (java.io.InputStream)270 URL (java.net.URL)263 ZipEntry (java.util.zip.ZipEntry)203 Manifest (java.util.jar.Manifest)186 ArrayList (java.util.ArrayList)156 Test (org.junit.Test)127 JarURLConnection (java.net.JarURLConnection)123 FileOutputStream (java.io.FileOutputStream)122 ZipFile (java.util.zip.ZipFile)121 FileInputStream (java.io.FileInputStream)111 Attributes (java.util.jar.Attributes)110 MalformedURLException (java.net.MalformedURLException)94 Enumeration (java.util.Enumeration)67 JarOutputStream (java.util.jar.JarOutputStream)65 HashSet (java.util.HashSet)58 URLClassLoader (java.net.URLClassLoader)55