Search in sources :

Example 26 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)

Example 27 with JarEntry

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

the class WarBundleManifestGenerator method getJarsInWebInfLib.

private static List<String> getJarsInWebInfLib(JarFile jarFile) {
    List<String> res = new ArrayList<String>();
    Enumeration<JarEntry> en = jarFile.entries();
    while (en.hasMoreElements()) {
        JarEntry e = en.nextElement();
        if (e.getName().startsWith("WEB-INF/lib/") && e.getName().endsWith(".jar")) {
            res.add(e.getName());
        }
    }
    return res;
}
Also used : ArrayList(java.util.ArrayList) JarEntry(java.util.jar.JarEntry)

Example 28 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 29 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 30 with JarEntry

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

the class JarDirectoryStepHelper method createJarFile.

public static int createJarFile(ProjectFilesystem filesystem, Path pathToOutputFile, CustomZipOutputStream outputFile, ImmutableSortedSet<Path> entriesToJar, ImmutableSet<String> alreadyAddedEntriesToOutputFile, Optional<String> mainClass, Optional<Path> manifestFile, boolean mergeManifests, Iterable<Pattern> blacklist, JavacEventSink eventSink, PrintStream stdErr) throws IOException {
    Set<String> alreadyAddedEntries = Sets.newHashSet(alreadyAddedEntriesToOutputFile);
    // Write the manifest first.
    JarEntry metaInf = new JarEntry("META-INF/");
    // We want deterministic JARs, so avoid mtimes. -1 is timzeone independent, 0 is not.
    metaInf.setTime(ZipConstants.getFakeTime());
    outputFile.putNextEntry(metaInf);
    outputFile.closeEntry();
    alreadyAddedEntries.add("META-INF/");
    Manifest manifest = createManifest(filesystem, entriesToJar, mainClass, manifestFile, mergeManifests);
    JarEntry manifestEntry = new JarEntry(JarFile.MANIFEST_NAME);
    // We want deterministic JARs, so avoid mtimes. -1 is timzeone independent, 0 is not.
    manifestEntry.setTime(ZipConstants.getFakeTime());
    outputFile.putNextEntry(manifestEntry);
    manifest.write(outputFile);
    outputFile.closeEntry();
    alreadyAddedEntries.add(JarFile.MANIFEST_NAME);
    Path absoluteOutputPath = filesystem.getPathForRelativePath(pathToOutputFile);
    for (Path entry : entriesToJar) {
        Path file = filesystem.getPathForRelativePath(entry);
        if (Files.isRegularFile(file)) {
            Preconditions.checkArgument(!file.equals(absoluteOutputPath), "Trying to put file %s into itself", file);
            // Assume the file is a ZIP/JAR file.
            copyZipEntriesToJar(file, pathToOutputFile, outputFile, alreadyAddedEntries, eventSink, blacklist);
        } else if (Files.isDirectory(file)) {
            addFilesInDirectoryToJar(filesystem, file, outputFile, alreadyAddedEntries, blacklist, eventSink);
        } else {
            throw new IllegalStateException("Must be a file or directory: " + file);
        }
    }
    if (mainClass.isPresent() && !mainClassPresent(mainClass.get(), alreadyAddedEntries)) {
        stdErr.print(String.format("ERROR: Main class %s does not exist.\n", mainClass.get()));
        return 1;
    }
    return 0;
}
Also used : Path(java.nio.file.Path) JarEntry(java.util.jar.JarEntry) Manifest(java.util.jar.Manifest)

Aggregations

JarEntry (java.util.jar.JarEntry)506 JarFile (java.util.jar.JarFile)238 File (java.io.File)188 IOException (java.io.IOException)167 InputStream (java.io.InputStream)116 JarOutputStream (java.util.jar.JarOutputStream)102 FileOutputStream (java.io.FileOutputStream)96 FileInputStream (java.io.FileInputStream)79 JarInputStream (java.util.jar.JarInputStream)76 URL (java.net.URL)70 ArrayList (java.util.ArrayList)55 Manifest (java.util.jar.Manifest)50 JarURLConnection (java.net.JarURLConnection)42 Test (org.junit.Test)34 ZipFile (java.util.zip.ZipFile)30 ZipEntry (java.util.zip.ZipEntry)29 OutputStream (java.io.OutputStream)26 HashSet (java.util.HashSet)26 BufferedInputStream (java.io.BufferedInputStream)21 ByteArrayInputStream (java.io.ByteArrayInputStream)20