Example usage for java.util.jar JarFile getInputStream

List of usage examples for java.util.jar JarFile getInputStream

Introduction

In this page you can find the example usage for java.util.jar JarFile getInputStream.

Prototype

public synchronized InputStream getInputStream(ZipEntry ze) throws IOException 

Source Link

Document

Returns an input stream for reading the contents of the specified zip file entry.

Usage

From source file:org.nuxeo.runtime.deployment.preprocessor.DeploymentPreprocessor.java

protected FragmentDescriptor getJARFragment(File file) throws IOException {
    FragmentDescriptor fd = null;//  ww  w.  j  av  a2 s .c o m
    JarFile jar = new JarFile(file);
    try {
        ZipEntry ze = jar.getEntry(FRAGMENT_FILE);
        if (ze != null) {
            InputStream in = new BufferedInputStream(jar.getInputStream(ze));
            try {
                fd = (FragmentDescriptor) xmap.load(in);
            } finally {
                in.close();
            }
            if (fd.name == null) {
                // fallback on symbolic name
                fd.name = getSymbolicName(file);
            }
            if (fd.name == null) {
                // fallback on artifact id
                fd.name = getJarArtifactName(file.getName());
            }
            if (fd.version == 0) { // compat with versions < 5.4
                processBundleForCompat(fd, file);
            }
        }
    } finally {
        jar.close();
    }
    return fd;
}

From source file:com.jayway.maven.plugins.android.phase04processclasses.DexMojo.java

private void unjar(JarFile jarFile, File outputDirectory) throws IOException {
    for (Enumeration en = jarFile.entries(); en.hasMoreElements();) {
        JarEntry entry = (JarEntry) en.nextElement();
        File entryFile = new File(outputDirectory, entry.getName());
        if (!entryFile.getParentFile().exists() && !entry.getName().startsWith("META-INF")) {
            entryFile.getParentFile().mkdirs();
        }/*  ww w . j a  v a 2  s .  c  om*/
        if (!entry.isDirectory() && entry.getName().endsWith(".class")) {
            final InputStream in = jarFile.getInputStream(entry);
            try {
                final OutputStream out = new FileOutputStream(entryFile);
                try {
                    IOUtil.copy(in, out);
                } finally {
                    IOUtils.closeQuietly(out);
                }
            } finally {
                IOUtils.closeQuietly(in);
            }
        }
    }
}

From source file:org.wso2.esb.integration.common.utils.common.FileManager.java

public void copyJarFile(String sourceFileLocationWithFileName, String destinationDirectory)
        throws IOException, URISyntaxException {
    File sourceFile = new File(getClass().getResource(sourceFileLocationWithFileName).toURI());
    File destinationFileDirectory = new File(destinationDirectory);
    JarFile jarFile = new JarFile(sourceFile);
    String fileName = jarFile.getName();
    String fileNameLastPart = fileName.substring(fileName.lastIndexOf(File.separator));
    File destinationFile = new File(destinationFileDirectory, fileNameLastPart);

    JarOutputStream jarOutputStream = new JarOutputStream(new FileOutputStream(destinationFile));
    Enumeration<JarEntry> entries = jarFile.entries();

    while (entries.hasMoreElements()) {
        JarEntry jarEntry = entries.nextElement();
        InputStream inputStream = jarFile.getInputStream(jarEntry);

        //jarOutputStream.putNextEntry(jarEntry);
        //create a new jarEntry to avoid ZipException: invalid jarEntry compressed size
        jarOutputStream.putNextEntry(new JarEntry(jarEntry.getName()));
        byte[] buffer = new byte[4096];
        int bytesRead = 0;
        while ((bytesRead = inputStream.read(buffer)) != -1) {
            jarOutputStream.write(buffer, 0, bytesRead);
        }//ww w  . ja va 2s .c  om
        inputStream.close();
        jarOutputStream.flush();
        jarOutputStream.closeEntry();
    }
    jarOutputStream.close();
}

From source file:org.jasig.portal.plugin.deployer.AbstractExtractingEarDeployer.java

/**
 * Reads the specified {@link JarEntry} from the {@link JarFile} and writes its contents
 * to the specified {@link File}./*from   w ww  .ja  va2s  . c  o m*/
 * 
 * @param earEntry The JarEntry for the file to read from the archive.
 * @param earFile The JarFile to get the {@link InputStream} for the file from.
 * @param destinationFile The File to write to, all parent directories should exist and no file should already exist at this location.
 * @throws IOException If the copying of data from the JarEntry to the File fails.
 */
protected void copyAndClose(JarEntry earEntry, JarFile earFile, File destinationFile)
        throws MojoFailureException {
    if (this.getLogger().isInfoEnabled()) {
        this.getLogger().info("Copying EAR entry '" + earFile.getName() + "!" + earEntry.getName() + "' to '"
                + destinationFile + "'");
    }

    InputStream jarEntryStream = null;
    try {
        jarEntryStream = earFile.getInputStream(earEntry);
        final OutputStream jarOutStream = new FileOutputStream(destinationFile);
        try {
            IOUtils.copy(jarEntryStream, jarOutStream);
        } finally {
            IOUtils.closeQuietly(jarOutStream);
        }
    } catch (IOException e) {
        throw new MojoFailureException("Failed to copy EAR entry '" + earEntry.getName() + "' out of '"
                + earFile.getName() + "' to '" + destinationFile + "'", e);
    } finally {
        IOUtils.closeQuietly(jarEntryStream);
    }
}

From source file:com.iflytek.edu.cloud.frame.doc.ServiceDocBuilder.java

private void doServiceJavaSource(String jarFileUrl) throws IOException {
    JarFile jarFile = new JarFile(jarFileUrl);
    try {//  w  w  w . ja  va  2s .  c  o  m
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("Looking for matching resources in jar file [" + jarFileUrl + "]");
        }

        for (Enumeration<JarEntry> entries = jarFile.entries(); entries.hasMoreElements();) {
            JarEntry entry = entries.nextElement();

            String entryPath = entry.getName();
            if (entryPath.endsWith(".java")) {
                InputStream inputStream = jarFile.getInputStream(entry);
                builder.addSource(new InputStreamReader(inputStream, "UTF-8"));
            }
        }
    } finally {
        jarFile.close();
    }
}

From source file:com.technophobia.substeps.report.DefaultExecutionReportBuilder.java

public void copyJarResourcesRecursively(final File destination, final JarURLConnection jarConnection)
        throws IOException {
    final JarFile jarFile = jarConnection.getJarFile();
    for (final JarEntry entry : Collections.list(jarFile.entries())) {
        if (entry.getName().startsWith(jarConnection.getEntryName())) {
            final String fileName = StringUtils.removeStart(entry.getName(), jarConnection.getEntryName());
            if (!entry.isDirectory()) {
                InputStream entryInputStream = null;
                try {
                    entryInputStream = jarFile.getInputStream(entry);
                    FileUtils.copyInputStreamToFile(entryInputStream, new File(destination, fileName));
                } finally {
                    IOUtils.closeQuietly(entryInputStream);
                }/*from w  w w  . j a va 2 s . com*/
            } else {
                new File(destination, fileName).mkdirs();
            }
        }
    }
}

From source file:UnpackedJarFile.java

public static File toTempFile(URL url) throws IOException {
    InputStream in = null;/*from w  w  w.j a v  a  2s  .  c  o m*/
    OutputStream out = null;
    JarFile jarFile = null;
    try {
        if (url.getProtocol().equalsIgnoreCase("jar")) {
            // url.openStream() locks the jar file and does not release the lock even after the stream is closed.
            // This problem is avoided by using JarFile APIs.
            File file = new File(url.getFile().substring(5, url.getFile().indexOf("!/")));
            String path = url.getFile().substring(url.getFile().indexOf("!/") + 2);
            jarFile = new JarFile(file);
            JarEntry jarEntry = jarFile.getJarEntry(path);
            if (jarEntry != null) {
                in = jarFile.getInputStream(jarEntry);
            } else {
                throw new FileNotFoundException("JarEntry " + path + " not found in " + file);
            }
        } else {
            in = url.openStream();
        }
        int index = url.getPath().lastIndexOf(".");
        String extension = null;
        if (index > 0) {
            extension = url.getPath().substring(index);
        }
        File tempFile = createTempFile(extension);

        out = new FileOutputStream(tempFile);

        writeAll(in, out);
        return tempFile;
    } finally {
        close(out);
        close(in);
        close(jarFile);
    }
}

From source file:org.javaan.bytecode.JarFileLoader.java

private void processEntry(String path, String fileName, JarFile jar, List<Type> classes, JarEntry entry)
        throws IOException {
    if (!entry.isDirectory()) {
        String name = entry.getName();
        boolean isClass = name.endsWith(".class");
        boolean isLibrary = name.endsWith(".jar") || name.endsWith(".war") || name.endsWith(".ear");
        if (isClass) {
            ClassParser parser = new ClassParser(fileName, entry.getName());
            JavaClass javaClass = parser.parse();
            String filePath = path + File.pathSeparator + javaClass.getFileName();
            Type type = Type.create(javaClass, filePath);
            classes.add(type);//from  w ww. j a v a  2 s.c om
        } else if (isLibrary) {
            InputStream input = jar.getInputStream(entry);
            try {
                processJar(path + File.pathSeparator + entry.getName(), input, classes);
            } finally {
                input.close();
            }
        }
    }
}

From source file:org.xwiki.webjars.internal.WebJarsExportURLFactoryActionHandler.java

private void copyResourceFromJAR(String resourcePath, String prefix, ExportURLFactoryContext factoryContext)
        throws IOException {
    // Note that we cannot use ClassLoader.getResource() to access the resource since the resourcePath may point
    // to a directory inside the JAR, and in this case we need to copy all resources inside that directory in the
    // JAR!/*from  ww w . ja v a2  s . c  o m*/

    JarFile jar = new JarFile(getJARFile(resourcePath));
    for (Enumeration<JarEntry> enumeration = jar.entries(); enumeration.hasMoreElements();) {
        JarEntry entry = enumeration.nextElement();
        if (entry.getName().startsWith(resourcePath) && !entry.isDirectory()) {
            // Copy the resource!
            String targetPath = prefix + entry.getName();
            File targetLocation = new File(factoryContext.getExportDir(), targetPath);
            if (!targetLocation.exists()) {
                targetLocation.getParentFile().mkdirs();
                FileOutputStream fos = new FileOutputStream(targetLocation);
                InputStream is = jar.getInputStream(entry);
                IOUtils.copy(is, fos);
                fos.close();
                is.close();
            }
        }
    }
    jar.close();
}

From source file:org.spoutcraft.launcher.util.Utils.java

public static void extractJar(JarFile jar, File dest, List<String> ignores) throws IOException {
    if (!dest.exists()) {
        dest.mkdirs();//from  ww  w  .j a v  a 2  s . c  o m
    } else {
        if (!dest.isDirectory()) {
            throw new IllegalArgumentException("The destination was not a directory");
        }
        FileUtils.cleanDirectory(dest);
    }
    Enumeration<JarEntry> entries = jar.entries();

    while (entries.hasMoreElements()) {
        JarEntry entry = entries.nextElement();
        File file = new File(dest, entry.getName());
        if (ignores != null) {
            boolean skip = false;
            for (String path : ignores) {
                if (entry.getName().startsWith(path)) {
                    skip = true;
                    break;
                }
            }
            if (skip) {
                continue;
            }
        }

        if (entry.getName().endsWith("/")) {
            if (!file.mkdir()) {
                if (ignores == null) {
                    ignores = new ArrayList<String>();
                }
                ignores.add(entry.getName());
            }
            continue;
        }

        if (file.exists()) {
            file.delete();
        }

        file.createNewFile();

        InputStream in = new BufferedInputStream(jar.getInputStream(entry));
        OutputStream out = new BufferedOutputStream(new FileOutputStream(file));

        byte buffer[] = new byte[1024];
        int len;
        while ((len = in.read(buffer)) > 0) {
            out.write(buffer, 0, len);
        }
        out.close();
        in.close();
    }
    jar.close();
}