Example usage for java.util.jar JarFile JarFile

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

Introduction

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

Prototype

public JarFile(File file) throws IOException 

Source Link

Document

Creates a new JarFile to read from the specified File object.

Usage

From source file:org.ormma.controller.OrmmaAssetController.java

/**
 * Copy text file from jar into asset directory.
 * /*from  ww w.java2  s  .com*/
 * @param alias
 *            the alias to store it in
 * @param source
 *            the source
 * @return the path to the copied asset
 */
public String copyTextFromJarIntoAssetDir(String alias, String source) {
    InputStream in = null;
    try {
        URL url = OrmmaAssetController.class.getClassLoader().getResource(source);
        String file = url.getFile();
        if (file.startsWith("file:")) {
            file = file.substring(5);
        }
        int pos = file.indexOf("!");
        if (pos > 0)
            file = file.substring(0, pos);
        JarFile jf = new JarFile(file);
        JarEntry entry = jf.getJarEntry(source);
        in = jf.getInputStream(entry);
        String name = writeToDisk(in, alias, false);
        return name;
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (Exception e) {
                // TODO: handle exception
            }
            in = null;
        }
    }
    return null;
}

From source file:com.enderville.enderinstaller.util.InstallScript.java

/**
 * Unpacks all the contents of the old minecraft.jar to the temp directory.
 *
 * @param tmpdir The temp directory to unpack to.
 * @param mcjar The location of the old minecraft.jar
 * @throws IOException/*from   ww  w  . j  a v a  2  s.c  o m*/
 */
public static void unpackMCJar(File tmpdir, File mcjar) throws IOException {
    byte[] dat = new byte[4 * 1024];
    JarFile jar = new JarFile(mcjar);
    Enumeration<JarEntry> entries = jar.entries();
    while (entries.hasMoreElements()) {
        JarEntry entry = entries.nextElement();
        String name = entry.getName();
        //This gets rid of META-INF if it exists.
        if (name.startsWith("META-INF")) {
            continue;
        }
        InputStream in = jar.getInputStream(entry);
        File dest = new File(FilenameUtils.concat(tmpdir.getPath(), name));
        if (entry.isDirectory()) {
            //I don't think this actually happens
            LOGGER.warn("Found a directory while iterating over jar.");
            dest.mkdirs();
        } else if (!dest.getParentFile().exists()) {
            if (!dest.getParentFile().mkdirs()) {
                throw new IOException("Couldn't create directory for " + name);
            }
        }
        FileOutputStream out = new FileOutputStream(dest);
        int len = -1;
        while ((len = in.read(dat)) > 0) {
            out.write(dat, 0, len);
        }
        out.flush();
        out.close();
        in.close();
    }
}

From source file:net.erdfelt.android.sdkfido.local.JarListing.java

private final void loadJar(File jarfile) throws IOException {
    JarFile jar = null;/*from  w w w .  ja  va 2 s. co  m*/

    try {
        String name;
        jar = new JarFile(jarfile);
        Enumeration<JarEntry> entries = jar.entries();
        while (entries.hasMoreElements()) {
            JarEntry entry = entries.nextElement();
            name = entry.getName();
            add(name);

            if (!entry.isDirectory() && name.endsWith(".class")) {
                classlist.add(name);
            }
        }
    } finally {
        if (jar != null) {
            jar.close();
        }
    }
}

From source file:org.apache.sqoop.connector.ConnectorManagerUtils.java

static boolean isConnectorJar(File file) {
    try {//from   ww w  .  ja v  a  2  s . c  o m
        @SuppressWarnings("resource")
        JarEntry entry = new JarFile(file).getJarEntry(ConfigurationConstants.FILENAME_CONNECTOR_PROPERTIES);
        return entry != null;
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.commonjava.web.test.fixture.JarKnockouts.java

public static File rewriteJar(final File source, final File targetDir, final Set<JarKnockouts> jarKnockouts)
        throws IOException {
    final JarKnockouts allKnockouts = new JarKnockouts();
    for (final JarKnockouts jk : jarKnockouts) {
        allKnockouts.knockoutPaths(jk.getKnockedOutPaths());
    }/*  www.j a  v a2s. c o  m*/

    targetDir.mkdirs();
    final File target = new File(targetDir, source.getName());

    JarFile in = null;
    JarOutputStream out = null;
    try {
        in = new JarFile(source);

        final BufferedOutputStream fos = new BufferedOutputStream(new FileOutputStream(target));
        out = new JarOutputStream(fos, in.getManifest());

        final Enumeration<JarEntry> entries = in.entries();
        while (entries.hasMoreElements()) {
            final JarEntry entry = entries.nextElement();
            if (!allKnockouts.knockout(entry.getName())) {
                final InputStream stream = in.getInputStream(entry);
                out.putNextEntry(entry);
                copy(stream, out);
                out.closeEntry();
            }
        }
    } finally {
        closeQuietly(out);
        if (in != null) {
            try {
                in.close();
            } catch (final IOException e) {
            }
        }
    }

    return target;
}

From source file:se.crisp.codekvast.agent.daemon.appversion.ManifestAppVersionStrategy.java

@Override
public String resolveAppVersion(Collection<File> codeBases, String[] args) {
    String jarUri = args[1];//  w w w  .j  a  v  a  2  s .  co  m
    String manifestAttribute = args.length > 2 ? args[2] : DEFAULT_MANIFEST_ATTRIBUTE;
    for (File codeBaseFile : codeBases) {
        try (JarFile jarFile = new JarFile(getJarFile(codeBaseFile, jarUri))) {
            Attributes attributes = jarFile.getManifest().getMainAttributes();
            String resolvedVersion = attributes.getValue(manifestAttribute);
            if (resolvedVersion != null) {
                log.debug("{}!/META-INF/MANIFEST.MF:{}={}", jarUri, manifestAttribute, resolvedVersion);
                return resolvedVersion;
            }
            if (!manifestAttribute.equalsIgnoreCase(DEFAULT_MANIFEST_ATTRIBUTE)) {
                resolvedVersion = attributes.getValue(DEFAULT_MANIFEST_ATTRIBUTE);
            }
            if (resolvedVersion != null) {
                log.debug("{}!/META-INF/MANIFEST.MF:{}={}", jarUri, DEFAULT_MANIFEST_ATTRIBUTE,
                        resolvedVersion);
                return resolvedVersion;
            }
        } catch (Exception e) {
            log.error("Cannot open " + jarUri + ": " + e);
        }
    }
    log.error("Cannot resolve {}!/META-INF/MANIFEST.MF:{}", jarUri, manifestAttribute);
    return UNKNOWN_VERSION;
}

From source file:org.commonjava.maven.galley.filearc.internal.ZipPublish.java

private Boolean writeArchive(final File dest, final String path) {
    final boolean isJar = isJarOperation();
    FileOutputStream fos = null;//from   ww w  .ja v  a 2 s.co m
    ZipOutputStream zos = null;
    ZipFile zf = null;
    try {
        fos = new FileOutputStream(dest);
        zos = isJar ? new JarOutputStream(fos) : new ZipOutputStream(fos);

        zf = isJar ? new JarFile(dest) : new ZipFile(dest);

        final ZipEntry entry = zf.getEntry(path);
        zos.putNextEntry(entry);
        copy(stream, zos);

        return true;
    } catch (final IOException e) {
        error = new TransferException("Failed to write path: %s to NEW archive: %s. Reason: %s", e, path, dest,
                e.getMessage());
    } finally {
        closeQuietly(zos);
        closeQuietly(fos);
        if (zf != null) {
            //noinspection EmptyCatchBlock
            try {
                zf.close();
            } catch (final IOException e) {
            }
        }
        closeQuietly(stream);
    }

    return false;
}

From source file:com.netease.hearttouch.hthotfix.HashTransformExecutor.java

public void transformJar(File inputFile, File outputFile) throws IOException {
    logger.info("HASHTRANSFORMJAR\t" + outputFile.getAbsolutePath());
    final JarFile jar = new JarFile(inputFile);
    final OutputJar outputJar = new OutputJar(outputFile);
    for (final Enumeration<JarEntry> entries = jar.entries(); entries.hasMoreElements();) {
        final JarEntry entry = entries.nextElement();
        if (entry.isDirectory()) {
            //                new File(outFile, entry.getName()).mkdirs();
            continue;
        }// w  w w .ja  v  a2 s  . c  o  m
        final InputStream is = jar.getInputStream(entry);

        try {
            byte[] bytecode = IOUtils.toByteArray(is);
            if (entry.getName().endsWith(".class")) {
                if (!extension.getGeneratePatch()) {
                    hashFileGenerator.addClass(bytecode);
                    ClassReader cr = new ClassReader(bytecode);

                    outputJar.writeEntry(entry, hackInjector.inject(cr.getClassName(), bytecode));
                } else {
                    outputJar.writeEntry(entry, bytecode);
                }
            } else {
                outputJar.writeEntry(entry, bytecode);
            }
        } finally {
            IOUtils.closeQuietly(is);
        }
    }
    outputJar.close();
}

From source file:com.katsu.dwm.web.Loader.java

private void copyContent(File f, JarEntry je) {
    File rootPath = JarUtils.getRootApplicationContentPath();
    File temp = new File(rootPath, je.getName());
    if (je.isDirectory()) {
        logger.debug("Create path: " + temp.mkdirs() + " " + temp.getPath());
    } else {//from ww  w.j av  a 2 s  . c o  m
        InputStream is = null;
        FileOutputStream fos = null;
        try {
            JarFile jf = new JarFile(f);
            is = jf.getInputStream(je);
            if (!temp.exists()) {
                temp.createNewFile();
            }
            fos = new FileOutputStream(temp);
            byte[] array = new byte[1024];
            int readed;
            while ((readed = is.read(array)) != -1) {
                fos.write(array, 0, readed);
            }
        } catch (Exception e) {
            logger.error(e);
        } finally {
            try {
                if (is != null) {
                    is.close();
                }
            } catch (IOException ex) {
            }
            try {
                if (fos != null) {
                    fos.close();
                }
            } catch (IOException ex) {
            }
        }
    }
}

From source file:com.gzj.tulip.load.vfs.JarFileObject.java

JarFileObject(FileSystemManager fs, URL url) throws FileNotFoundException, IOException {
    this.fs = fs;
    String urlString = url.toString();
    String entryName = urlString.substring(
            urlString.indexOf(ResourceUtils.JAR_URL_SEPARATOR) + ResourceUtils.JAR_URL_SEPARATOR.length());
    if (entryName.length() == 0) {
        this.root = this;
        int beginIndex = urlString.indexOf(ResourceUtils.FILE_URL_PREFIX)
                + ResourceUtils.FILE_URL_PREFIX.length();
        int endIndex = urlString.indexOf(ResourceUtils.JAR_URL_SEPARATOR);
        this.jarFile = new JarFile(urlString.substring(beginIndex, endIndex));
    } else {/*from   w ww. jav  a 2s.c om*/
        this.root = (JarFileObject) fs.resolveFile(urlString.substring(//
                0,
                urlString.indexOf(ResourceUtils.JAR_URL_SEPARATOR) + ResourceUtils.JAR_URL_SEPARATOR.length()));
        this.jarFile = root.jarFile;
    }
    this.entry = jarFile.getJarEntry(entryName);
    this.url = url;
    this.urlString = urlString;
    int indexSep = entryName.lastIndexOf('/');
    if (indexSep == -1) {
        this.fileName = new FileNameImpl(this, entryName);
    } else {
        if (entryName.endsWith("/")) {
            int index = entryName.lastIndexOf('/', entryName.length() - 2);
            this.fileName = new FileNameImpl(this, entryName.substring(index + 1, indexSep));
        } else {
            this.fileName = new FileNameImpl(this, entryName.substring(indexSep + 1));
        }
    }
}