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:com.baomidou.framework.common.JarHelper.java

public static List<String> readLines(JarFile jarFile, String fileName) throws IOException {
    if (jarFile == null || StringUtils.isEmpty(fileName)) {
        return null;
    }/*from  ww w  .java2  s. co m*/
    List<String> lines = new ArrayList<String>();
    JarEntry entry = jarFile.getJarEntry(fileName);
    InputStream inputStream = jarFile.getInputStream(entry);
    InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
    BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
    String line;
    while ((line = bufferedReader.readLine()) != null) {
        lines.add(line);
    }
    bufferedReader.close();
    inputStreamReader.close();
    return lines;
}

From source file:com.zb.app.common.file.FileUtils.java

public static void unJar(File jarFile, File toDir) throws IOException {
    JarFile jar = new JarFile(jarFile);
    try {/*from  w w w .j a  v  a  2  s . c  o m*/
        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:Main.java

/**
 * Extracts certificate from APK/*from   www  . j  av  a2  s  .co  m*/
 * 
 * @param jar
 * @param entry
 *            rsa or dsa jar item
 * @return
 * @throws IOException
 *             I/O problem to read jar
 * @throws CertificateException
 *             certificate has problems
 */
private static List<Certificate> extractCertificate(JarFile jar, JarEntry entry)
        throws IOException, CertificateException {
    List<Certificate> certList = new ArrayList<Certificate>();
    InputStream inStream = null;
    try {
        CertificateFactory cf = CertificateFactory.getInstance("X.509");
        inStream = jar.getInputStream(entry);
        Collection<? extends Certificate> c = cf.generateCertificates(inStream);
        Iterator<? extends Certificate> i = c.iterator();
        while (i.hasNext()) {
            Certificate cert = i.next();
            certList.add(cert);
        }
    } finally {
        if (inStream != null) {
            inStream.close();
        }
    }
    return certList;
}

From source file:org.jboss.tools.central.internal.ImageUtil.java

/**
 * Creates an image from an {@link URL}.
 * If the iconUrl points at a jar file, the created image doesn't not leak file handle. 
 *//* w  w w .j ava2s  .c o  m*/
public static Image createImageFromUrl(Device device, URL iconUrl) {

    if (!iconUrl.getProtocol().equals("jar")) {
        ImageDescriptor descriptor = ImageDescriptor.createFromURL(iconUrl);
        return descriptor.createImage();
    }

    //Load from jar:
    Image image = null;
    try {
        String fileName = iconUrl.getFile();
        if (fileName.contains("!")) {
            String[] location = fileName.split("!");
            fileName = location[0];
            String imageName = URLDecoder.decode(location[1].substring(1), "utf-8");
            File file = new File(new URI(fileName));
            JarFile jarFile = null;
            InputStream inputStream = null;
            try {
                jarFile = new JarFile(file);
                ZipEntry imageEntry = jarFile.getEntry(imageName);
                if (imageEntry != null) {
                    inputStream = jarFile.getInputStream(imageEntry);
                    image = new Image(device, inputStream);
                }
            } finally {
                IOUtils.closeQuietly(inputStream);
                try {
                    if (jarFile != null) {
                        jarFile.close();
                    }
                } catch (Exception e) {
                    //ignore
                }
            }
        }
    } catch (Exception e) {
        JBossCentralActivator.log(e);
    }

    return image;
}

From source file:org.clickframes.util.JarResourceUtil.java

private static void copyFile(JarFile jarFile, File temporaryFolder, JarEntry je, final String fileInJar)
        throws IOException, FileNotFoundException {
    if (!je.isDirectory()) {
        File tempFile = new File(temporaryFolder, fileInJar);
        tempFile.getParentFile().mkdirs();
        tempFile.deleteOnExit();// w  w  w.j a  v  a2  s.  co  m
        IOUtils.copy(jarFile.getInputStream(je), new FileOutputStream(tempFile));
    }
}

From source file:org.ebayopensource.turmeric.eclipse.utils.io.IOUtil.java

/**
 * Loads the properties file residing inside a jar. The enclosed jar file is
 * queried for the given jar entry path and is parsed into a properties
 * file. Most of the cases this is being used to load some SOA properties
 * file from a jar project which is not imported to the workspace but is
 * stored somewhere in the file system. But any client who wants to load a
 * properties file can use this. There is nothing SOA specific here in this
 * API.//from w w w .j a v  a2 s.co  m
 *
 * @param enclosedJarFile the enclosed jar file
 * @param jarEntryPath the jar entry path
 * @return the properties
 * @throws IOException This is a special case where we want the consumers to
 * continue without failures, because most of the cases consumes
 * this in a loop.
 */
public static Properties loadProperties(JarFile enclosedJarFile, String jarEntryPath) throws IOException {
    JarEntry jarEntry = enclosedJarFile.getJarEntry(jarEntryPath);
    InputStream inputStream = enclosedJarFile.getInputStream(jarEntry);
    Properties properties = new Properties();
    properties.load(inputStream);
    return properties;

}

From source file:org.nuxeo.osgi.JarBundleFile.java

public static void extractNestedJar(JarFile file, ZipEntry entry, File dest) throws IOException {
    InputStream in = null;//from ww  w.j a va2 s . c o  m
    try {
        in = file.getInputStream(entry);
        FileUtils.copyToFile(in, dest);
    } finally {
        if (in != null) {
            in.close();
        }
    }
}

From source file:org.apache.hadoop.hbase.util.RunTrigger.java

public static void unJar(File jarFile, File toDir) throws IOException {
    JarFile jar = new JarFile(jarFile);
    try {//ww w .ja v a  2  s .c om
        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());
                    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.nuxeo.osgi.JarBundleFile.java

public static void extractNestedJar(JarFile file, String path, File dest) throws IOException {
    InputStream in = null;/*  w w w .jav  a 2  s  .  c  o m*/
    ZipEntry entry = file.getEntry(path);
    try {
        in = file.getInputStream(entry);
        FileUtils.copyToFile(in, dest);
    } finally {
        if (in != null) {
            in.close();
        }
    }
}

From source file:com.asual.summer.onejar.OneJarServer.java

private static String getCurrentWarFile() throws IOException {
    JarFile jarFile = new JarFile(System.getProperty("java.class.path"));
    Enumeration<JarEntry> entries = jarFile.entries();
    while (entries.hasMoreElements()) {
        String name = entries.nextElement().getName();
        if (name.endsWith(".war")) {
            File war = new File(new File(System.getProperty("java.io.tmpdir")),
                    "summer-onejar-" + System.currentTimeMillis() + ".war");
            InputStream input = jarFile.getInputStream(new ZipEntry(name));
            FileOutputStream output = new FileOutputStream(war);
            IOUtils.copy(input, output);
            IOUtils.closeQuietly(input);
            IOUtils.closeQuietly(output);
            war.deleteOnExit();//  w  w w .ja va2 s .  c  o  m
            return war.getAbsolutePath();
        }
    }
    return null;
}