Search in sources :

Example 6 with JarInputStream

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

the class JarResource method copyTo.

/* ------------------------------------------------------------ */
@Override
public void copyTo(File directory) throws IOException {
    if (!exists())
        return;
    if (LOG.isDebugEnabled())
        LOG.debug("Extract " + this + " to " + directory);
    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));
    String subEntryName = (endOfJarUrl + 2 < urlString.length() ? urlString.substring(endOfJarUrl + 2) : null);
    boolean subEntryIsDir = (subEntryName != null && subEntryName.endsWith("/") ? true : false);
    if (LOG.isDebugEnabled())
        LOG.debug("Extracting entry = " + subEntryName + " from jar " + jarFileURL);
    URLConnection c = jarFileURL.openConnection();
    c.setUseCaches(false);
    try (InputStream is = c.getInputStream();
        JarInputStream jin = new JarInputStream(is)) {
        JarEntry entry;
        boolean shouldExtract;
        while ((entry = jin.getNextJarEntry()) != null) {
            String entryName = entry.getName();
            if ((subEntryName != null) && (entryName.startsWith(subEntryName))) {
                // is the subentry really a dir?
                if (!subEntryIsDir && subEntryName.length() + 1 == entryName.length() && entryName.endsWith("/"))
                    subEntryIsDir = true;
                //extract it.
                if (subEntryIsDir) {
                    //if it is a subdirectory we are looking for, then we
                    //are looking to extract its contents into the target
                    //directory. Remove the name of the subdirectory so
                    //that we don't wind up creating it too.
                    entryName = entryName.substring(subEntryName.length());
                    if (!entryName.equals("")) {
                        //the entry is
                        shouldExtract = true;
                    } else
                        shouldExtract = false;
                } else
                    shouldExtract = true;
            } else if ((subEntryName != null) && (!entryName.startsWith(subEntryName))) {
                //there is a particular entry we are looking for, and this one
                //isn't it
                shouldExtract = false;
            } else {
                //we are extracting everything
                shouldExtract = true;
            }
            if (!shouldExtract) {
                if (LOG.isDebugEnabled())
                    LOG.debug("Skipping entry: " + entryName);
                continue;
            }
            String dotCheck = entryName.replace('\\', '/');
            dotCheck = URIUtil.canonicalPath(dotCheck);
            if (dotCheck == null) {
                if (LOG.isDebugEnabled())
                    LOG.debug("Invalid entry: " + entryName);
                continue;
            }
            File file = new File(directory, entryName);
            if (entry.isDirectory()) {
                // Make directory
                if (!file.exists())
                    file.mkdirs();
            } else {
                // 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());
            }
        }
        if ((subEntryName == null) || (subEntryName != null && subEntryName.equalsIgnoreCase("META-INF/MANIFEST.MF"))) {
            Manifest manifest = jin.getManifest();
            if (manifest != null) {
                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) FilterInputStream(java.io.FilterInputStream) JarInputStream(java.util.jar.JarInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) JarEntry(java.util.jar.JarEntry) Manifest(java.util.jar.Manifest) URL(java.net.URL) URLConnection(java.net.URLConnection) JarURLConnection(java.net.JarURLConnection) FileOutputStream(java.io.FileOutputStream) File(java.io.File)

Example 7 with JarInputStream

use of java.util.jar.JarInputStream 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 8 with JarInputStream

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

the class WarURLConnection method substitueManifest.

/**
     * Use PipedOuputStream and PipedInputStream to do the transformation without making
     * a new temporary file ust to replace the manifest.
     * @param newmanifest The new manifest
     * @param rawIn The file input stream or equivalent. not the jar input stream.
     */
public static InputStream substitueManifest(final Manifest newmanifest, final InputStream rawIn) throws IOException {
    final PipedOutputStream pOut = new PipedOutputStream();
    PipedInputStream pIn = new PipedInputStream(pOut);
    Runnable run = new Runnable() {

        public void run() {
            JarInputStream jin = null;
            JarOutputStream dest = null;
            try {
                jin = new JarInputStream(rawIn, false);
                dest = new JarOutputStream(pOut, newmanifest);
                ZipEntry next = jin.getNextEntry();
                while (next != null) {
                    if (next.getName().equalsIgnoreCase(JarFile.MANIFEST_NAME)) {
                        continue;
                    }
                    dest.putNextEntry(next);
                    if (next.getSize() > 0) {
                        IO.copy(jin, dest, next.getSize());
                    }
                    next = jin.getNextJarEntry();
                }
            } catch (IOException ioe) {
                ioe.printStackTrace();
            } finally {
                if (dest != null)
                    IO.close(dest);
                if (jin != null)
                    IO.close(jin);
                IO.close(pOut);
            }
        }
    };
    Thread th = new Thread(run);
    th.start();
    return pIn;
}
Also used : JarInputStream(java.util.jar.JarInputStream) ZipEntry(java.util.zip.ZipEntry) JarOutputStream(java.util.jar.JarOutputStream) PipedOutputStream(java.io.PipedOutputStream) PipedInputStream(java.io.PipedInputStream) IOException(java.io.IOException)

Example 9 with JarInputStream

use of java.util.jar.JarInputStream 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 10 with JarInputStream

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

the class HashingDeterministicJarWriterTest method entriesAreWrittenAsTheyAreEncounteredWithManifestLast.

@Test
public void entriesAreWrittenAsTheyAreEncounteredWithManifestLast() throws IOException {
    writer.writeEntry("Z", new ByteArrayInputStream("Z's contents".getBytes(StandardCharsets.UTF_8)));
    writer.writeEntry("A", new ByteArrayInputStream("A's contents".getBytes(StandardCharsets.UTF_8)));
    writer.close();
    try (JarInputStream jar = new JarInputStream(new ByteArrayInputStream(out.toByteArray()))) {
        JarEntry entry;
        entry = jar.getNextJarEntry();
        assertEquals("Z", entry.getName());
        entry = jar.getNextJarEntry();
        assertEquals("A", entry.getName());
        entry = jar.getNextJarEntry();
        assertEquals(JarFile.MANIFEST_NAME, entry.getName());
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) JarInputStream(java.util.jar.JarInputStream) JarEntry(java.util.jar.JarEntry) Test(org.junit.Test)

Aggregations

JarInputStream (java.util.jar.JarInputStream)154 JarEntry (java.util.jar.JarEntry)72 IOException (java.io.IOException)66 FileInputStream (java.io.FileInputStream)63 Manifest (java.util.jar.Manifest)50 File (java.io.File)46 InputStream (java.io.InputStream)45 ZipEntry (java.util.zip.ZipEntry)29 JarOutputStream (java.util.jar.JarOutputStream)27 FileOutputStream (java.io.FileOutputStream)24 ByteArrayInputStream (java.io.ByteArrayInputStream)22 URL (java.net.URL)20 Test (org.junit.Test)20 OutputStream (java.io.OutputStream)13 JarFile (java.util.jar.JarFile)13 BufferedInputStream (java.io.BufferedInputStream)11 ByteArrayOutputStream (java.io.ByteArrayOutputStream)11 ArrayList (java.util.ArrayList)10 Attributes (java.util.jar.Attributes)10 HashSet (java.util.HashSet)8