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(File f, String encoding, boolean useUnicodeExtraFields) throws IOException 

Source Link

Document

Opens the given file for reading, assuming the specified encoding for file names.

Usage

From source file:com.amazonaws.codepipeline.jenkinsplugin.ExtractionTools.java

private static void extractZip(final File source, final File destination) throws IOException {
    try (final ZipFile zipFile = new ZipFile(source, StandardCharsets.UTF_8.name(), true)) {
        extractZipFile(destination, zipFile);
    }//www . jav  a  2  s.  com
}

From source file:org.alfresco.repo.action.executer.ImporterActionExecuter.java

/**
 * @see org.alfresco.repo.action.executer.ActionExecuter#execute(Action, NodeRef)
 *///from  w w w.  j a  v  a 2 s  .co  m
public void executeImpl(Action ruleAction, NodeRef actionedUponNodeRef) {
    if (this.nodeService.exists(actionedUponNodeRef) == true) {
        // The node being passed in should be an Alfresco content package
        ContentReader reader = this.contentService.getReader(actionedUponNodeRef, ContentModel.PROP_CONTENT);
        if (reader != null) {
            NodeRef importDest = (NodeRef) ruleAction.getParameterValue(PARAM_DESTINATION_FOLDER);
            if (MimetypeMap.MIMETYPE_ACP.equals(reader.getMimetype())) {
                // perform an import of an Alfresco ACP file (special format ZIP structure)
                File zipFile = null;
                try {
                    // unfortunately a ZIP file can not be read directly from an input stream so we have to create
                    // a temporary file first
                    zipFile = TempFileProvider.createTempFile(TEMP_FILE_PREFIX, TEMP_FILE_SUFFIX_ACP);
                    reader.getContent(zipFile);

                    ACPImportPackageHandler importHandler = new ACPImportPackageHandler(zipFile,
                            (String) ruleAction.getParameterValue(PARAM_ENCODING));

                    this.importerService.importView(importHandler, new Location(importDest), null, null);
                } finally {
                    // now the import is done, delete the temporary file
                    if (zipFile != null) {
                        zipFile.delete();
                    }
                }
            } else if (MimetypeMap.MIMETYPE_ZIP.equals(reader.getMimetype())) {
                // perform an import of a standard ZIP file
                ZipFile zipFile = null;
                File tempFile = null;
                try {
                    tempFile = TempFileProvider.createTempFile(TEMP_FILE_PREFIX, TEMP_FILE_SUFFIX_ACP);
                    reader.getContent(tempFile);
                    // NOTE: This encoding allows us to workaround bug:
                    //       http://bugs.sun.com/bugdatabase/view_bug.do;:WuuT?bug_id=4820807
                    // We also try to use the extra encoding information if present
                    String encoding = (String) ruleAction.getParameterValue(PARAM_ENCODING);
                    if (encoding == null) {
                        encoding = "UTF-8";
                    } else {
                        if (encoding.equalsIgnoreCase("default")) {
                            encoding = null;
                        }
                    }
                    zipFile = new ZipFile(tempFile, encoding, true);
                    // build a temp dir name based on the ID of the noderef we are importing
                    // also use the long life temp folder as large ZIP files can take a while
                    File alfTempDir = TempFileProvider.getLongLifeTempDir("import");
                    File tempDir = new File(
                            alfTempDir.getPath() + File.separatorChar + actionedUponNodeRef.getId());
                    try {
                        // TODO: improve this code to directly pipe the zip stream output into the repo objects - 
                        //       to remove the need to expand to the filesystem first?
                        extractFile(zipFile, tempDir.getPath());
                        importDirectory(tempDir.getPath(), importDest);
                    } finally {
                        deleteDir(tempDir);
                    }
                } catch (IOException ioErr) {
                    throw new AlfrescoRuntimeException("Failed to import ZIP file.", ioErr);
                } finally {
                    // now the import is done, delete the temporary file
                    if (tempFile != null) {
                        tempFile.delete();
                    }
                    if (zipFile != null) {
                        try {
                            zipFile.close();
                        } catch (IOException e) {
                            throw new AlfrescoRuntimeException("Failed to close zip package.", e);
                        }
                    }
                }
            }
        }
    }
}

From source file:org.apache.ant.compress.taskdefs.Unzip.java

protected void expandFile(FileUtils fileUtils, File srcF, File dir) {
    log("Expanding: " + srcF + " into " + dir, Project.MSG_INFO);
    ZipFile zf = null;//from w ww .  j a v a 2  s .  c  om
    FileNameMapper mapper = getMapper();
    if (!srcF.exists()) {
        throw new BuildException("Unable to expand " + srcF + " as the file does not exist", getLocation());
    }
    try {
        zf = new ZipFile(srcF, getEncoding(), true);
        boolean empty = true;
        Enumeration e = zf.getEntries();
        while (e.hasMoreElements()) {
            empty = false;
            ZipArchiveEntry ze = (ZipArchiveEntry) e.nextElement();
            if (getSkipUnreadableEntries() && !zf.canReadEntryData(ze)) {
                log(Messages.skippedIsUnreadable(ze));
                continue;
            }
            log("extracting " + ze.getName(), Project.MSG_DEBUG);
            InputStream is = null;
            try {
                extractFile(fileUtils, srcF, dir, is = zf.getInputStream(ze), ze.getName(),
                        new Date(ze.getTime()), ze.isDirectory(), mapper);
            } finally {
                FileUtils.close(is);
            }
        }
        if (empty && getFailOnEmptyArchive()) {
            throw new BuildException("archive '" + srcF + "' is empty");
        }
        log("expand complete", Project.MSG_VERBOSE);
    } catch (IOException ioe) {
        throw new BuildException("Error while expanding " + srcF.getPath() + "\n" + ioe.toString(), ioe);
    } finally {
        ZipFile.closeQuietly(zf);
    }
}