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:org.sonar.plugins.scm.git.JGitBlameCommandTest.java

private static void javaUnzip(File zip, File toDir) {
    try {/*from www . ja  v a  2s  .co m*/
        ZipFile zipFile = new ZipFile(zip);
        try {
            Enumeration<? extends ZipEntry> entries = zipFile.entries();
            while (entries.hasMoreElements()) {
                ZipEntry entry = entries.nextElement();
                File to = new File(toDir, entry.getName());
                if (entry.isDirectory()) {
                    FileUtils.forceMkdir(to);
                } else {
                    File parent = to.getParentFile();
                    if (parent != null) {
                        FileUtils.forceMkdir(parent);
                    }

                    OutputStream fos = new FileOutputStream(to);
                    try {
                        IOUtils.copy(zipFile.getInputStream(entry), fos);
                    } finally {
                        Closeables.closeQuietly(fos);
                    }
                }
            }
        } finally {
            zipFile.close();
        }
    } catch (Exception e) {
        throw new IllegalStateException("Fail to unzip " + zip + " to " + toDir, e);
    }
}

From source file:com.twosigma.beakerx.kernel.magic.command.ClasspathAddMvnDepsCellMagicCommandTest.java

private static void unzipRepo() {
    try {//w  ww  .ja v a2s  .c  o  m
        ZipFile zipFile = new ZipFile(BUILD_PATH + "/testMvnCache.zip");
        Enumeration<?> enu = zipFile.entries();
        while (enu.hasMoreElements()) {
            ZipEntry zipEntry = (ZipEntry) enu.nextElement();
            String name = BUILD_PATH + "/" + zipEntry.getName();
            File file = new File(name);
            if (name.endsWith("/")) {
                file.mkdirs();
                continue;
            }

            File parent = file.getParentFile();
            if (parent != null) {
                parent.mkdirs();
            }

            InputStream is = zipFile.getInputStream(zipEntry);
            FileOutputStream fos = new FileOutputStream(file);
            byte[] bytes = new byte[1024];
            int length;
            while ((length = is.read(bytes)) >= 0) {
                fos.write(bytes, 0, length);
            }
            is.close();
            fos.close();

        }
        zipFile.close();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:kr.ac.kaist.swrc.jhannanum.share.JSONZipReader.java

/**
 * read JSON file from JAR file.//from  w w w  . ja  v a 2 s  . c o  m
 * @return Length of JSON Keys
 * @throws JSONException, IOException
 */

protected int read() throws JSONException, IOException {
    ZipFile zip = new ZipFile(zipFilePath);
    ZipEntry entry = zip.getEntry(jsonFile);
    InputStream in = zip.getInputStream(entry);
    this.json = read(in);
    zip.close();
    return json.length();
}

From source file:org.roda.common.certification.ODFSignatureUtils.java

public static List<Path> runDigitalSignatureExtract(Path input) throws SignatureException, IOException {
    List<Path> paths = new ArrayList<Path>();

    ZipFile zipFile = new ZipFile(input.toString());
    Enumeration<?> enumeration;
    for (enumeration = zipFile.entries(); enumeration.hasMoreElements();) {
        ZipEntry entry = (ZipEntry) enumeration.nextElement();
        String entryName = entry.getName();
        if (META_INF_DOCUMENTSIGNATURES_XML.equalsIgnoreCase(entryName)) {
            Path extractedSignature = Files.createTempFile("extraction", ".xml");
            InputStream zipStream = zipFile.getInputStream(entry);
            FileUtils.copyInputStreamToFile(zipStream, extractedSignature.toFile());
            paths.add(extractedSignature);
            IOUtils.closeQuietly(zipStream);
        }/*from w w w  .j  a  v a  2 s.  com*/
    }

    zipFile.close();
    return paths;
}

From source file:org.wso2.carbon.mediation.initializer.utils.SynapseArtifactInitUtils.java

private static void extract(String sourcePath, String destPath) throws IOException {
    Enumeration entries;/*from   w  w  w . j av a2s .c  o m*/
    ZipFile zipFile;

    zipFile = new ZipFile(sourcePath);
    entries = zipFile.entries();

    String canonicalDestPath = new File(destPath).getCanonicalPath();
    while (entries.hasMoreElements()) {
        ZipEntry entry = (ZipEntry) entries.nextElement();
        String canonicalEntryPath = new File(destPath + entry.getName()).getCanonicalPath();
        if (!canonicalEntryPath.startsWith(canonicalDestPath)) {
            throw new IOException("Entry is outside of the target dir: " + entry.getName());
        }
        // if the entry is a directory, create a new dir
        if (!entry.isDirectory() && entry.getName().equalsIgnoreCase(CONNECTOR_XML)) {
            // if the entry is a file, write the file
            copyInputStream(zipFile.getInputStream(entry),
                    new BufferedOutputStream(new FileOutputStream(destPath + entry.getName())));
        }
    }
    zipFile.close();
}

From source file:de.xwic.appkit.core.util.ZipUtil.java

/**
 * Unzips the files from the zipped file into the destination folder.
 * //from  ww  w  . j av a2  s  . c o m
 * @param zippedFile
 *            the files array
 * @param destinationFolder
 *            the folder in which the zip archive will be unzipped
 * @return the file array which consists into the files which were zipped in
 *         the zippedFile
 * @throws IOException
 */
public static File[] unzip(File zippedFile, String destinationFolder) throws IOException {

    ZipFile zipFile = null;
    List<File> files = new ArrayList<File>();

    try {

        zipFile = new ZipFile(zippedFile);
        Enumeration<?> entries = zipFile.entries();

        while (entries.hasMoreElements()) {

            ZipEntry entry = (ZipEntry) entries.nextElement();

            if (!entry.isDirectory()) {

                String filePath = destinationFolder + System.getProperty("file.separator") + entry.getName();
                FileOutputStream stream = new FileOutputStream(filePath);

                InputStream is = zipFile.getInputStream(entry);

                log.info("Unzipping " + entry.getName());

                int n = 0;
                while ((n = is.read(BUFFER)) > 0) {
                    stream.write(BUFFER, 0, n);
                }

                is.close();
                stream.close();

                files.add(new File(filePath));

            }

        }

        zipFile.close();
    } catch (IOException e) {

        log.error("Error: " + e.getMessage(), e);
        throw e;

    } finally {

        try {

            if (null != zipFile) {
                zipFile.close();
            }

        } catch (IOException e) {
            log.error("Error: " + e.getMessage(), e);
            throw e;
        }

    }

    File[] array = files.toArray(new File[files.size()]);
    return array;
}

From source file:com.openAtlas.bundleInfo.maker.PackageLite.java

public static PackageLite parse(String apkPath) {
    ZipFile file = null;

    StringBuilder xmlSb = new StringBuilder(100);
    try {/*from   w ww.j a  va2 s.com*/
        File apkFile = new File(apkPath);
        file = new ZipFile(apkFile, ZipFile.OPEN_READ);
        ZipEntry entry = file.getEntry(DEFAULT_XML);

        AXmlResourceParser parser = new AXmlResourceParser();
        parser.open(file.getInputStream(entry));
        PackageLite packageLite = new PackageLite();
        packageLite.apkMD5 = FileUtils.getMD5(apkPath);
        packageLite.size = apkFile.length();
        packageLite.checkNativeLibs(file);
        packageLite.parse(parser);
        //System.err.println(packageLite.getBundleInfo().toString());
        file.close();
        return packageLite;
        //parser.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:org.acdd.ext.bundleInfo.maker.PackageLite.java

public static PackageLite parse(String apkPath) {
    ZipFile file = null;

    StringBuilder xmlSb = new StringBuilder(100);
    try {//from   w  ww  . j a  v  a  2  s . com
        File apkFile = new File(apkPath);
        file = new ZipFile(apkFile, ZipFile.OPEN_READ);
        ZipEntry entry = file.getEntry(DEFAULT_XML);

        AXmlResourceParser parser = new AXmlResourceParser();
        parser.open(file.getInputStream(entry));
        PackageLite packageLite = new PackageLite();
        packageLite.apkMD5 = ACDDFileUtils.getMD5(apkPath);
        packageLite.size = apkFile.length();
        packageLite.checkNativeLibs(file);
        packageLite.parse(parser);
        //System.err.println(packageLite.getBundleInfo().toString());
        file.close();
        return packageLite;
        //parser.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:org.apache.synapse.libraries.util.LibDeployerUtils.java

private static void extract(String sourcePath, String destPath) throws IOException {
    Enumeration entries;/*from   www  . j  a va2  s .  com*/
    ZipFile zipFile;

    zipFile = new ZipFile(sourcePath);
    entries = zipFile.entries();

    while (entries.hasMoreElements()) {
        ZipEntry entry = (ZipEntry) entries.nextElement();
        // we don't need to copy the META-INF dir
        if (entry.getName().startsWith("META-INF/")) {
            continue;
        }
        // if the entry is a directory, create a new dir
        if (entry.isDirectory()) {
            createDir(destPath + entry.getName());
            continue;
        }
        // if the entry is a file, write the file
        copyInputStream(zipFile.getInputStream(entry),
                new BufferedOutputStream(new FileOutputStream(destPath + entry.getName())));
    }
    zipFile.close();
}

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

public static long extractZipEntrySizeInBytes(final File archive, final String zipEntryToBeExtracted)
        throws IOException {
    ZipFile zf = new ZipFile(archive);
    ZipEntry entry = zf.getEntry(zipEntryToBeExtracted);
    zf.close();
    if (entry != null)
        return entry.getSize();
    return 0;/*from   ww w . j  a va2  s. c om*/
}