Search in sources :

Example 1 with JarArchiveInputStream

use of org.apache.commons.compress.archivers.jar.JarArchiveInputStream in project systemml by apache.

the class BuildLite method getAllClassesInJar.

/**
 * Obtain a list of all classes in a jar file corresponding to a referenced
 * class.
 *
 * @param classInJarFile
 * @return list of all the commons-math3 classes in the referenced
 *         commons-math3 jar file
 * @throws IOException
 *             if an IOException occurs
 * @throws ClassNotFoundException
 *             if a ClassNotFoundException occurs
 */
private static List<String> getAllClassesInJar(Class<?> classInJarFile) throws IOException, ClassNotFoundException {
    List<String> classPathNames = new ArrayList<>();
    String jarLocation = classInJarFile.getProtectionDomain().getCodeSource().getLocation().getPath();
    File f = new File(jarLocation);
    try (FileInputStream fis = new FileInputStream(f);
        JarArchiveInputStream jais = new JarArchiveInputStream(fis)) {
        while (true) {
            JarArchiveEntry jae = jais.getNextJarEntry();
            if (jae == null) {
                break;
            }
            String name = jae.getName();
            if (name.endsWith(".class")) {
                classPathNames.add(name);
            }
        }
    }
    String jarName = jarLocation.substring(jarLocation.lastIndexOf("/") + 1);
    addClassPathNamesToJarsAndClasses(jarName, classPathNames);
    return classPathNames;
}
Also used : JarArchiveInputStream(org.apache.commons.compress.archivers.jar.JarArchiveInputStream) JarArchiveEntry(org.apache.commons.compress.archivers.jar.JarArchiveEntry) ArrayList(java.util.ArrayList) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 2 with JarArchiveInputStream

use of org.apache.commons.compress.archivers.jar.JarArchiveInputStream in project tomee by apache.

the class JarPatcher method unjar.

private int unjar(final File exploded, final File from) {
    int method = -1;
    JarArchiveInputStream stream = null;
    try {
        stream = new JarArchiveInputStream(new FileInputStream(from));
        JarArchiveEntry entry;
        while ((entry = stream.getNextJarEntry()) != null) {
            final File archiveEntry = new File(exploded, entry.getName());
            archiveEntry.getParentFile().mkdirs();
            if (entry.isDirectory()) {
                archiveEntry.mkdir();
                continue;
            }
            final OutputStream out = new FileOutputStream(archiveEntry);
            IOUtils.copy(stream, out);
            out.close();
            if (method < 0) {
                method = entry.getMethod();
            }
        }
    } catch (final IOException e) {
        throw new IllegalArgumentException(e);
    } finally {
        IO.close(stream);
    }
    return method;
}
Also used : JarArchiveInputStream(org.apache.commons.compress.archivers.jar.JarArchiveInputStream) JarArchiveEntry(org.apache.commons.compress.archivers.jar.JarArchiveEntry) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) JarArchiveOutputStream(org.apache.commons.compress.archivers.jar.JarArchiveOutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 3 with JarArchiveInputStream

use of org.apache.commons.compress.archivers.jar.JarArchiveInputStream in project packr by libgdx.

the class ArchiveUtils method extractJarArchive.

/**
 * Extracts a JAR archive. If the current platform supports POSIX permissions, the archive entry permissions are applied to the created file or directory.
 * Symbolic links are also supported.
 *
 * @param inputStream        the archive input stream
 * @param extractToDirectory the directory to extract the archive into
 * @throws IOException if an IO error occurs
 */
private static void extractJarArchive(InputStream inputStream, Path extractToDirectory) throws IOException {
    final JarArchiveInputStream archiveInputStream = new JarArchiveInputStream(inputStream);
    JarArchiveEntry entry;
    while ((entry = archiveInputStream.getNextJarEntry()) != null) {
        if (!archiveInputStream.canReadEntryData(entry)) {
            LOG.error("Failed to read archive entry " + entry);
            continue;
        }
        extractZipEntry(extractToDirectory, archiveInputStream, entry);
    }
}
Also used : JarArchiveInputStream(org.apache.commons.compress.archivers.jar.JarArchiveInputStream) JarArchiveEntry(org.apache.commons.compress.archivers.jar.JarArchiveEntry)

Example 4 with JarArchiveInputStream

use of org.apache.commons.compress.archivers.jar.JarArchiveInputStream in project uPortal by Jasig.

the class JaxbPortalDataHandlerService method importDataArchive.

private void importDataArchive(Resource archive, InputStream resourceStream, BatchImportOptions options) {
    BufferedInputStream bufferedResourceStream = null;
    try {
        // Make sure the stream is buffered
        if (resourceStream instanceof BufferedInputStream) {
            bufferedResourceStream = (BufferedInputStream) resourceStream;
        } else {
            bufferedResourceStream = new BufferedInputStream(resourceStream);
        }
        // Buffer up to 100MB, bad things will happen if we bust this buffer.
        // TODO see if there is a buffered stream that will write to a file once the buffer
        // fills up
        bufferedResourceStream.mark(100 * 1024 * 1024);
        final MediaType type = getMediaType(bufferedResourceStream, archive.getFilename());
        if (MT_JAVA_ARCHIVE.equals(type)) {
            final ArchiveInputStream archiveStream = new JarArchiveInputStream(bufferedResourceStream);
            importDataArchive(archive, archiveStream, options);
        } else if (MediaType.APPLICATION_ZIP.equals(type)) {
            final ArchiveInputStream archiveStream = new ZipArchiveInputStream(bufferedResourceStream);
            importDataArchive(archive, archiveStream, options);
        } else if (MT_CPIO.equals(type)) {
            final ArchiveInputStream archiveStream = new CpioArchiveInputStream(bufferedResourceStream);
            importDataArchive(archive, archiveStream, options);
        } else if (MT_AR.equals(type)) {
            final ArchiveInputStream archiveStream = new ArArchiveInputStream(bufferedResourceStream);
            importDataArchive(archive, archiveStream, options);
        } else if (MT_TAR.equals(type)) {
            final ArchiveInputStream archiveStream = new TarArchiveInputStream(bufferedResourceStream);
            importDataArchive(archive, archiveStream, options);
        } else if (MT_BZIP2.equals(type)) {
            final CompressorInputStream compressedStream = new BZip2CompressorInputStream(bufferedResourceStream);
            importDataArchive(archive, compressedStream, options);
        } else if (MT_GZIP.equals(type)) {
            final CompressorInputStream compressedStream = new GzipCompressorInputStream(bufferedResourceStream);
            importDataArchive(archive, compressedStream, options);
        } else if (MT_PACK200.equals(type)) {
            final CompressorInputStream compressedStream = new Pack200CompressorInputStream(bufferedResourceStream);
            importDataArchive(archive, compressedStream, options);
        } else if (MT_XZ.equals(type)) {
            final CompressorInputStream compressedStream = new XZCompressorInputStream(bufferedResourceStream);
            importDataArchive(archive, compressedStream, options);
        } else {
            throw new RuntimeException("Unrecognized archive media type: " + type);
        }
    } catch (IOException e) {
        throw new RuntimeException("Could not load InputStream for resource: " + archive, e);
    } finally {
        IOUtils.closeQuietly(bufferedResourceStream);
    }
}
Also used : JarArchiveInputStream(org.apache.commons.compress.archivers.jar.JarArchiveInputStream) GzipCompressorInputStream(org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream) ZipArchiveInputStream(org.apache.commons.compress.archivers.zip.ZipArchiveInputStream) ArArchiveInputStream(org.apache.commons.compress.archivers.ar.ArArchiveInputStream) CompressorInputStream(org.apache.commons.compress.compressors.CompressorInputStream) XZCompressorInputStream(org.apache.commons.compress.compressors.xz.XZCompressorInputStream) Pack200CompressorInputStream(org.apache.commons.compress.compressors.pack200.Pack200CompressorInputStream) BZip2CompressorInputStream(org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream) GzipCompressorInputStream(org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream) IOException(java.io.IOException) Pack200CompressorInputStream(org.apache.commons.compress.compressors.pack200.Pack200CompressorInputStream) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) JarArchiveInputStream(org.apache.commons.compress.archivers.jar.JarArchiveInputStream) ArchiveInputStream(org.apache.commons.compress.archivers.ArchiveInputStream) CpioArchiveInputStream(org.apache.commons.compress.archivers.cpio.CpioArchiveInputStream) ArArchiveInputStream(org.apache.commons.compress.archivers.ar.ArArchiveInputStream) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) ZipArchiveInputStream(org.apache.commons.compress.archivers.zip.ZipArchiveInputStream) BZip2CompressorInputStream(org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream) BufferedInputStream(java.io.BufferedInputStream) MediaType(org.apache.tika.mime.MediaType) CpioArchiveInputStream(org.apache.commons.compress.archivers.cpio.CpioArchiveInputStream) XZCompressorInputStream(org.apache.commons.compress.compressors.xz.XZCompressorInputStream)

Example 5 with JarArchiveInputStream

use of org.apache.commons.compress.archivers.jar.JarArchiveInputStream in project incubator-systemml by apache.

the class BuildLite method getAllClassesInJar.

/**
 * Obtain a list of all classes in a jar file corresponding to a referenced
 * class.
 *
 * @param classInJarFile
 * @return list of all the commons-math3 classes in the referenced
 *         commons-math3 jar file
 * @throws IOException
 *             if an IOException occurs
 * @throws ClassNotFoundException
 *             if a ClassNotFoundException occurs
 */
private static List<String> getAllClassesInJar(Class<?> classInJarFile) throws IOException, ClassNotFoundException {
    List<String> classPathNames = new ArrayList<>();
    String jarLocation = classInJarFile.getProtectionDomain().getCodeSource().getLocation().getPath();
    File f = new File(jarLocation);
    try (FileInputStream fis = new FileInputStream(f);
        JarArchiveInputStream jais = new JarArchiveInputStream(fis)) {
        while (true) {
            JarArchiveEntry jae = jais.getNextJarEntry();
            if (jae == null) {
                break;
            }
            String name = jae.getName();
            if (name.endsWith(".class")) {
                classPathNames.add(name);
            }
        }
    }
    String jarName = jarLocation.substring(jarLocation.lastIndexOf("/") + 1);
    addClassPathNamesToJarsAndClasses(jarName, classPathNames);
    return classPathNames;
}
Also used : JarArchiveInputStream(org.apache.commons.compress.archivers.jar.JarArchiveInputStream) JarArchiveEntry(org.apache.commons.compress.archivers.jar.JarArchiveEntry) ArrayList(java.util.ArrayList) File(java.io.File) FileInputStream(java.io.FileInputStream)

Aggregations

JarArchiveInputStream (org.apache.commons.compress.archivers.jar.JarArchiveInputStream)5 JarArchiveEntry (org.apache.commons.compress.archivers.jar.JarArchiveEntry)4 File (java.io.File)3 FileInputStream (java.io.FileInputStream)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 BufferedInputStream (java.io.BufferedInputStream)1 FileOutputStream (java.io.FileOutputStream)1 OutputStream (java.io.OutputStream)1 ArchiveInputStream (org.apache.commons.compress.archivers.ArchiveInputStream)1 ArArchiveInputStream (org.apache.commons.compress.archivers.ar.ArArchiveInputStream)1 CpioArchiveInputStream (org.apache.commons.compress.archivers.cpio.CpioArchiveInputStream)1 JarArchiveOutputStream (org.apache.commons.compress.archivers.jar.JarArchiveOutputStream)1 TarArchiveInputStream (org.apache.commons.compress.archivers.tar.TarArchiveInputStream)1 ZipArchiveInputStream (org.apache.commons.compress.archivers.zip.ZipArchiveInputStream)1 CompressorInputStream (org.apache.commons.compress.compressors.CompressorInputStream)1 BZip2CompressorInputStream (org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream)1 GzipCompressorInputStream (org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream)1 Pack200CompressorInputStream (org.apache.commons.compress.compressors.pack200.Pack200CompressorInputStream)1 XZCompressorInputStream (org.apache.commons.compress.compressors.xz.XZCompressorInputStream)1