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

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

Introduction

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

Prototype

public ZipFile(String name) throws IOException 

Source Link

Document

Opens the given file for reading, assuming "UTF8".

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 .j  a  v  a2s  . c  o  m
    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.XarFile.java

/**
 * @param file the XAR file/*from   ww  w .  j  a  v  a 2 s.  c  om*/
 * @param xarPackage the descriptor of the XAR package
 * @throws XarException when failing parse the file (for example if it's not a valid XAR package)
 * @throws IOException when failing to read file
 */
public XarFile(File file, XarPackage xarPackage) throws XarException, IOException {
    this.file = file;
    this.zipFile = new ZipFile(file);
    this.xarPackage = xarPackage != null ? xarPackage : new XarPackage(this.zipFile);
}

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

/**
 * @param file the XAR file/*from  ww  w. j av  a  2  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.zuinnote.hadoop.office.format.common.writer.msexcel.internal.EncryptedZipEntrySource.java

public void setInputStream(InputStream is) throws IOException {
    this.tmpFile = TempFile.createTempFile("hadoopoffice-protected", ".zip");

    ZipArchiveInputStream zis = new ZipArchiveInputStream(is);
    FileOutputStream fos = new FileOutputStream(tmpFile);
    ZipArchiveOutputStream zos = new ZipArchiveOutputStream(fos);
    ZipArchiveEntry ze;//from  w w w .j a v a2  s  . c o  m
    while ((ze = (ZipArchiveEntry) zis.getNextEntry()) != null) {
        // rewrite zip entries to match the size of the encrypted data (with padding)
        ZipArchiveEntry zeNew = new ZipArchiveEntry(ze.getName());
        zeNew.setComment(ze.getComment());
        zeNew.setExtra(ze.getExtra());
        zeNew.setTime(ze.getTime());
        zos.putArchiveEntry(zeNew);
        FilterOutputStream fos2 = new FilterOutputStream(zos) {
            // do not close underlyzing ZipOutputStream
            @Override
            public void close() {
            }
        };
        OutputStream nos;
        if (this.ciEncoder != null) { // encrypt if needed
            nos = new CipherOutputStream(fos2, this.ciEncoder);
        } else { // do not encrypt
            nos = fos2;
        }
        IOUtils.copy(zis, nos);
        nos.close();
        if (fos2 != null) {
            fos2.close();
        }
        zos.closeArchiveEntry();

    }
    zos.close();
    fos.close();
    zis.close();
    IOUtils.closeQuietly(is);
    this.zipFile = new ZipFile(this.tmpFile);

}

From source file:stroom.util.zip.StroomZipFile.java

private ZipFile getZipFile() throws IOException {
    if (zipFile == null) {
        this.zipFile = new ZipFile(file);
    }/* ww  w. j  a  v a2 s . c  o m*/
    return zipFile;
}

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

private void extractZipFileContents(File file) {
    Enumeration entries;/*from  w ww .  j a va  2 s .  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());
    }
}