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, boolean verify, int mode) throws IOException 

Source Link

Document

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

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    JarFile jarfile = new JarFile(new File("filename.jar"), true, JarFile.OPEN_READ);

    JarEntry entry = jarfile.getJarEntry("fileName");

}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    JarFile jarfile = new JarFile(new File("filename.jar"), true, JarFile.OPEN_READ);

    Manifest manifest = jarfile.getManifest();

    Attributes attrs = (Attributes) manifest.getMainAttributes();

    for (Iterator it = attrs.keySet().iterator(); it.hasNext();) {
        Attributes.Name attrName = (Attributes.Name) it.next();

        String attrValue = attrs.getValue(attrName);
    }/*from  w  w  w  . j a  v  a  2s  . c o  m*/
}

From source file:JarEntryOutputStream.java

/**
 * @see java.util.jar.JarFile#JarFile(java.io.File, boolean, int)
 *//*from   w w  w .  j a  va 2 s.  c o  m*/
public EnhancedJarFile(File file, boolean verify, int mode) throws IOException {

    this.jar = new JarFile(file, verify, mode);
}

From source file:JarUtil.java

/**
 * Extracts the given jar-file to the specified directory. The target
 * directory will be cleaned before the jar-file will be extracted.
 * /*from  w  w  w.j a va  2s. c o m*/
 * @param jarFile
 *            The jar file which should be unpacked
 * @param targetDir
 *            The directory to which the jar-content should be extracted.
 * @throws FileNotFoundException
 *             when the jarFile does not exist
 * @throws IOException
 *             when a file could not be written or the jar-file could not
 *             read.
 */
public static void unjar(File jarFile, File targetDir) throws FileNotFoundException, IOException {
    // clear target directory:
    if (targetDir.exists()) {
        targetDir.delete();
    }
    // create new target directory:
    targetDir.mkdirs();
    // read jar-file:
    String targetPath = targetDir.getAbsolutePath() + File.separatorChar;
    byte[] buffer = new byte[1024 * 1024];
    JarFile input = new JarFile(jarFile, false, ZipFile.OPEN_READ);
    Enumeration<JarEntry> enumeration = input.entries();
    for (; enumeration.hasMoreElements();) {
        JarEntry entry = enumeration.nextElement();
        if (!entry.isDirectory()) {
            // do not copy anything from the package cache:
            if (entry.getName().indexOf("package cache") == -1) {
                String path = targetPath + entry.getName();
                File file = new File(path);
                if (!file.getParentFile().exists()) {
                    file.getParentFile().mkdirs();
                }
                FileOutputStream out = new FileOutputStream(file);
                InputStream in = input.getInputStream(entry);
                int read;
                while ((read = in.read(buffer)) != -1) {
                    out.write(buffer, 0, read);
                }
                in.close();
                out.close();
            }
        }
    }

}

From source file:JarUtil.java

/**
 * Extracts the given resource from a jar-file to the specified directory.
 * /*from  w w w  .  ja v a  2s  .  c  o m*/
 * @param jarFile
 *            The jar file which should be unpacked
 * @param resource
 *            The name of a resource in the jar
 * @param targetDir
 *            The directory to which the jar-content should be extracted.
 * @throws FileNotFoundException
 *             when the jarFile does not exist
 * @throws IOException
 *             when a file could not be written or the jar-file could not
 *             read.
 */
public static void unjar(File jarFile, String resource, File targetDir)
        throws FileNotFoundException, IOException {
    // clear target directory:
    if (targetDir.exists()) {
        targetDir.delete();
    }
    // create new target directory:
    targetDir.mkdirs();
    // read jar-file:
    String targetPath = targetDir.getAbsolutePath() + File.separatorChar;
    byte[] buffer = new byte[1024 * 1024];
    JarFile input = new JarFile(jarFile, false, ZipFile.OPEN_READ);
    Enumeration<JarEntry> enumeration = input.entries();
    for (; enumeration.hasMoreElements();) {
        JarEntry entry = enumeration.nextElement();
        if (!entry.isDirectory()) {
            // do not copy anything from the package cache:
            if (entry.getName().equals(resource)) {
                String path = targetPath + entry.getName();
                File file = new File(path);
                if (!file.getParentFile().exists()) {
                    file.getParentFile().mkdirs();
                }
                FileOutputStream out = new FileOutputStream(file);
                InputStream in = input.getInputStream(entry);
                int read;
                while ((read = in.read(buffer)) != -1) {
                    out.write(buffer, 0, read);
                }
                in.close();
                out.close();
            }
        }
    }
}

From source file:JarUtil.java

/**
 * Reads the package-names from the given jar-file.
 * // ww  w .  j  a  v  a2  s .  c o  m
 * @param jarFile
 *            the jar file
 * @return an array with all found package-names
 * @throws IOException
 *             when the jar-file could not be read
 */
public static String[] getPackageNames(File jarFile) throws IOException {
    HashMap<String, String> packageNames = new HashMap<String, String>();
    JarFile input = new JarFile(jarFile, false, ZipFile.OPEN_READ);
    Enumeration<JarEntry> enumeration = input.entries();
    for (; enumeration.hasMoreElements();) {
        JarEntry entry = enumeration.nextElement();
        String name = entry.getName();
        if (name.endsWith(".class")) {
            int endPos = name.lastIndexOf('/');
            boolean isWindows = false;
            if (endPos == -1) {
                endPos = name.lastIndexOf('\\');
                isWindows = true;
            }
            name = name.substring(0, endPos);
            name = name.replace('/', '.');
            if (isWindows) {
                name = name.replace('\\', '.');
            }
            packageNames.put(name, name);
        }
    }
    return (String[]) packageNames.values().toArray(new String[packageNames.size()]);
}