Example usage for java.util.zip ZipFile close

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

Introduction

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

Prototype

public void close() throws IOException 

Source Link

Document

Closes the ZIP file.

Usage

From source file:ren.hankai.cordwood.mobile.MobileAppScanner.java

/**
 * ?? Android .apk ?/*from  www .  java  2 s.  c  o m*/
 *
 * @param filePath apk 
 * @return ?
 * @author hankai
 * @since May 20, 2016 3:46:18 PM
 */
public static MobileAppInfo scanAndroidApk(String filePath) {
    final MobileAppInfo appInfo = new MobileAppInfo();
    ApkParser parser = null;
    ZipFile zipFile = null;
    InputStream inputStream = null;
    try {
        parser = new ApkParser(filePath);
        final ApkMeta meta = parser.getApkMeta();
        appInfo.setName(meta.getName());
        appInfo.setVersion(meta.getVersionName() + "#" + meta.getVersionCode());
        appInfo.setBundleId(meta.getPackageName());
        final String iconPath = meta.getIcon();
        appInfo.setIconName(iconPath);
        if (!StringUtils.isEmpty(iconPath)) {
            zipFile = new ZipFile(filePath, Charset.forName("UTF-8"));
            final Enumeration<? extends ZipEntry> entries = zipFile.entries();
            while (entries.hasMoreElements()) {
                final ZipEntry entry = entries.nextElement();
                if (entry.getName().contains(iconPath)) {
                    inputStream = zipFile.getInputStream(entry);
                    appInfo.setIcon(IOUtils.toByteArray(inputStream));
                    break;
                }
            }
        }
    } catch (final Exception ex) {
        logger.error(String.format("Failed to parse apk file at %s", filePath));
    } finally {
        try {
            if (parser != null) {
                parser.close();
            }
            if (zipFile != null) {
                zipFile.close();
            }
            if (inputStream != null) {
                inputStream.close();
            }
        } catch (final Exception ex) {
            logger.trace("Failed to close stream.", ex);
        }
    }
    return appInfo;
}

From source file:net.rptools.lib.FileUtil.java

public static void unzipFile(File sourceFile, File destDir) throws IOException {
    if (!sourceFile.exists())
        throw new IOException("source file does not exist: " + sourceFile);

    ZipFile zipFile = null;
    InputStream is = null;/* w w  w . ja  va 2  s . c  o m*/
    OutputStream os = null;
    try {
        zipFile = new ZipFile(sourceFile);
        Enumeration<? extends ZipEntry> entries = zipFile.entries();

        while (entries.hasMoreElements()) {
            ZipEntry entry = entries.nextElement();
            if (entry.isDirectory())
                continue;

            File file = new File(destDir, entry.getName());
            String path = file.getAbsolutePath();
            file.getParentFile().mkdirs();

            //System.out.println("Writing file: " + path);
            is = zipFile.getInputStream(entry);
            os = new BufferedOutputStream(new FileOutputStream(path));
            copyWithClose(is, os);
            IOUtils.closeQuietly(is);
            IOUtils.closeQuietly(os);
        }
    } finally {
        IOUtils.closeQuietly(is);
        IOUtils.closeQuietly(os);
        try {
            if (zipFile != null)
                zipFile.close();
        } catch (Exception e) {
        }
    }
}

From source file:com.moss.nomad.core.runner.Runner.java

private MigrationContainer extractContainer(File f) throws Exception {

    ZipFile file = new ZipFile(f);
    ZipEntry entry = new ZipEntry("META-INF/container.xml");
    InputStream in = file.getInputStream(entry);
    Unmarshaller u = context.createUnmarshaller();
    MigrationContainer container = (MigrationContainer) u.unmarshal(in);
    in.close();//from   w w  w.j  ava  2s.c o m
    file.close();

    return container;
}

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;
    }/*from ww w.  j a v a  2  s  . c o  m*/
    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();
        }
    };
}

From source file:org.formic.util.Extractor.java

/**
 * Extracts the specified archive to the supplied destination.
 *
 * @param archive The archive to extract.
 * @param dest The directory to extract it to.
 *///from   www.  j  a va  2s. c o  m
public static void extractArchive(File archive, File dest, ArchiveExtractionListener listener)
        throws IOException {
    ZipFile file = null;
    try {
        file = new ZipFile(archive);

        if (listener != null) {
            listener.startExtraction(file.size());
        }

        Enumeration entries = file.entries();
        for (int ii = 0; entries.hasMoreElements(); ii++) {
            ZipEntry entry = (ZipEntry) entries.nextElement();
            if (!entry.isDirectory()) {
                // create parent directories if necessary.
                String name = dest + "/" + entry.getName();
                if (name.indexOf('/') != -1) {
                    File dir = new File(name.substring(0, name.lastIndexOf('/')));
                    if (!dir.exists()) {
                        dir.mkdirs();
                    }
                }

                if (listener != null) {
                    listener.startExtractingFile(ii, entry.getName());
                }

                FileOutputStream out = new FileOutputStream(name);
                InputStream in = file.getInputStream(entry);

                IOUtils.copy(in, out);

                in.close();
                out.close();

                if (listener != null) {
                    listener.finishExtractingFile(ii, entry.getName());
                }
            }
        }
    } finally {
        try {
            file.close();
        } catch (Exception ignore) {
        }
    }

    if (listener != null) {
        listener.finishExtraction();
    }
}

From source file:org.python.pydev.core.REF.java

/**
 * @param f the zip file that should be opened
 * @param pathInZip the path within the zip file that should be gotten
 * @param returnType the class that specifies the return type of this method. 
 * If null, it'll return in the fastest possible way available.
 * Valid options are://from w  w  w  . j a  va  2  s .  com
 *      String.class
 *      IDocument.class
 *      FastStringBuffer.class
 * 
 * @return an object with the contents from a path within a zip file, having the return type
 * of the object specified by the parameter returnType.
 */
public static Object getCustomReturnFromZip(File f, String pathInZip, Class<? extends Object> returnType)
        throws Exception {

    ZipFile zipFile = new ZipFile(f, ZipFile.OPEN_READ);
    try {
        InputStream inputStream = zipFile.getInputStream(zipFile.getEntry(pathInZip));
        try {
            return REF.getStreamContents(inputStream, null, null, returnType);
        } finally {
            inputStream.close();
        }
    } finally {
        zipFile.close();
    }
}

From source file:org.dbgl.util.FileUtils.java

public static void extractZip(final File archive, final File dirToBeExtracted, final File dstDir,
        final ProgressNotifyable prog) throws IOException {
    ZipFile zf = new ZipFile(archive);
    for (Enumeration<? extends ZipEntry> entries = zf.entries(); entries.hasMoreElements();) {
        ZipEntry entry = entries.nextElement();
        if (areRelated(dirToBeExtracted, new File(entry.getName()))) {
            File dstFile = new File(dstDir, strip(new File(entry.getName()), dirToBeExtracted).getPath());
            extractEntry(zf, entry, dstFile, prog);
        }//ww w . j av a 2s  . co  m
    }
    zf.close();
}

From source file:com.heliosdecompiler.helios.transformers.disassemblers.KrakatauDisassembler.java

public boolean disassembleClassNode(ClassNode cn, byte[] b, StringBuilder output) {
    if (Helios.ensurePython2Set()) {
        File inputJar = null;//from   ww w  .j  a  va 2 s.c  om
        File outputZip = null;
        String processLog = null;

        try {
            inputJar = Files.createTempFile("kdisin", ".jar").toFile();
            outputZip = Files.createTempFile("kdisout", ".zip").toFile();

            Map<String, byte[]> data = Helios.getAllLoadedData();
            data.put(cn.name + ".class", b);

            Utils.saveClasses(inputJar, data);

            Process process = Helios.launchProcess(new ProcessBuilder(
                    com.heliosdecompiler.helios.Settings.PYTHON2_LOCATION.get().asString(), "-O",
                    "disassemble.py", "-path", inputJar.getAbsolutePath(), "-out", outputZip.getAbsolutePath(),
                    cn.name + ".class", Settings.ROUNDTRIP.isEnabled() ? "-roundtrip" : "")
                            .directory(Constants.KRAKATAU_DIR));

            processLog = Utils.readProcess(process);

            ZipFile zipFile = new ZipFile(outputZip);
            Enumeration<? extends ZipEntry> entries = zipFile.entries();
            byte[] disassembled = null;
            while (entries.hasMoreElements()) {
                ZipEntry next = entries.nextElement();
                if (next.getName().equals(cn.name + ".j")) {
                    disassembled = IOUtils.toByteArray(zipFile.getInputStream(next));
                }
            }
            zipFile.close();
            output.append(new String(disassembled, "UTF-8"));
            return true;
        } catch (Exception e) {
            output.append(parseException(e)).append(processLog);
            return false;
        } finally {
            FileUtils.deleteQuietly(inputJar);
            FileUtils.deleteQuietly(outputZip);
        }
    } else {
        output.append("You need to set the location of Python 2.x");
    }
    return false;
}

From source file:averroes.JarOrganizer.java

/**
 * Process a given JAR file./*from   ww w  . j  av a2 s .  c om*/
 * 
 * @param fileName
 * @param fromApplicationArchive
 */
private void processArchive(String fileName, boolean fromApplicationArchive) {
    // Exit if the fileName is empty
    if (fileName.trim().length() <= 0) {
        return;
    }

    File file = new File(fileName);
    System.out.println("Processing " + (fromApplicationArchive ? "input" : "library") + " archive: "
            + file.getAbsolutePath());

    try {
        ZipFile archive = new ZipFile(file);
        Enumeration<? extends ZipEntry> entries = archive.entries();

        while (entries.hasMoreElements()) {
            ZipEntry entry = entries.nextElement();
            if (entry.getName().endsWith(".class")) {
                addClass(archive, entry, fromApplicationArchive);
            }
        }
        archive.close();
    } catch (IOException e) {
        e.printStackTrace();
        System.exit(1);
    }
}

From source file:com.samczsun.helios.transformers.disassemblers.KrakatauDisassembler.java

public boolean disassembleClassNode(ClassNode cn, byte[] b, StringBuilder output) {
    if (Helios.ensurePython2Set()) {
        File inputJar = null;/* w w w.jav  a2  s  . c  om*/
        File outputZip = null;
        String processLog = null;

        try {
            inputJar = Files.createTempFile("kdisin", ".jar").toFile();
            outputZip = Files.createTempFile("kdisout", ".zip").toFile();

            Map<String, byte[]> data = Helios.getAllLoadedData();
            data.put(cn.name + ".class", b);

            Utils.saveClasses(inputJar, data);

            Process process = Helios.launchProcess(new ProcessBuilder(
                    com.samczsun.helios.Settings.PYTHON2_LOCATION.get().asString(), "-O", "disassemble.py",
                    "-path", inputJar.getAbsolutePath(), "-out", outputZip.getAbsolutePath(),
                    cn.name + ".class", Settings.ROUNDTRIP.isEnabled() ? "-roundtrip" : "")
                            .directory(Constants.KRAKATAU_DIR));

            processLog = Utils.readProcess(process);

            ZipFile zipFile = new ZipFile(outputZip);
            Enumeration<? extends ZipEntry> entries = zipFile.entries();
            byte[] disassembled = null;
            while (entries.hasMoreElements()) {
                ZipEntry next = entries.nextElement();
                if (next.getName().equals(cn.name + ".j")) {
                    disassembled = IOUtils.toByteArray(zipFile.getInputStream(next));
                }
            }
            zipFile.close();
            output.append(new String(disassembled, "UTF-8"));
            return true;
        } catch (Exception e) {
            output.append(parseException(e)).append(processLog);
            return false;
        } finally {
            FileUtils.deleteQuietly(inputJar);
            FileUtils.deleteQuietly(outputZip);
        }
    } else {
        output.append("You need to set the location of Python 2.x");
    }
    return false;
}