Example usage for java.util.zip ZipFile getInputStream

List of usage examples for java.util.zip ZipFile getInputStream

Introduction

In this page you can find the example usage for java.util.zip ZipFile getInputStream.

Prototype

public InputStream getInputStream(ZipEntry entry) throws IOException 

Source Link

Document

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

Usage

From source file:com.github.swt_release_fetcher.Artifact.java

private void extractFromZip(ZipFile zipFile, String fileNameToExtract, File targetFile) throws IOException {
    ZipEntry entry = zipFile.getEntry(fileNameToExtract);
    InputStream inputStream = null;
    OutputStream outputStream = null;
    try {/*from  w  ww. ja va2  s . c  o  m*/
        inputStream = zipFile.getInputStream(entry);
        outputStream = new FileOutputStream(targetFile);
        IOUtils.copy(inputStream, outputStream);
    } finally {
        IOUtils.closeQuietly(inputStream);
        IOUtils.closeQuietly(outputStream);
    }
}

From source file:com.koushikdutta.superuser.MainActivity.java

File extractSu() throws IOException, InterruptedException {
    ZipFile zf = new ZipFile(getPackageCodePath());
    ZipEntry su = zf.getEntry("assets/" + getArch() + "/su");
    InputStream zin = zf.getInputStream(su);
    File ret = getFileStreamPath("su");
    FileOutputStream fout = new FileOutputStream(ret);
    StreamUtility.copyStream(zin, fout);
    zin.close();/*from   ww w  .j  a v  a  2  s. c om*/
    zf.close();
    fout.close();
    return ret;
}

From source file:org.cloudifysource.shell.commands.TestRecipe.java

/**
 * Unzips a given file.//from  w w  w  .j av a  2 s. co  m
 *
 * @param inputFile
 *            The zip file to extract
 * @return The new folder, containing the extracted content of the zip file
 * @throws IOException
 *             Reporting a failure to extract the zipped file or close it afterwards
 */
private static File unzipFile(final File inputFile) throws IOException {

    ZipFile zipFile = null;
    try {
        final File baseDir = TestRecipe.createTempDir();
        zipFile = new ZipFile(inputFile);
        final Enumeration<? extends ZipEntry> entries = zipFile.entries();

        while (entries.hasMoreElements()) {
            final ZipEntry entry = entries.nextElement();

            if (entry.isDirectory()) {

                logger.fine("Extracting directory: " + entry.getName());
                final File dir = new File(baseDir, entry.getName());
                dir.mkdir();
                continue;
            }

            logger.finer("Extracting file: " + entry.getName());
            final File file = new File(baseDir, entry.getName());
            file.getParentFile().mkdirs();
            ServiceReader.copyInputStream(zipFile.getInputStream(entry),
                    new BufferedOutputStream(new FileOutputStream(file)));
        }
        return baseDir;

    } finally {
        if (zipFile != null) {
            try {
                zipFile.close();
            } catch (final IOException e) {
                logger.log(Level.SEVERE, "Failed to close zip file after unzipping zip contents", e);
            }
        }
    }

}

From source file:com.android.tradefed.util.FileUtil.java

/**
 * Utility method to extract entire contents of zip file into given
 * directory/*  w  w w. j  av  a  2  s.com*/
 *
 * @param zipFile
 *            the {@link ZipFile} to extract
 * @param destDir
 *            the local dir to extract file to
 * @throws IOException
 *             if failed to extract file
 */
public static void extractZip(ZipFile zipFile, File destDir) throws IOException {
    Enumeration<? extends ZipEntry> entries = zipFile.entries();
    while (entries.hasMoreElements()) {

        ZipEntry entry = entries.nextElement();
        File childFile = new File(destDir, entry.getName());
        childFile.getParentFile().mkdirs();
        if (entry.isDirectory()) {
            continue;
        } else {
            FileUtil.writeToFile(zipFile.getInputStream(entry), childFile);
        }
    }
}

From source file:jp.co.cyberagent.jenkins.plugins.DeployStrategyAndroid.java

private String getStringFromManifest(final String name) {
    File tempApk = null;//www .ja v  a  2s  .  c  om
    InputStream is = null;
    ZipFile zip = null;
    try {
        tempApk = File.createTempFile(getBuild().getId(), "nr-" + getBuild().getNumber());
        mApkFile.copyTo(new FileOutputStream(tempApk));
        zip = new ZipFile(tempApk);
        ZipEntry mft = zip.getEntry("AndroidManifest.xml");
        is = zip.getInputStream(mft);

        byte[] xml = new byte[is.available()];
        is.read(xml);

        String string = AndroidUtils.decompressXML(xml);
        int start = string.indexOf(name + "=\"") + name.length() + 2;
        int end = string.indexOf("\"", start);
        String version = string.substring(start, end);

        if (version.startsWith("resourceID 0x")) {
            int resId = Integer.parseInt(version.substring(13), 16);
            return getStringFromResource(tempApk, resId);
        } else {
            return version;
        }
    } catch (Exception e) {
        getLogger().println(TAG + "Error: " + e.getMessage());
    } finally {
        if (tempApk != null)
            tempApk.delete();
        if (zip != null)
            try {
                zip.close();
            } catch (IOException e) {
                getLogger().println(TAG + "Error: " + e.getMessage());
            }
        if (is != null)
            try {
                is.close();
            } catch (IOException e) {
                getLogger().println(TAG + "Error: " + e.getMessage());
            }
    }
    return null;
}

From source file:com.gettingagile.tisugly.analyzer.ASMAnalyzer.java

private void loadClassesFromClasspathArchiveFile(File classpathElement) throws IOException {
    ZipFile f = new ZipFile(classpathElement.getAbsolutePath());
    Enumeration<? extends ZipEntry> en = f.entries();
    while (en.hasMoreElements()) {
        ZipEntry e = en.nextElement();
        String name = e.getName();
        if (name.endsWith(".class")) {
            new ClassReader(f.getInputStream(e)).accept(dependencyVisitor, 0);
        }/*from  w  w w. j  av  a  2  s.  c  o m*/
    }
}

From source file:edu.harvard.i2b2.PreProcessFhirSpec.java

public PreProcessFhirSpec() throws IOException, XQueryUtilException {
    logger.trace("hi");
    ArrayList<String> resourceList = new ArrayList<String>();
    resourceList.add("Patient".toLowerCase());
    resourceList.add("Medication".toLowerCase());
    resourceList.add("MedicationStatement".toLowerCase());
    resourceList.add("Observation".toLowerCase());
    ZipFile zipFile = new ZipFile(Utils.getFilePath("fhir-spec.zip"));// fhir-all-xsd.zip
    logger.trace("" + zipFile.getName());

    Enumeration<? extends ZipEntry> entries = zipFile.entries();

    for (String rName : resourceList) {
        String fName = "site/" + rName + ".profile.xml";
        logger.trace("" + zipFile.getEntry(fName));
        String fContent = IOUtils.toString(zipFile.getInputStream(zipFile.getEntry(fName)));
        logger.trace("" + XQueryUtil.processXQuery(
                "declare default element namespace \"http://hl7.org/fhir\";//searchParam", fContent));

    }//  w w w.java 2 s.  c  om

    /*while (entries.hasMoreElements()) {
       ZipEntry entry = entries.nextElement();
       if(!entry.getName().matches(".*\\.html$")) continue;
       Pattern p = Pattern.compile("^site/([^\\.\\\\]*)\\..*$");
       Matcher m = p.matcher(entry.getName());
       String rname="";
       if (m.matches()) {
    //logger.trace("" + entry.getName() + "->" + m.group(1));
    rname=m.group(1).toLowerCase();
       }
               
       if (resourceList.contains(rname))
    logger.trace(entry.getName()+"->" + rname);
       // InputStream stream = zipFile.getInputStream(entry);
               
    }*/

}

From source file:net.sf.sripathi.ws.mock.util.FileUtil.java

public static void createFilesAndFolder(String root, ZipFile zip) {

    @SuppressWarnings("unchecked")
    Enumeration<ZipEntry> entries = (Enumeration<ZipEntry>) zip.entries();
    Set<String> files = new TreeSet<String>();
    while (entries.hasMoreElements()) {
        ZipEntry entry = entries.nextElement();

        if (entry.getName().toLowerCase().endsWith(".wsdl") || entry.getName().toLowerCase().endsWith(".xsd"))
            files.add(entry.getName());/*from   www  .j ava 2s.c om*/
    }

    File rootFolder = new File(root);
    if (!rootFolder.exists()) {
        throw new MockException("Unable to create file - " + root);
    }

    for (String fileStr : files) {

        String folder = root;

        String[] split = fileStr.split("/");

        if (split.length > 1) {

            for (int i = 0; i < split.length - 1; i++) {

                folder = folder + "/" + split[i];
                File f = new File(folder);
                if (!f.exists()) {
                    f.mkdir();
                }
            }
        }

        File file = new File(folder + "/" + split[split.length - 1]);
        FileOutputStream fos = null;
        InputStream zipStream = null;
        try {
            fos = new FileOutputStream(file);
            zipStream = zip.getInputStream(zip.getEntry(fileStr));
            fos.write(IOUtils.toByteArray(zipStream));
        } catch (Exception e) {
            throw new MockException("Unable to create file - " + fileStr);
        } finally {
            try {
                fos.close();
            } catch (Exception e) {
            }
            try {
                zipStream.close();
            } catch (Exception e) {
            }
        }
    }
}

From source file:com.alibaba.jstorm.yarn.utils.JStormUtils.java

/**
 * Extra dir from the jar to destdir//w w w.j a  va2  s . c o m
 *
 * @param jarpath
 * @param dir
 * @param destdir
 */
public static void extractDirFromJar(String jarpath, String dir, String destdir) {
    ZipFile zipFile = null;
    try {
        zipFile = new ZipFile(jarpath);
        Enumeration<? extends ZipEntry> entries = zipFile.entries();
        while (entries != null && entries.hasMoreElements()) {
            ZipEntry ze = entries.nextElement();
            if (!ze.isDirectory() && ze.getName().startsWith(dir)) {
                InputStream in = zipFile.getInputStream(ze);
                try {
                    File file = new File(destdir, ze.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 {
                    if (in != null)
                        in.close();
                }
            }
        }
    } catch (Exception e) {
        LOG.warn("No " + dir + " from " + jarpath + "!\n" + e.getMessage());
    } finally {
        if (zipFile != null)
            try {
                zipFile.close();
            } catch (Exception e) {
                LOG.warn(e.getMessage());
            }

    }
}

From source file:org.eclipse.mylyn.internal.context.core.InteractionContextExternalizer.java

public InputStream getAdditionalInformation(File file, String contributorIdentifier) throws IOException {
    if (!file.exists()) {
        return null;
    }//w  w w.  j  a  va  2s . com
    final ZipFile zipFile = new ZipFile(file);
    ZipEntry entry = findFileInZip(zipFile, contributorIdentifier);
    if (entry == null) {
        return null;
    }

    return new FilterInputStream(zipFile.getInputStream(entry)) {
        @Override
        public void close() throws IOException {
            super.close();
            zipFile.close();
        }
    };
}