Example usage for org.apache.commons.compress.archivers.zip ZipFile close

List of usage examples for org.apache.commons.compress.archivers.zip ZipFile close

Introduction

In this page you can find the example usage for org.apache.commons.compress.archivers.zip ZipFile close.

Prototype

public void close() throws IOException 

Source Link

Document

Closes the archive.

Usage

From source file:org.xwiki.wiki.workspacesmigrator.internal.DefaultDocumentRestorerFromAttachedXAR.java

@Override
public void restoreDocumentFromAttachedXAR(DocumentReference docReference, String attachmentName,
        List<DocumentReference> documentsToRestore) throws XWikiException {
    XWikiContext xcontext = xcontextProvider.get();
    XWiki xwiki = xcontext.getWiki();/*from w  w  w .ja v  a 2 s.  c  om*/
    File tempZipFile = null;
    try {
        tempZipFile = getTemporaryZipFile(docReference, attachmentName);
        if (tempZipFile == null) {
            return;
        }
        ZipFile zipFile = new ZipFile(tempZipFile);
        // We look for each document to restore if there is a corresponding zipEntry.
        Iterator<DocumentReference> itDocumentsToRestore = documentsToRestore.iterator();
        while (itDocumentsToRestore.hasNext()) {
            DocumentReference docRef = itDocumentsToRestore.next();

            // Compute what should be the filename of the document to restore
            String fileNameToRestore = String.format("%s/%s.xml", docRef.getLastSpaceReference().getName(),
                    docRef.getName());

            // Get the corresponding zip Entry
            ZipArchiveEntry zipEntry = zipFile.getEntry(fileNameToRestore);
            if (zipEntry != null) {
                // Restore the document
                XWikiDocument docToRestore = xwiki.getDocument(docRef, xcontext);
                docToRestore.fromXML(zipFile.getInputStream(zipEntry));
                xwiki.saveDocument(docToRestore, xcontext);
                // We have restored this document
                itDocumentsToRestore.remove();
            }
        }
        zipFile.close();
    } catch (IOException e) {
        logger.error("Error during the decompression of [{}].", attachmentName, e);
    } finally {
        // Delete the temporary zip file
        if (tempZipFile != null) {
            tempZipFile.delete();
        }
    }
}

From source file:org.xwiki.xar.XarPackage.java

/**
 * @param file the XAR file/*from   www .j  a  v a2 s  .c o  m*/
 * @throws IOException when failing to read the file
 * @throws XarException when failing to parse the XAR package
 */
public XarPackage(File file) throws IOException, XarException {
    ZipFile zipFile = new ZipFile(file);

    try {
        read(zipFile);
    } finally {
        zipFile.close();
    }
}

From source file:org.yes.cart.utils.impl.ZipUtils.java

/**
 * Unzip archive to given folder./*  w w w  . ja v  a 2 s .  c o m*/
 * @param archive given archive
 * @param outputDir  given folder
 * @throws IOException in case of error
 */

public void unzipArchive(final File archive, final File outputDir) throws IOException {
    ZipFile zipfile = new ZipFile(archive, encoding);
    for (Enumeration e = zipfile.getEntries(); e.hasMoreElements();) {
        final ZipArchiveEntry entry = (ZipArchiveEntry) e.nextElement();
        unzipEntry(zipfile, entry, outputDir);
    }
    zipfile.close();
}

From source file:uk.nhs.cfh.dsp.srth.distribution.FileDownloader.java

private void extractZipFileContents(File file) {
    Enumeration entries;//  ww w.  ja v a 2s  . c o  m

    try {
        logger.info("Extracting zip file contents...");
        ZipFile zipFile = new ZipFile(file);

        entries = zipFile.getEntries();

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

            if (entry.isDirectory()) {

                logger.info("Extracting directory: " + entry.getName());
                File dir = new File(file.getParent(), entry.getName());
                boolean canWrite = dir.exists();
                if (!canWrite) {
                    canWrite = dir.mkdir();
                }

                if (canWrite) {
                    continue;
                } else {
                    logger.warning("Error creating directory during extraction");
                }
            }

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

        zipFile.close();
        logger.info("Closed zip file.");
    } catch (IOException e) {
        logger.warning("Nested exception is : " + e.fillInStackTrace());
    }
}