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.omegat.filters3.xml.opendoc.OpenDocFilter.java

/** Returns true if it's OpenDocument file. */
public boolean isFileSupported(File inFile, Map<String, String> config, FilterContext fc) {
    try {//from  ww w  .  j ava 2 s .  c o  m
        ZipFile file = new ZipFile(inFile);
        Enumeration<? extends ZipEntry> entries = file.entries();
        while (entries.hasMoreElements()) {
            ZipEntry entry = entries.nextElement();
            if (TRANSLATABLE.contains(entry.getName())) {
                file.close();
                return true;
            }
        }
        file.close();
    } catch (IOException e) {
    }
    return false;
}

From source file:org.rioproject.impl.util.DownloadManager.java

/**
 * Remove installed software//  ww w.ja v  a  2s.  com
 * 
 * @param record The DownloadRecord to remove
 *
 * @return the top-most directory/file that was removed
 */
public static String remove(DownloadRecord record) {
    if (record == null)
        throw new IllegalArgumentException("record is null");
    File software = new File(FileUtils.makeFileName(record.getPath(), record.getName()));
    if (!software.exists()) {
        logger.debug("Software recorded at [{}] does not exist or has already been removed "
                + "from the file system, removal aborted", FileUtils.getFilePath(software));
        return null;
    }
    String removed;
    if (record.unarchived()) {
        if (software.getName().endsWith(".gz") || software.getName().endsWith(".gzip")) {
            FileUtils.remove(software);
            /* Strip off the extension and see if we still have
             * something to remove */
            int ndx = software.getName().lastIndexOf(".");
            if (ndx != -1) {
                String newName = software.getName().substring(0, ndx);
                software = new File(FileUtils.makeFileName(record.getPath(), newName));
            }
        }
        if (software.getName().endsWith("tar")) {
            try {
                File root = new File(record.getPath());
                removeExtractedTarFiles(root, software);
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            ZipFile zipFile = null;
            try {
                zipFile = new ZipFile(software);
                Enumeration zipEntries = zipFile.entries();
                while (zipEntries.hasMoreElements()) {
                    ZipEntry zipEntry = (ZipEntry) zipEntries.nextElement();
                    File file = new File(record.getPath() + File.separator + zipEntry.getName());
                    FileUtils.remove(file);
                }
            } catch (ZipException e) {
                logger.error("Error in opening zip file {}", FileUtils.getFilePath(software), e);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (zipFile != null) {
                    try {
                        zipFile.close();
                    } catch (IOException e) {
                        logger.error("Could not close zip file", e);
                    }
                }
            }
        }
        removed = FileUtils.getFilePath(software);
        FileUtils.remove(software);
        File softwareDirectory = new File(record.getPath());
        String[] list = softwareDirectory.list();
        if (list.length == 0 && !softwareDirectory.getName().equals("native"))
            FileUtils.remove(softwareDirectory);
    } else {
        if (record.createdParentDirectory()) {
            removed = record.getPath();
            FileUtils.remove(new File(record.getPath()));
        } else {
            removed = FileUtils.getFilePath(software);
            FileUtils.remove(software);
        }
    }

    return removed;
}

From source file:org.apache.geode.management.internal.configuration.utils.ZipUtils.java

public static void unzip(String zipFilePath, String outputDirectoryPath) throws IOException {
    ZipFile zipFile = new ZipFile(zipFilePath);
    @SuppressWarnings("unchecked")
    Enumeration<ZipEntry> zipEntries = (Enumeration<ZipEntry>) zipFile.entries();

    try {/*from ww  w . j  a va2 s  .  c o  m*/
        while (zipEntries.hasMoreElements()) {
            ZipEntry zipEntry = zipEntries.nextElement();
            String fileName = outputDirectoryPath + File.separator + zipEntry.getName();

            if (zipEntry.isDirectory()) {
                FileUtils.forceMkdir(new File(fileName));
                continue;
            }
            File entryDestination = new File(fileName);
            File parent = entryDestination.getParentFile();
            if (parent != null) {
                FileUtils.forceMkdir(parent);
            }
            if (entryDestination.createNewFile()) {

                InputStream in = zipFile.getInputStream(zipEntry);
                OutputStream out = new FileOutputStream(entryDestination);
                try {
                    IOUtils.copy(in, out);
                } finally {
                    IOUtils.closeQuietly(in);
                    IOUtils.closeQuietly(out);
                }
            } else {
                throw new IOException("Cannot create file :" + entryDestination.getCanonicalPath());
            }
        }
    } finally {
        zipFile.close();
    }
}

From source file:com.aliyun.odps.local.common.utils.ArchiveUtils.java

/**
 * Given a File input it will unzip the file in a the unzip directory passed
 * as the second parameter//  w  w  w.j  a v  a2s  .  c  o  m
 * 
 * @param inFile
 *          The zip file as input
 * @param unzipDir
 *          The unzip directory where to unzip the zip file.
 * @throws IOException
 */
public static void unZip(File inFile, File unzipDir) throws IOException {
    Enumeration<? extends ZipEntry> entries;
    ZipFile zipFile = new ZipFile(inFile);

    try {
        entries = zipFile.entries();
        while (entries.hasMoreElements()) {
            ZipEntry entry = entries.nextElement();
            if (!entry.isDirectory()) {
                InputStream in = zipFile.getInputStream(entry);
                try {
                    File file = new File(unzipDir, 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 {
        zipFile.close();
    }
}

From source file:it.readbeyond.minstrel.unzipper.Unzipper.java

private String list(String inputZip) throws IOException, JSONException {
    // list all files
    List<String> acc = new ArrayList<String>();
    File sourceZipFile = new File(inputZip);
    ZipFile zipFile = new ZipFile(sourceZipFile, ZipFile.OPEN_READ);
    Enumeration<? extends ZipEntry> zipFileEntries = zipFile.entries();
    while (zipFileEntries.hasMoreElements()) {
        acc.add(zipFileEntries.nextElement().getName());
    }//from  w w  w  .j a  v a2 s. c  om
    zipFile.close();

    // sort
    Collections.sort(acc);

    // return JSON string
    return stringify(acc);
}

From source file:org.mycontroller.standalone.BackupRestore.java

private static void extractZipFile(String zipFileName, String destination)
        throws FileNotFoundException, IOException {
    ZipFile zipFile = new ZipFile(zipFileName);
    Enumeration<?> enu = zipFile.entries();
    //create destination if not exists
    FileUtils.forceMkdir(FileUtils.getFile(destination));
    while (enu.hasMoreElements()) {
        ZipEntry zipEntry = (ZipEntry) enu.nextElement();
        String name = zipEntry.getName();
        long size = zipEntry.getSize();
        long compressedSize = zipEntry.getCompressedSize();
        _logger.debug("name:{} | size:{} | compressed size:{}", name, size, compressedSize);
        File file = FileUtils.getFile(destination + "/" + name);
        //Create destination if it's not available
        if (name.endsWith("/")) {
            file.mkdirs();/*from w  ww . j av  a  2  s  . c  o  m*/
            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();
}

From source file:org.openoffice.plugin.core.model.UnoPackageTest.java

private String getManifestContent() throws ZipException, IOException {
    ZipFile zip = new ZipFile(tmpFile);
    try {/*w  ww .  j a va2s.c  o  m*/
        ZipEntry entry = zip.getEntry("META-INF/manifest.xml");
        InputStream istream = zip.getInputStream(entry);
        return IOUtils.toString(istream);
    } finally {
        zip.close();
    }
}

From source file:org.mule.tools.maven.app.it.AbstractMavenIT.java

protected void assertZipDoesContain(File file, String... filenames) throws IOException {
    ZipFile zipFile = null;
    try {//from  ww  w . j av  a 2s  .co  m
        zipFile = new ZipFile(file);
        for (String name : filenames) {
            if (zipFile.getEntry(name) == null) {
                fail(file.getAbsolutePath() + " does not contain valid entry " + name);
            }
        }
    } finally {
        if (zipFile != null) {
            zipFile.close();
        }
    }
}

From source file:org.pentaho.webpackage.deployer.UrlTransformer.java

@Override
public boolean canHandle(File file) {
    if (!file.exists()) {
        return false;
    }//from  w ww .  j  a v  a 2s .  c  o m

    if (file.getName().endsWith(".tgz")) {
        TarArchiveInputStream tarInput = null;
        try {
            tarInput = new TarArchiveInputStream(new GzipCompressorInputStream(new FileInputStream(file)));
            TarArchiveEntry currentEntry = tarInput.getNextTarEntry();
            while (currentEntry != null) {
                if (currentEntry.getName().endsWith("package.json")) {
                    return true;
                }

                currentEntry = tarInput.getNextTarEntry();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (tarInput != null) {
                try {
                    tarInput.close();
                } catch (IOException ignored) {
                    // Ignore
                }
            }
        }

        return false;
    } else if (file.getName().endsWith(".zip") || file.getName().endsWith(".jar")) {
        ZipFile zipFile = null;

        try {
            zipFile = new ZipFile(file);

            return zipFile.getEntry("package.json") != null && zipFile.getEntry("META-INF/MANIFEST.MF") == null;
        } catch (IOException e) {
            this.logger.error(e.getMessage(), e);
        } finally {
            if (zipFile != null) {
                try {
                    zipFile.close();
                } catch (IOException ignored) {
                    // Ignore
                }
            }
        }
    }

    return false;
}

From source file:nz.ac.otago.psyanlab.common.util.FileUtils.java

/**
 * Inflate a pale file to a given working directory.
 * //from  w ww. ja  v  a  2 s . c o m
 * @param paleFile .pale file to inflate.
 * @param workingDir Path to extract to.
 * @return Path of the location the file was extracted to.
 * @throws FileNotFoundException
 * @throws IOException
 */
public static File decompress(File paleFile, File workingDir) throws FileNotFoundException, IOException {
    ZipFile archive = new ZipFile(paleFile);

    try {
        // Iterate over elements in zip file and extract them to working
        // directory.
        Enumeration<? extends ZipEntry> entries = archive.entries();
        while (entries.hasMoreElements()) {
            ZipEntry ze = entries.nextElement();
            File newFile = new File(workingDir, ze.getName());

            if (ze.isDirectory()) {
                if (!newFile.exists()) {
                    newFile.mkdirs();
                }
                continue;
            }

            if (!newFile.getParentFile().exists()) {
                // Dir tree missing for some reason so create it.
                newFile.getParentFile().mkdirs();
            }

            InputStream in = archive.getInputStream(ze);
            OutputStream out = new BufferedOutputStream(new FileOutputStream(newFile));

            try {
                copy(in, out);
            } finally {
                in.close();
                out.close();
            }
        }
    } finally {
        archive.close();
    }

    return workingDir;
}