Example usage for org.apache.commons.compress.archivers.jar JarArchiveInputStream JarArchiveInputStream

List of usage examples for org.apache.commons.compress.archivers.jar JarArchiveInputStream JarArchiveInputStream

Introduction

In this page you can find the example usage for org.apache.commons.compress.archivers.jar JarArchiveInputStream JarArchiveInputStream.

Prototype

public JarArchiveInputStream(final InputStream inputStream) 

Source Link

Usage

From source file:divconq.mod.JarLibLoader.java

public JarLibLoader(String name) {
    super(name);//from  w w  w . j  a  va 2 s .  c om

    JarArchiveInputStream stream = null;

    try {
        InputStream theFile = new FileInputStream(this.name);
        stream = new JarArchiveInputStream(theFile);

        JarArchiveEntry entry = stream.getNextJarEntry();

        while (entry != null) {
            if (!entry.isDirectory()) {
                //if (entry.getName().endsWith("Container.class"))
                //   System.out.println("at cont");

                int esize = (int) entry.getSize();

                if (esize > 0) {
                    int eleft = esize;
                    byte[] buff = new byte[esize];
                    int offset = 0;

                    while (offset < esize) {
                        int d = stream.read(buff, offset, eleft);
                        offset += d;
                        eleft -= d;
                    }

                    this.entries.put("/" + entry.getName(), buff);
                }
            }

            entry = stream.getNextJarEntry();
        }
    } catch (Exception x) {
        // TODO logging
        System.out.println(x);
    } finally {
        try {
            if (stream != null)
                stream.close();
        } catch (Exception x) {

        }
    }
}

From source file:com.soeima.resources.jar.JarArchive.java

/**
 * @see  AbstractCompressArchive#newArchiveInputStream(InputStream)
 *///w  ww. ja  v  a  2  s.c  o  m
@Override
protected ArchiveInputStream newArchiveInputStream(InputStream is) {
    return new JarArchiveInputStream(is);
}

From source file:com.torchmind.upm.bundle.BasicResource.java

/**
 * {@inheritDoc}/*  w  w w  . j  av  a2 s  .  c  o  m*/
 */
@Nonnull
@Override
public ArchiveInputStream createArchiveInputStream() throws IllegalStateException, IOException {
    switch (this.type) {
    case RAW:
        throw new IllegalStateException("Cannot convert RAW resource into archive");
    case JAR:
        return new JarArchiveInputStream(this.createInputStream());
    case TAR_ARCHIVE:
        return new TarArchiveInputStream(this.createInputStream());
    case TAR_GZ_ARCHIVE:
        return new TarArchiveInputStream(new GzipCompressorInputStream(this.createInputStream()));
    case TAR_XZ_ARCHIVE:
        return new TarArchiveInputStream(new XZCompressorInputStream(this.createInputStream()));
    case ZIP_ARCHIVE:
        return new ZipArchiveInputStream(this.createInputStream());
    }

    throw new UnsupportedOperationException("No such resource type: " + this.type);
}

From source file:com.offbynull.coroutines.instrumenter.asm.FileSystemClassInformationRepository.java

private void addJar(File file) throws IOException {
    Validate.notNull(file);//from  w  ww  .  java2s  .  c  o  m
    Validate.isTrue(file.isFile());
    try (FileInputStream fis = new FileInputStream(file);
            JarArchiveInputStream jais = new JarArchiveInputStream(fis)) {
        JarArchiveEntry entry;
        while ((entry = jais.getNextJarEntry()) != null) {
            if (!entry.getName().endsWith(".class") || entry.isDirectory()) {
                continue;
            }

            populateSuperClassMapping(jais);
        }
    }
}

From source file:es.jamisoft.comun.utils.compression.Jar.java

private void unjar(InputStream is, String outputDirectory) throws IOException {
    JarArchiveInputStream jis = new JarArchiveInputStream(is);
    JarArchiveEntry jarEntry = null;/*  w w  w  .ja  v  a  2s .c o  m*/
    byte buffer[] = new byte[1024];
    int readCount = 0;

    if (!outputDirectory.endsWith(File.separator)) {
        outputDirectory = outputDirectory + File.separator;
    }

    do {
        if ((jarEntry = jis.getNextJarEntry()) == null) {
            break;
        }

        if (jarEntry.isDirectory()) {
            File file = new File(outputDirectory + jarEntry.getName());

            if (!file.exists()) {
                file.mkdir();
            }
        } else {
            FileOutputStream fos = new FileOutputStream(outputDirectory + jarEntry.getName());

            while ((readCount = jis.read(buffer)) != -1) {
                fos.write(buffer, 0, readCount);
            }

            fos.close();
        }
    } while (true);

    jis.close();
}

From source file:org.apache.openejb.maven.plugin.customizer.monkey.jar.JarPatcher.java

private int unjar(final File exploded, final File from) {
    int method = -1;

    JarArchiveInputStream stream = null;
    try {//  w w w.j ava2 s  .c o m
        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;
}

From source file:org.apache.sysml.utils.lite.BuildLite.java

/**
 * Obtain a list of all classes in a jar file corresponding to a referenced
 * class./*www.j  a v  a2 s.  com*/
 * 
 * @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;
}

From source file:org.apereo.portal.io.xml.JaxbPortalDataHandlerService.java

protected void importDataArchive(Resource archive, InputStream resourceStream, BatchImportOptions options) {
    BufferedInputStream bufferedResourceStream = null;
    try {/*from   www .  j  av  a  2s.co  m*/
        //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);
    }
}