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.apache.hadoop.streaming.StreamUtil.java

static void unJar(File jarFile, File toDir) throws IOException {
    JarFile jar = new JarFile(jarFile);
    try {/*  w w  w. j  a v a2  s .  com*/
        Enumeration entries = jar.entries();
        while (entries.hasMoreElements()) {
            JarEntry entry = (JarEntry) entries.nextElement();
            if (!entry.isDirectory()) {
                InputStream in = jar.getInputStream(entry);
                try {
                    File file = new File(toDir, entry.getName());
                    file.getParentFile().mkdirs();
                    OutputStream out = new FileOutputStream(file);
                    try {
                        byte[] buffer = new byte[8192];
                        int i;
                        while ((i = in.read(buffer)) != -1) {
                            out.write(buffer, 0, i);
                        }
                    } finally {
                        out.close();
                    }
                } finally {
                    in.close();
                }
            }
        }
    } finally {
        jar.close();
    }
}

From source file:com.photon.maven.plugins.android.common.JarHelper.java

/** Unjars the specified jar file into the the specified directory
 *
 * @param jarFile//from   ww w  . j a  v a  2  s . c  o  m
 * @param outputDirectory
 * @param unjarListener
 * @throws IOException
 */
public static void unjar(JarFile jarFile, File outputDirectory, UnjarListener unjarListener)
        throws IOException {
    for (Enumeration en = jarFile.entries(); en.hasMoreElements();) {
        JarEntry entry = (JarEntry) en.nextElement();
        File entryFile = new File(outputDirectory, entry.getName());
        if (unjarListener.include(entry)) {
            if (!entryFile.getParentFile().exists() && !entryFile.getParentFile().mkdirs()) {
                throw new IOException("Error creating output directory: " + entryFile.getParentFile());
            }

            // If the entry is an actual file, unzip that too
            if (!entry.isDirectory()) {
                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:net.technicpack.launchercore.util.ZipUtils.java

public static void copyMinecraftJar(File minecraft, File output) throws IOException {
    String[] security = { "MOJANG_C.DSA", "MOJANG_C.SF", "CODESIGN.RSA", "CODESIGN.SF" };
    JarFile jarFile = new JarFile(minecraft);
    try {//from   ww  w  .  j a  va 2s.  c o  m
        String fileName = jarFile.getName();
        String fileNameLastPart = fileName.substring(fileName.lastIndexOf(File.separator));

        JarOutputStream jos = new JarOutputStream(new FileOutputStream(output));
        Enumeration<JarEntry> entries = jarFile.entries();

        while (entries.hasMoreElements()) {
            JarEntry entry = entries.nextElement();
            if (containsAny(entry.getName(), security)) {
                continue;
            }
            InputStream is = jarFile.getInputStream(entry);

            //jos.putNextEntry(entry);
            //create a new entry to avoid ZipException: invalid entry compressed size
            jos.putNextEntry(new JarEntry(entry.getName()));
            byte[] buffer = new byte[4096];
            int bytesRead = 0;
            while ((bytesRead = is.read(buffer)) != -1) {
                jos.write(buffer, 0, bytesRead);
            }
            is.close();
            jos.flush();
            jos.closeEntry();
        }
        jos.close();
    } finally {
        jarFile.close();
    }

}

From source file:org.ebayopensource.turmeric.eclipse.utils.wsdl.WSDLUtil.java

/**
 * Reading wsdl from the provided jar file.
 * The format for documetn base URI would be "jar:file:[JAR_FILE_LOCATION]!/[WSDL_JAR_ENTRY_PATH]"
 *
 * @param file the file/*from  ww w . j  av a 2 s . c  o m*/
 * @param jarEntryLocation the jar entry location
 * @return The instance of WSDL definition, or null is could not read it.
 * @throws WSDLException the wSDL exception
 * @throws IOException Signals that an I/O exception has occurred.
 */
public static Definition readWSDLFromJarFile(final File file, final String jarEntryLocation)
        throws WSDLException, IOException {
    InputStream wsdlStream = null;
    if (file.exists() && file.canRead()) {
        final JarFile jarFile = new JarFile(file);
        final JarEntry jarEntry = jarFile.getJarEntry(jarEntryLocation);
        if (jarEntry != null) {
            // found the wsdl file
            wsdlStream = jarFile.getInputStream(jarEntry);
            return WSDLUtil.readWSDL(StringUtil.toString(URL_PREFIX_JAR_FILE, file.getAbsolutePath(),
                    JAR_FILE_SEPARATOR, jarEntryLocation), wsdlStream);
        }
    }
    return null;
}

From source file:org.wso2.carbon.automation.test.utils.common.FileManager.java

public static void copyJarFile(File sourceFile, String destinationDirectory) throws IOException {
    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 = null;
    try {/*from www  . ja v  a 2s .  co m*/
        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);
            }
            inputStream.close();
            jarOutputStream.flush();
            jarOutputStream.closeEntry();
        }
    } finally {
        if (jarOutputStream != null) {
            try {
                jarOutputStream.close();
            } catch (IOException e) {
            }
        }
    }
}

From source file:eu.sisob.uma.footils.File.FileFootils.java

public static boolean copyJarResourcesRecursively(final File destDir, final JarURLConnection jarConnection)
        throws IOException {

    final JarFile jarFile = jarConnection.getJarFile();

    for (final Enumeration<JarEntry> e = jarFile.entries(); e.hasMoreElements();) {
        final JarEntry entry = e.nextElement();
        if (entry.getName().startsWith(jarConnection.getEntryName())) {
            final String filename = StringUtils.removeStart(entry.getName(), //
                    jarConnection.getEntryName());

            final File f = new File(destDir, filename);
            if (!entry.isDirectory()) {
                final InputStream entryInputStream = jarFile.getInputStream(entry);
                if (!FileFootils.copyStream(entryInputStream, f)) {
                    return false;
                }// w  ww .  j  ava 2  s .c o  m
                entryInputStream.close();
            } else {
                if (!FileFootils.ensureDirectoryExists(f)) {
                    throw new IOException("Could not create directory: " + f.getAbsolutePath());
                }
            }
        }
    }
    return true;
}

From source file:com.zb.jcseg.core.JcsegTaskConfig.java

public static void unJar(JarFile jar, File toDir) throws IOException {
    try {/*  w  w  w  . j av  a  2 s  .  c om*/
        Enumeration<JarEntry> entries = jar.entries();
        while (entries.hasMoreElements()) {
            JarEntry entry = (JarEntry) entries.nextElement();
            if (!entry.isDirectory()) {
                InputStream in = jar.getInputStream(entry);
                try {
                    File file = new File(toDir, entry.getName());
                    if (!file.getParentFile().mkdirs()) {
                        if (!file.getParentFile().isDirectory()) {
                            throw new IOException("Mkdirs failed to create " + file.getParentFile().toString());
                        }
                    }
                    OutputStream out = new FileOutputStream(file);
                    try {
                        byte[] buffer = new byte[8192];
                        int i;
                        while ((i = in.read(buffer)) != -1) {
                            out.write(buffer, 0, i);
                        }
                    } finally {
                        out.close();
                    }
                } finally {
                    in.close();
                }
            }
        }
    } finally {
        jar.close();
    }
}

From source file:org.spout.api.plugin.PluginLoader.java

/**
 * @param file Plugin file object// www.j av  a 2s . c o  m
 * @return The current plugin's description element.
 */
protected static synchronized PluginDescriptionFile getDescription(File file)
        throws InvalidPluginException, InvalidDescriptionFileException {
    if (!file.exists()) {
        throw new InvalidPluginException(file.getName() + " does not exist!");
    }

    PluginDescriptionFile description = null;
    JarFile jar = null;
    InputStream in = null;
    try {
        // Spout plugin properties file
        jar = new JarFile(file);
        JarEntry entry = jar.getJarEntry(YAML_SPOUT);

        // Fallback plugin properties file
        if (entry == null) {
            entry = jar.getJarEntry(YAML_OTHER);
        }

        if (entry == null) {
            throw new InvalidPluginException("Jar has no properties.yml or plugin.yml!");
        }

        in = jar.getInputStream(entry);
        description = new PluginDescriptionFile(in);
    } catch (IOException e) {
        throw new InvalidPluginException(e);
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
                Spout.getLogger().log(Level.WARNING, "Problem closing input stream", e);
            }
        }
        if (jar != null) {
            try {
                jar.close();
            } catch (IOException e) {
                Spout.getLogger().log(Level.WARNING, "Problem closing jar input stream", e);
            }
        }
    }
    return description;
}

From source file:com.catalyst.sonar.score.batch.util.FileInstaller.java

/**
 * Copies a directory from a {@link JarURLConnection} to a destination
 * Directory outside the jar./*w w w.ja  va2  s  . co  m*/
 * 
 * @param destDir
 * @param jarConnection
 * @return true if copy is successful, false otherwise.
 * @throws IOException
 */
public static boolean copyJarResourcesRecursively(final File destDir, final JarURLConnection jarConnection)
        throws IOException {
    logger.debug("copyJarResourcesRecursively()");
    boolean success = true;
    final JarFile jarFile = jarConnection.getJarFile();

    for (final Enumeration<JarEntry> e = jarFile.entries(); e.hasMoreElements();) {
        final JarEntry entry = e.nextElement();
        if (entry.getName().startsWith(jarConnection.getEntryName())) {
            final String filename = StringUtils.removeStart(entry.getName(), //
                    jarConnection.getEntryName());

            final File f = new File(destDir, filename);
            if (!entry.isDirectory()) {
                final InputStream entryInputStream = jarFile.getInputStream(entry);
                if (!FileInstaller.copyStream(entryInputStream, f)) {
                    success = false;
                    logger.debug("returning " + success);
                    return success;
                }
                entryInputStream.close();
            } else {
                if (!FileInstaller.ensureDirectoryExists(f)) {
                    logger.debug("throwing an IOException");
                    throw new IOException("Could not create directory: " + f.getAbsolutePath());
                }
            }
        }
    }
    logger.debug("returning " + success);
    return success;
}

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

public static void copyJarFile(File sourceFile, String destinationDirectory) throws IOException {
    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 = null;
    try {// www.ja  va  2  s.c o  m
        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);
            }
            inputStream.close();
            jarOutputStream.flush();
            jarOutputStream.closeEntry();
        }
    } finally {
        if (jarOutputStream != null) {
            try {
                jarOutputStream.close();
            } catch (IOException e) {

            }
        }
    }

}