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:ZipExploder.java

/**
 * Explode source ZIP or JAR file into a target directory
 * /*  ww w.  ja  v  a  2 s. c o m*/
 * @param zipName
 *          names of source file
 * @param destDir
 *          target directory name (should already exist)
 * @exception IOException
 *              error creating a target file
 */
public void processFile(String zipName, String destDir) throws IOException {
    String source = new File(zipName).getCanonicalPath();
    String dest = new File(destDir).getCanonicalPath();
    ZipFile f = null;
    try {
        f = new ZipFile(source);
        Map fEntries = getEntries(f);
        String[] names = (String[]) fEntries.keySet().toArray(new String[] {});
        if (sortNames) {
            Arrays.sort(names);
        }
        // copy all files
        for (int i = 0; i < names.length; i++) {
            String name = names[i];
            ZipEntry e = (ZipEntry) fEntries.get(name);
            copyFileEntry(dest, f, e);
        }
    } catch (IOException ioe) {
        String msg = ioe.getMessage();
        if (msg.indexOf(zipName) < 0) {
            msg += " - " + zipName;
        }
        throw new IOException(msg);
    } finally {
        if (f != null) {
            try {
                f.close();
            } catch (IOException ioe) {
            }
        }
    }
}

From source file:com.cloudant.sync.datastore.DatastoreSchemaTests.java

@SuppressWarnings("ResultOfMethodCallIgnored") // mkdirs result should be fine
private boolean unzipToDirectory(File zipPath, File outputDirectory) {
    try {//from   w w w  .  ja v a 2 s.  c om

        ZipFile zipFile = new ZipFile(zipPath);
        try {
            Enumeration<? extends ZipEntry> entries = zipFile.entries();
            while (entries.hasMoreElements()) {
                ZipEntry entry = entries.nextElement();
                File entryDestination = new File(outputDirectory, entry.getName());
                if (entry.isDirectory())
                    entryDestination.mkdirs();
                else {
                    entryDestination.getParentFile().mkdirs();
                    InputStream in = zipFile.getInputStream(entry);
                    OutputStream out = new FileOutputStream(entryDestination);
                    IOUtils.copy(in, out);
                    IOUtils.closeQuietly(in);
                    out.close();
                }
            }
        } finally {
            zipFile.close();
        }

        return true;

    } catch (Exception ex) {
        return false;
    }
}

From source file:io.jxcore.node.jxcore.java

private void Initialize(String home) {
    // assets.list is terribly slow, trick below is literally 100 times faster
    StringBuilder assets = new StringBuilder();
    assets.append("{");
    boolean first_entry = true;
    try {//from  www .ja v  a 2 s .  c o  m
        ZipFile zf = new ZipFile(activity.getBaseContext().getApplicationInfo().sourceDir);
        try {
            for (Enumeration<? extends ZipEntry> e = zf.entries(); e.hasMoreElements();) {
                ZipEntry ze = e.nextElement();
                String name = ze.getName();
                if (name.startsWith("assets/www/jxcore/")) {
                    if (first_entry)
                        first_entry = false;
                    else
                        assets.append(",");
                    int size = FileManager.aproxFileSize(name.substring(7));
                    assets.append("\"" + name.substring(18) + "\":" + size);
                }
            }
        } finally {
            zf.close();
        }
    } catch (Exception e) {
    }
    assets.append("}");

    prepareEngine(home + "/www/jxcore", assets.toString());

    String mainFile = FileManager.readFile("jxcore_cordova.js");

    String data = "process.setPaths = function(){ process.cwd = function() { return '" + home
            + "/www/jxcore';};\n" + "process.userPath ='"
            + activity.getBaseContext().getFilesDir().getAbsolutePath() + "';\n" + "};" + mainFile;

    defineMainFile(data);

    startEngine();

    jxcoreInitialized = true;
}

From source file:org.wso2.developerstudio.eclipse.embedded.tomcat.server.EmbeddedTomcatServer.java

/**
 * Extracts a war file to a given directory.
 * /*from w  w  w  .  j a  v  a 2  s . c o m*/
 * @param warFile Source war file.
 * @param extractpath Destination to extract.
 * 
 * @throws IOException
 */
private void extractWar(File warFile, File extractpath) throws IOException {
    ZipFile zipFile = new ZipFile(warFile);
    try {
        Enumeration<? extends ZipEntry> entries = zipFile.entries();
        while (entries.hasMoreElements()) {
            ZipEntry entry = entries.nextElement();
            File entryDestination = new File(extractpath, entry.getName());
            if (entry.isDirectory())
                entryDestination.mkdirs();
            else {
                entryDestination.getParentFile().mkdirs();
                InputStream in = zipFile.getInputStream(entry);
                OutputStream out = new FileOutputStream(entryDestination);
                IOUtils.copy(in, out);
                IOUtils.closeQuietly(in);
                out.close();
            }
        }
    } finally {
        zipFile.close();
    }
}

From source file:dalma.container.ClassLoaderImpl.java

/**
 * Cleans up any resources held by this classloader. Any open archive
 * files are closed.//from w w  w.j a v a 2s  . c o m
 */
public synchronized void cleanup() {
    for (Enumeration e = zipFiles.elements(); e.hasMoreElements();) {
        ZipFile zipFile = (ZipFile) e.nextElement();
        try {
            zipFile.close();
        } catch (IOException ioe) {
            // ignore
        }
    }
    zipFiles = new Hashtable<File, ZipFile>();
}

From source file:nl.edia.sakai.tool.skinmanager.impl.SkinFileSystemServiceImpl.java

protected void installSkin(File skinDir, File file) throws SkinException, IOException {
    ZipFile myZipFile = null;
    try {//from   w  ww . j  a v  a  2  s.c om
        myZipFile = new ZipFile(file);
        Enumeration<? extends ZipEntry> myEntries = myZipFile.entries();
        while (myEntries.hasMoreElements()) {
            ZipEntry myZipEntry = (ZipEntry) myEntries.nextElement();
            handleEntry(myZipEntry, myZipFile, skinDir);
        }
    } catch (ZipException z) {
        throw new SkinException("Error reading zip file", z);
    } finally {
        if (myZipFile != null) {
            try {
                myZipFile.close();
            } catch (IOException e) {
                // ignore
            }
        }

    }
}

From source file:com.funambol.framework.tools.FileArchiverTest.java

private void assertDestFileContent(File destFile, List<String> expContent) throws IOException {

    ZipFile archive = null;
    try {//from w  w  w .j  a v a2 s.c o m
        archive = new ZipFile(destFile);
        Enumeration<? extends ZipEntry> entries = archive.entries();

        assertEquals(expContent.size(), archive.size());

        while (entries.hasMoreElements()) {
            ZipEntry entry = entries.nextElement();
            assertTrue(expContent.contains(entry.getName()));
        }
    } finally {
        if (archive != null) {
            archive.close();
            archive = null;
        }
    }
}

From source file:org.apache.phoenix.pherf.util.ResourceList.java

Collection<String> getResourcesFromJarFile(final File file, final Pattern pattern) {
    final List<String> retVal = new ArrayList<>();
    ZipFile zf;
    try {/*  w  ww  .j av  a2  s . c  o m*/
        zf = new ZipFile(file);
    } catch (FileNotFoundException e) {
        // Gracefully handle a jar listed on the classpath that doesn't actually exist.
        return Collections.emptyList();
    } catch (final ZipException e) {
        throw new Error(e);
    } catch (final IOException e) {
        throw new Error(e);
    }
    final Enumeration e = zf.entries();
    while (e.hasMoreElements()) {
        final ZipEntry ze = (ZipEntry) e.nextElement();
        final String fileName = ze.getName();
        final boolean accept = pattern.matcher(fileName).matches();
        logger.trace("fileName:" + fileName);
        logger.trace("File:" + file.toString());
        logger.trace("Match:" + accept);
        if (accept) {
            logger.trace("Adding File from Jar: " + fileName);
            retVal.add("/" + fileName);
        }
    }
    try {
        zf.close();
    } catch (final IOException e1) {
        throw new Error(e1);
    }
    return retVal;
}

From source file:com.ikon.util.DocConverter.java

/**
 * Convert ZIP to PDF/*from ww  w  .  ja  v  a2 s .c  om*/
 */
@SuppressWarnings("rawtypes")
public void zip2pdf(File input, File output) throws ConversionException, DatabaseException, IOException {
    log.debug("** Convert from ZIP to PDF **");
    FileOutputStream fos = null;
    ZipFile zipFile = null;

    try {
        fos = new FileOutputStream(output);

        // Make conversion
        zipFile = new ZipFile(input);
        Document doc = new Document(PageSize.A4);
        PdfWriter.getInstance(doc, fos);
        doc.open();

        for (Enumeration e = zipFile.entries(); e.hasMoreElements();) {
            ZipEntry entry = (ZipEntry) e.nextElement();
            doc.add(new Paragraph(12F, entry.getName()));
        }

        doc.close();
        zipFile.close();
    } catch (ZipException e) {
        throw new ConversionException("Exception in conversion: " + e.getMessage(), e);
    } catch (DocumentException e) {
        throw new ConversionException("Exception in conversion: " + e.getMessage(), e);
    } finally {
        IOUtils.closeQuietly(fos);
    }
}

From source file:org.opencms.module.CmsModuleImportExportHandler.java

/**
 * Reads a module object from an external file source.<p>
 * /*ww  w  .  j  a v  a2 s .  com*/
 * @param importResource the name of the input source
 * 
 * @return the imported module
 *  
 * @throws CmsConfigurationException if the module could not be imported
 */
public static CmsModule readModuleFromImport(String importResource) throws CmsConfigurationException {

    // instantiate Digester and enable XML validation
    Digester digester = new Digester();
    digester.setUseContextClassLoader(true);
    digester.setValidating(false);
    digester.setRuleNamespaceURI(null);
    digester.setErrorHandler(new CmsXmlErrorHandler(importResource));

    // add this class to the Digester
    CmsModuleImportExportHandler handler = new CmsModuleImportExportHandler();
    digester.push(handler);

    CmsModuleXmlHandler.addXmlDigesterRules(digester);

    InputStream stream = null;
    ZipFile importZip = null;

    try {
        File file = new File(importResource);
        if (file.isFile()) {
            importZip = new ZipFile(importResource);
            ZipEntry entry = importZip.getEntry(CmsImportExportManager.EXPORT_MANIFEST);
            if (entry != null) {
                stream = importZip.getInputStream(entry);
            } else {
                CmsMessageContainer message = Messages.get().container(Messages.ERR_NO_MANIFEST_MODULE_IMPORT_1,
                        importResource);
                LOG.error(message.key());
                throw new CmsConfigurationException(message);
            }
        } else if (file.isDirectory()) {
            file = new File(file, CmsImportExportManager.EXPORT_MANIFEST);
            stream = new FileInputStream(file);
        }

        // start the parsing process        
        digester.parse(stream);
    } catch (IOException e) {
        CmsMessageContainer message = Messages.get().container(Messages.ERR_IO_MODULE_IMPORT_1, importResource);
        LOG.error(message.key(), e);
        throw new CmsConfigurationException(message, e);
    } catch (SAXException e) {
        CmsMessageContainer message = Messages.get().container(Messages.ERR_SAX_MODULE_IMPORT_1,
                importResource);
        LOG.error(message.key(), e);
        throw new CmsConfigurationException(message, e);
    } finally {
        try {
            if (importZip != null) {
                importZip.close();
            }
            if (stream != null) {
                stream.close();
            }
        } catch (Exception e) {
            // noop
        }
    }

    CmsModule importedModule = handler.getModule();

    // the digester must have set the module now
    if (importedModule == null) {
        throw new CmsConfigurationException(
                Messages.get().container(Messages.ERR_IMPORT_MOD_ALREADY_INSTALLED_1, importResource));
    }

    return importedModule;
}