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.openspaces.core.util.FileUtils.java

public static byte[] unzipFileToMemory(String applicationFile, File directoryOrZip, long maxFileSizeInBytes) {
    ZipFile zipFile = null;
    try {/*from ww w. j  ava2  s . c  om*/
        zipFile = new ZipFile(directoryOrZip);
        final ZipEntry zipEntry = zipFile.getEntry(applicationFile);
        if (zipEntry == null) {
            throw new RuntimeException("Failed to load " + applicationFile + " from " + directoryOrZip);
        }
        final int length = (int) zipEntry.getSize();
        if (length > maxFileSizeInBytes) {
            throw new RuntimeException(
                    applicationFile + " file size cannot be bigger than " + maxFileSizeInBytes + " bytes");
        }
        byte[] buffer = new byte[length];
        final InputStream in = zipFile.getInputStream(zipEntry);
        final DataInputStream din = new DataInputStream(in);
        try {
            din.readFully(buffer, 0, length);
            return buffer;
        } catch (final IOException e) {
            throw new RuntimeException("Failed to read application file input stream", e);
        }

    } catch (final IOException e) {
        throw new RuntimeException("Failed to read zip file " + directoryOrZip, e);
    } finally {
        if (zipFile != null) {
            try {
                zipFile.close();
            } catch (final IOException e) {
                logger.debug("Failed to close zip file " + directoryOrZip, e);
            }
        }
    }
}

From source file:org.opt4j.config.ModuleAutoFinder.java

/**
 * Returns {@code true} if the file is a Jar archive.
 * /*from www.  jav  a2 s  .  c o m*/
 * @param file
 *            the tested file
 * @return {@code true} if the file is a Jar archive
 */
protected boolean isJar(File file) {
    if (file.isDirectory()) {
        return false;
    }

    try {
        ZipFile zf = new ZipFile(file);
        zf.close();
        return true;
    } catch (ZipException e) {

    } catch (IOException e) {
        e.printStackTrace();
    }
    return false;
}

From source file:azkaban.project.AzkabanProjectLoader.java

private File unzipFile(final File archiveFile) throws IOException {
    final ZipFile zipfile = new ZipFile(archiveFile);
    final File unzipped = Utils.createTempDir(this.tempDir);
    Utils.unzip(zipfile, unzipped);//  www .  ja v a  2 s .co  m
    zipfile.close();

    return unzipped;
}

From source file:io.apiman.common.plugin.PluginClassLoader.java

/**
 * Closes any resources the plugin classloader is holding open.
 * @throws IOException if an I/O error has occurred
 *//*from www.ja  v a  2  s  . c  om*/
public void close() throws IOException {
    if (closed) {
        return;
    }
    this.pluginArtifactZip.close();
    for (ZipFile zipFile : this.dependencyZips) {
        zipFile.close();
    }
    closed = true;
}

From source file:de.shadowhunt.subversion.internal.AbstractPrepare.java

private static boolean extractArchive(final File zip, final File prefix) throws Exception {
    final ZipFile zipFile = new ZipFile(zip);
    final Enumeration<? extends ZipEntry> enu = zipFile.entries();
    while (enu.hasMoreElements()) {
        final ZipEntry zipEntry = enu.nextElement();

        final String name = zipEntry.getName();

        final File file = new File(prefix, name);
        if (name.charAt(name.length() - 1) == Resource.SEPARATOR_CHAR) {
            if (!file.isDirectory() && !file.mkdirs()) {
                throw new IOException("can not create directory structure: " + file);
            }/*from  w w w. j ava 2 s  .  com*/
            continue;
        }

        final File parent = file.getParentFile();
        if (parent != null) {
            if (!parent.isDirectory() && !parent.mkdirs()) {
                throw new IOException("can not create directory structure: " + parent);
            }
        }

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

    }
    zipFile.close();
    return true;
}

From source file:net.sourceforge.pmd.it.BinaryDistributionIT.java

@Test
public void testZipFileContent() throws IOException {
    Set<String> expectedFileNames = getExpectedFileNames();

    ZipFile zip = new ZipFile(getBinaryDistribution());

    Enumeration<? extends ZipEntry> entries = zip.entries();
    while (entries.hasMoreElements()) {
        ZipEntry entry = entries.nextElement();
        expectedFileNames.remove(entry.getName());
    }/*from  w  w  w.  ja va2 s  . c  om*/

    zip.close();

    if (!expectedFileNames.isEmpty()) {
        fail("Missing files in archive: " + expectedFileNames);
    }
}

From source file:org.zgis.wps.swat.AnnotatedSwatRunnerAlgorithm.java

/**
 * @param locationUri//from  w  ww  . j  a v a  2  s .  c o  m
 * @param fileName
 * @return
 * @throws ZipException
 * @throws IOException
 */
private URI getFile(final URI locationUri, final String fileName) throws ZipException, IOException {
    final File location;
    final URI fileURI;

    location = new File(locationUri);

    // not in a JAR, just return the path on disk
    if (location.isDirectory()) {
        fileURI = URI.create(locationUri.toString() + System.getProperty("file.separator") + fileName);
    } else {
        final ZipFile zipFile;

        zipFile = new ZipFile(location);

        try {
            fileURI = extractToTemp(zipFile, fileName);
        } finally {
            zipFile.close();
        }
    }
    return (fileURI);
}

From source file:Main.java

public static void unzipEPub(String inputZip, String destinationDirectory) throws IOException {
    int BUFFER = 2048;
    List zipFiles = new ArrayList();
    File sourceZipFile = new File(inputZip);
    File unzipDestinationDirectory = new File(destinationDirectory);
    unzipDestinationDirectory.mkdir();/*from  w ww  .  j ava2s . c  om*/

    ZipFile zipFile;
    zipFile = new ZipFile(sourceZipFile, ZipFile.OPEN_READ);
    Enumeration zipFileEntries = zipFile.entries();

    // Process each entry
    while (zipFileEntries.hasMoreElements()) {

        ZipEntry entry = (ZipEntry) zipFileEntries.nextElement();
        String currentEntry = entry.getName();
        File destFile = new File(unzipDestinationDirectory, currentEntry);

        if (currentEntry.endsWith(".zip")) {
            zipFiles.add(destFile.getAbsolutePath());
        }

        File destinationParent = destFile.getParentFile();
        destinationParent.mkdirs();

        if (!entry.isDirectory()) {
            BufferedInputStream is = new BufferedInputStream(zipFile.getInputStream(entry));
            int currentByte;
            // buffer for writing file
            byte data[] = new byte[BUFFER];

            FileOutputStream fos = new FileOutputStream(destFile);
            BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER);

            while ((currentByte = is.read(data, 0, BUFFER)) != -1) {
                dest.write(data, 0, currentByte);
            }
            dest.flush();
            dest.close();
            is.close();

        }

    }
    zipFile.close();

    for (Iterator iter = zipFiles.iterator(); iter.hasNext();) {
        String zipName = (String) iter.next();
        unzipEPub(zipName,
                destinationDirectory + File.separatorChar + zipName.substring(0, zipName.lastIndexOf(".zip")));
    }
}

From source file:org.sipfoundry.sipxconfig.upload.Upload.java

/**
 * Expand zip file into destination directory
 *///  w w  w . j a  va  2s .  c  o  m
static void deployZipFile(File expandDirectory, File zipFile, FileSetting fs) {
    if (!checkZipFile(zipFile)) {
        throw new UserException(ERROR_WRONG_TYPE_FILE, zipFile.getName());
    }

    try {
        ZipFile zip = new ZipFile(zipFile);
        Enumeration<? extends ZipEntry> entries = zip.entries();
        while (entries.hasMoreElements()) {
            InputStream in = null;
            OutputStream out = null;
            try {
                ZipEntry entry = entries.nextElement();
                if (fs.isExcluded(entry.getName())) {
                    continue;
                }
                File file = new File(expandDirectory, entry.getName());
                expandDirectory.mkdirs();
                if (entry.isDirectory()) {
                    file.mkdirs();
                } else {
                    file.getParentFile().mkdirs();
                    in = zip.getInputStream(entry);
                    out = new FileOutputStream(file);
                    IOUtils.copy(in, out);
                }
            } finally {
                IOUtils.closeQuietly(in);
                IOUtils.closeQuietly(out);
            }
        }
        zip.close();
    } catch (ZipException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.gisgraphy.importer.ImporterHelper.java

/**
 * unzip a file in the same directory as the zipped file
 * //ww  w.j  a v a 2 s.  c om
 * @param file
 *            The file to unzip
 */
public static void unzipFile(File file) {
    logger.info("will Extracting file: " + file.getName());
    Enumeration<? extends ZipEntry> entries;
    ZipFile zipFile;

    try {
        zipFile = new ZipFile(file);

        entries = zipFile.entries();

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

            if (entry.isDirectory()) {
                // Assume directories are stored parents first then
                // children.
                (new File(entry.getName())).mkdir();
                continue;
            }

            logger.info("Extracting file: " + entry.getName() + " to " + file.getParent() + File.separator
                    + entry.getName());
            copyInputStream(zipFile.getInputStream(entry), new BufferedOutputStream(
                    new FileOutputStream(file.getParent() + File.separator + entry.getName())));
        }

        zipFile.close();
    } catch (IOException e) {
        logger.error("can not unzip " + file.getName() + " : " + e.getMessage(), e);
        throw new ImporterException(e);
    }
}