Example usage for org.apache.commons.compress.archivers ArchiveEntry isDirectory

List of usage examples for org.apache.commons.compress.archivers ArchiveEntry isDirectory

Introduction

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

Prototype

public boolean isDirectory();

Source Link

Document

True if the entry refers to a directory

Usage

From source file:org.kaaproject.kaa.server.control.service.sdk.ObjCSdkGenerator.java

@Override
public FileData generateSdk(String buildVersion, List<BootstrapNodeInfo> bootstrapNodes,
        SdkProfileDto sdkProperties, String profileSchemaBody, String notificationSchemaBody,
        String configurationProtocolSchemaBody, String configurationBaseSchemaBody,
        byte[] defaultConfigurationData, List<EventFamilyMetadata> eventFamilies, String logSchemaBody)
        throws Exception {

    final String sdkToken = sdkProperties.getToken();
    final String defaultVerifierToken = sdkProperties.getDefaultVerifierToken();

    String sdkTemplateLocation = System.getProperty("server_home_dir") + "/" + SDK_TEMPLATE_DIR + SDK_PREFIX
            + buildVersion + ".tar.gz";

    LOG.debug("Lookup Objective C SDK template: {}", sdkTemplateLocation);

    CompressorStreamFactory csf = new CompressorStreamFactory();
    ArchiveStreamFactory asf = new ArchiveStreamFactory();

    CompressorInputStream cis = csf.createCompressorInputStream(CompressorStreamFactory.GZIP,
            new FileInputStream(sdkTemplateLocation));

    final ArchiveInputStream templateArchive = asf.createArchiveInputStream(ArchiveStreamFactory.TAR, cis);

    ByteArrayOutputStream sdkOutput = new ByteArrayOutputStream();
    CompressorOutputStream cos = csf.createCompressorOutputStream(CompressorStreamFactory.GZIP, sdkOutput);

    ArchiveOutputStream sdkFile = asf.createArchiveOutputStream(ArchiveStreamFactory.TAR, cos);

    Map<String, TarEntryData> replacementData = new HashMap<>();

    List<TarEntryData> objcSources = new ArrayList<>();

    if (StringUtils.isNotBlank(profileSchemaBody)) {
        LOG.debug("Generating profile schema");
        Schema profileSchema = new Schema.Parser().parse(profileSchemaBody);
        String profileClassName = PROFILE_NAMESPACE + profileSchema.getName();

        String profileCommonHeader = readResource(PROFILE_TEMPLATE_DIR + PROFILE_COMMON + _H + TEMPLATE_SUFFIX)
                .replaceAll(PROFILE_CLASS_VAR, profileClassName);

        objcSources.add(CommonSdkUtil.tarEntryForSources(profileCommonHeader,
                KAA_ROOT + PROFILE_DIR + PROFILE_COMMON + _H));

        String profileCommonSource = readResource(PROFILE_TEMPLATE_DIR + PROFILE_COMMON + _M + TEMPLATE_SUFFIX)
                .replaceAll(PROFILE_CLASS_VAR, profileClassName);

        objcSources.add(CommonSdkUtil.tarEntryForSources(profileCommonSource,
                KAA_ROOT + PROFILE_DIR + PROFILE_COMMON + _M));

        objcSources.addAll(generateSourcesFromSchema(profileSchema, PROFILE_GEN, PROFILE_NAMESPACE));
    }//  w w w.  j ava 2s  . co m

    String logClassName = "";
    if (StringUtils.isNotBlank(logSchemaBody)) {
        LOG.debug("Generating log schema");
        Schema logSchema = new Schema.Parser().parse(logSchemaBody);
        logClassName = LOGGING_NAMESPACE + logSchema.getName();

        String logRecordHeader = readResource(LOG_TEMPLATE_DIR + LOG_RECORD + _H + TEMPLATE_SUFFIX)
                .replaceAll(LOG_RECORD_CLASS_VAR, logClassName);

        objcSources
                .add(CommonSdkUtil.tarEntryForSources(logRecordHeader, KAA_ROOT + LOG_DIR + LOG_RECORD + _H));

        String logRecordSource = readResource(LOG_TEMPLATE_DIR + LOG_RECORD + _M + TEMPLATE_SUFFIX)
                .replaceAll(LOG_RECORD_CLASS_VAR, logClassName);

        objcSources
                .add(CommonSdkUtil.tarEntryForSources(logRecordSource, KAA_ROOT + LOG_DIR + LOG_RECORD + _M));

        String logCollector = readResource(LOG_TEMPLATE_DIR + LOG_COLLECTOR_INTERFACE + TEMPLATE_SUFFIX)
                .replaceAll(LOG_RECORD_CLASS_VAR, logClassName);

        objcSources.add(
                CommonSdkUtil.tarEntryForSources(logCollector, KAA_ROOT + LOG_DIR + LOG_COLLECTOR_INTERFACE));

        String logCollectorImplHeader = readResource(
                LOG_TEMPLATE_DIR + LOG_COLLECTOR_SOURCE + _H + TEMPLATE_SUFFIX).replaceAll(LOG_RECORD_CLASS_VAR,
                        logClassName);

        objcSources.add(CommonSdkUtil.tarEntryForSources(logCollectorImplHeader,
                KAA_ROOT + LOG_DIR + LOG_COLLECTOR_SOURCE + _H));

        String logCollectorImplSource = readResource(
                LOG_TEMPLATE_DIR + LOG_COLLECTOR_SOURCE + _M + TEMPLATE_SUFFIX).replaceAll(LOG_RECORD_CLASS_VAR,
                        logClassName);

        objcSources.add(CommonSdkUtil.tarEntryForSources(logCollectorImplSource,
                KAA_ROOT + LOG_DIR + LOG_COLLECTOR_SOURCE + _M));

        objcSources.addAll(generateSourcesFromSchema(logSchema, LOG_GEN, LOGGING_NAMESPACE));
    }

    String configurationClassName = "";
    if (StringUtils.isNotBlank(configurationBaseSchemaBody)) {
        LOG.debug("Generating configuration schema");
        Schema configurationSchema = new Schema.Parser().parse(configurationBaseSchemaBody);
        configurationClassName = CONFIGURATION_NAMESPACE + configurationSchema.getName();

        String configurationCommon = readResource(
                CONFIGURATION_TEMPLATE_DIR + CONFIGURATION_COMMON + TEMPLATE_SUFFIX)
                        .replaceAll(CONFIGURATION_CLASS_VAR, configurationClassName);

        objcSources.add(CommonSdkUtil.tarEntryForSources(configurationCommon,
                KAA_ROOT + CONFIGURATION_DIR + CONFIGURATION_COMMON));

        String cfManagerImplHeader = readResource(
                CONFIGURATION_TEMPLATE_DIR + CONFIGURATION_MANAGER_IMPL + _H + TEMPLATE_SUFFIX)
                        .replaceAll(CONFIGURATION_CLASS_VAR, configurationClassName);

        objcSources.add(CommonSdkUtil.tarEntryForSources(cfManagerImplHeader,
                KAA_ROOT + CONFIGURATION_DIR + CONFIGURATION_MANAGER_IMPL + _H));

        String cfManagerImplSource = readResource(
                CONFIGURATION_TEMPLATE_DIR + CONFIGURATION_MANAGER_IMPL + _M + TEMPLATE_SUFFIX)
                        .replaceAll(CONFIGURATION_CLASS_VAR, configurationClassName);

        objcSources.add(CommonSdkUtil.tarEntryForSources(cfManagerImplSource,
                KAA_ROOT + CONFIGURATION_DIR + CONFIGURATION_MANAGER_IMPL + _M));

        String cfDeserializerHeader = readResource(
                CONFIGURATION_TEMPLATE_DIR + CONFIGURATION_DESERIALIZER + _H + TEMPLATE_SUFFIX)
                        .replaceAll(CONFIGURATION_CLASS_VAR, configurationClassName);

        objcSources.add(CommonSdkUtil.tarEntryForSources(cfDeserializerHeader,
                KAA_ROOT + CONFIGURATION_DIR + CONFIGURATION_DESERIALIZER + _H));

        String cfDeserializerSource = readResource(
                CONFIGURATION_TEMPLATE_DIR + CONFIGURATION_DESERIALIZER + _M + TEMPLATE_SUFFIX)
                        .replaceAll(CONFIGURATION_CLASS_VAR, configurationClassName);

        objcSources.add(CommonSdkUtil.tarEntryForSources(cfDeserializerSource,
                KAA_ROOT + CONFIGURATION_DIR + CONFIGURATION_DESERIALIZER + _M));

        objcSources.addAll(
                generateSourcesFromSchema(configurationSchema, CONFIGURATION_GEN, CONFIGURATION_NAMESPACE));
    }

    if (StringUtils.isNotBlank(notificationSchemaBody)) {
        LOG.debug("Generating notification schema");
        Schema notificationSchema = new Schema.Parser().parse(notificationSchemaBody);
        String notificationClassName = NOTIFICATION_NAMESPACE + notificationSchema.getName();

        String nfCommonHeader = readResource(
                NOTIFICATION_TEMPLATE_DIR + NOTIFICATION_COMMON + _H + TEMPLATE_SUFFIX)
                        .replaceAll(NOTIFICATION_CLASS_VAR, notificationClassName);

        objcSources.add(CommonSdkUtil.tarEntryForSources(nfCommonHeader,
                KAA_ROOT + NOTIFICATION_DIR + NOTIFICATION_COMMON + _H));

        String nfCommonSource = readResource(
                NOTIFICATION_TEMPLATE_DIR + NOTIFICATION_COMMON + _M + TEMPLATE_SUFFIX)
                        .replaceAll(NOTIFICATION_CLASS_VAR, notificationClassName);

        objcSources.add(CommonSdkUtil.tarEntryForSources(nfCommonSource,
                KAA_ROOT + NOTIFICATION_DIR + NOTIFICATION_COMMON + _M));

        objcSources.addAll(
                generateSourcesFromSchema(notificationSchema, NOTIFICATION_GEN, NOTIFICATION_NAMESPACE));
    }

    if (eventFamilies != null && !eventFamilies.isEmpty()) {
        objcSources.addAll(ObjCEventClassesGenerator.generateEventSources(eventFamilies));
    }

    String kaaClient = readResource(SDK_TEMPLATE_DIR + KAA_CLIENT + TEMPLATE_SUFFIX)
            .replaceAll(LOG_RECORD_CLASS_VAR, logClassName)
            .replaceAll(CONFIGURATION_CLASS_VAR, configurationClassName);

    objcSources.add(CommonSdkUtil.tarEntryForSources(kaaClient, KAA_ROOT + KAA_CLIENT));

    String baseKaaClientHeader = readResource(SDK_TEMPLATE_DIR + BASE_KAA_CLIENT + _H + TEMPLATE_SUFFIX)
            .replaceAll(LOG_RECORD_CLASS_VAR, logClassName)
            .replaceAll(CONFIGURATION_CLASS_VAR, configurationClassName);

    objcSources.add(CommonSdkUtil.tarEntryForSources(baseKaaClientHeader, KAA_ROOT + BASE_KAA_CLIENT + _H));
    String baseKaaClientSource = readResource(SDK_TEMPLATE_DIR + BASE_KAA_CLIENT + _M + TEMPLATE_SUFFIX)
            .replaceAll(LOG_RECORD_CLASS_VAR, logClassName)
            .replaceAll(CONFIGURATION_CLASS_VAR, configurationClassName);

    objcSources.add(CommonSdkUtil.tarEntryForSources(baseKaaClientSource, KAA_ROOT + BASE_KAA_CLIENT + _M));

    String tokenVerifier = defaultVerifierToken == null ? "nil" : "@\"" + defaultVerifierToken + "\"";

    String tokenVerifierSource = readResource(EVENT_TEMPLATE_DIR + USER_VERIFIER_CONSTANTS + TEMPLATE_SUFFIX)
            .replaceAll(DEFAULT_USER_VERIFIER_TOKEN_VAR, tokenVerifier);

    objcSources.add(CommonSdkUtil.tarEntryForSources(tokenVerifierSource,
            KAA_ROOT + EVENT_DIR + USER_VERIFIER_CONSTANTS));

    String kaaDefaultsTemplate = readResource(SDK_TEMPLATE_DIR + KAA_DEFAULTS + TEMPLATE_SUFFIX);
    objcSources.add(generateKaaDefaults(kaaDefaultsTemplate, bootstrapNodes, sdkToken,
            configurationProtocolSchemaBody, defaultConfigurationData, sdkProperties));

    for (TarEntryData entryData : objcSources) {
        replacementData.put(entryData.getEntry().getName(), entryData);
    }

    ArchiveEntry archiveEntry;
    while ((archiveEntry = templateArchive.getNextEntry()) != null) {
        if (!archiveEntry.isDirectory()) {
            if (replacementData.containsKey(archiveEntry.getName())) {
                TarEntryData entryData = replacementData.remove(archiveEntry.getName());
                sdkFile.putArchiveEntry(entryData.getEntry());
                sdkFile.write(entryData.getData());
            } else {
                sdkFile.putArchiveEntry(archiveEntry);
                IOUtils.copy(templateArchive, sdkFile);
            }
        } else {
            sdkFile.putArchiveEntry(archiveEntry);
        }
        sdkFile.closeArchiveEntry();
    }

    templateArchive.close();

    for (String entryName : replacementData.keySet()) {
        TarEntryData entryData = replacementData.get(entryName);
        sdkFile.putArchiveEntry(entryData.getEntry());
        sdkFile.write(entryData.getData());
        sdkFile.closeArchiveEntry();
    }

    sdkFile.finish();
    sdkFile.close();

    String sdkFileName = SDK_PREFIX + sdkProperties.getToken() + ".tar.gz";

    byte[] sdkData = sdkOutput.toByteArray();

    FileData sdk = new FileData();
    sdk.setFileName(sdkFileName);
    sdk.setFileData(sdkData);
    return sdk;
}

From source file:org.mcisb.subliminal.SubliminalUtils.java

/**
 * //from ww w .  ja  v a  2  s .c  o m
 * @param url
 * @param destinationDirectory
 * @throws IOException
 */
public static void untar(final URL url, final File destinationDirectory) throws IOException {
    if (!destinationDirectory.exists()) {
        if (!destinationDirectory.mkdir()) {
            throw new IOException();
        }
    }

    TarArchiveInputStream is = null;

    try {
        is = new TarArchiveInputStream(new GZIPInputStream(url.openStream()));
        ArchiveEntry tarEntry = null;

        while ((tarEntry = is.getNextEntry()) != null) {
            final File destination = new File(destinationDirectory, tarEntry.getName());

            if (tarEntry.isDirectory()) {
                if (!destination.mkdir()) {
                    // Take no action.
                    // throw new IOException();
                }
            } else {
                try (final OutputStream os = new FileOutputStream(destination)) {
                    new StreamReader(is, os).read();
                }
            }
        }
    } catch (IOException e) {
        if (!destinationDirectory.delete()) {
            throw new IOException();
        }
        throw e;
    } finally {
        if (is != null) {
            is.close();
        }
    }
}

From source file:org.mcisb.util.io.TarUtils.java

/**
 * /*  ww  w .  ja  va2s.c om*/
 * @param tarInputStream
 * @param destinationDirectory
 * @throws IOException
 */
public static void untar(final InputStream tarInputStream, final File destinationDirectory) throws IOException {
    if (!destinationDirectory.exists()) {
        if (!destinationDirectory.mkdir()) {
            throw new IOException();
        }
    }

    TarArchiveInputStream is = null;

    try {
        is = new TarArchiveInputStream(new GZIPInputStream(tarInputStream));
        ArchiveEntry tarEntry = null;

        while ((tarEntry = is.getNextEntry()) != null) {
            final File destination = new File(destinationDirectory, tarEntry.getName());

            if (tarEntry.isDirectory()) {
                if (!destination.mkdir()) {
                    // Take no action.
                    // throw new IOException();
                }
            } else {
                try (final OutputStream os = new FileOutputStream(destination)) {
                    new StreamReader(is, os).read();
                }
            }
        }
    } catch (IOException e) {
        if (!destinationDirectory.delete()) {
            throw new IOException();
        }
        throw e;
    } finally {
        if (is != null) {
            is.close();
        }
    }
}

From source file:org.metaservice.core.file.archives.ArchiveExtractionProcessor.java

private void processArchiveExtraction(ArchiveInputStream archiveInputStream, URI uri,
        RepositoryConnection repositoryConnection)
        throws IOException, FileProcessingException, RepositoryException {

    ValueFactory valueFactory = repositoryConnection.getValueFactory();
    ArchiveEntry archiveEntry;
    while ((archiveEntry = archiveInputStream.getNextEntry()) != null) {
        //ignore directories
        if (archiveEntry.isDirectory()) {
            continue;
        }/* w w w.j a v a  2 s.  co m*/
        FileIdentifier identifier = FileUriUtils.storeFile(archiveInputStream);
        URI entryURI = valueFactory.createURI(uri.toString() + "/" + archiveEntry.getName());
        FileRetrievalProcessor.addFile(repositoryConnection, identifier);
        URI contentURI = identifier.getUri();
        repositoryConnection.add(contentURI, DCTERMS.IS_PART_OF, uri);
        repositoryConnection.add(entryURI, DCTERMS.IS_FORMAT_OF, contentURI);
    }
}

From source file:org.mitre.xtext.converters.ArchiveNavigator.java

private boolean filterEntry(ArchiveEntry E) {
    if (E.isDirectory()) {
        return true;
    }//  w w w  .j a v  a2s.  co m
    if (filter.filterOutFile(E.getName())) {
        return true;
    }
    return false;
}

From source file:org.ng200.openolympus.controller.task.TaskFilesystemManipulatingController.java

@PreAuthorize(SecurityExpressionConstants.IS_ADMIN)
private void extractZipFile(final InputStream zipFile, final Path destination) throws Exception {
    try (ArchiveInputStream input = new ArchiveStreamFactory()
            .createArchiveInputStream(new BufferedInputStream(zipFile))) {
        ArchiveEntry entry;
        while ((entry = input.getNextEntry()) != null) {
            final Path dest = destination.resolve(entry.getName());
            if (entry.isDirectory()) {
                FileAccess.createDirectories(dest);
            } else {
                FileAccess.createDirectories(dest.getParent());
                FileAccess.createFile(dest);
                Files.copy(input, dest, StandardCopyOption.REPLACE_EXISTING);
            }/*from ww w .ja v  a 2 s  .c om*/
        }
    }
}

From source file:org.obm.push.arquillian.ManagedTomcatInstaller.java

private static void uncompressTarFile(File parent, File inputFile)
        throws FileNotFoundException, IOException, ArchiveException {
    FileInputStream inputStream = new FileInputStream(inputFile);
    ArchiveInputStream archiveInputStream = null;
    try {//from  www .j a  v a2s .c  om
        archiveInputStream = new ArchiveStreamFactory().createArchiveInputStream(ArchiveStreamFactory.TAR,
                inputStream);

        ArchiveEntry entry = null;
        while ((entry = archiveInputStream.getNextEntry()) != null) {
            File outputFile = new File(parent, entry.getName());
            if (entry.isDirectory()) {
                if (!outputFile.exists()) {
                    if (!outputFile.mkdir()) {
                        throw new IOException(
                                String.format("Unable to create directory %s", outputFile.getAbsolutePath()));
                    }
                }
            } else {
                FileOutputStream outputFileStream = new FileOutputStream(outputFile);
                try {
                    IOUtils.copy(archiveInputStream, outputFileStream);
                } finally {
                    outputFileStream.close();
                }
            }
        }
    } finally {
        try {
            if (archiveInputStream != null) {
                archiveInputStream.close();
            }
        } finally {
            inputStream.close();
        }
    }
}

From source file:org.openengsb.openengsbplugin.Provision.java

private void extract(ArchiveInputStream is, File targetDir) throws IOException {
    try {// ww  w.  j  a va 2 s  . co m
        if (!targetDir.exists()) {
            targetDir.mkdirs();
            ArchiveEntry entry = is.getNextEntry();
            while (entry != null) {
                String name = entry.getName();
                name = name.substring(name.indexOf("/") + 1);
                File file = new File(targetDir, name);
                if (entry.isDirectory()) {
                    file.mkdirs();
                } else {
                    file.getParentFile().mkdirs();
                    OutputStream os = new FileOutputStream(file);
                    try {
                        IOUtils.copy(is, os);
                    } finally {
                        IOUtils.closeQuietly(os);
                    }
                }
                entry = is.getNextEntry();
            }
        }
    } finally {
        is.close();
    }
}

From source file:org.owasp.dependencycheck.analyzer.ArchiveAnalyzer.java

/**
 * Extracts files from an archive.// ww  w .ja  v  a  2  s.com
 *
 * @param input the archive to extract files from
 * @param destination the location to write the files too
 * @param engine the dependency-check engine
 * @throws ArchiveExtractionException thrown if there is an exception extracting files from the archive
 */
private void extractArchive(ArchiveInputStream input, File destination, Engine engine)
        throws ArchiveExtractionException {
    ArchiveEntry entry;
    try {
        while ((entry = input.getNextEntry()) != null) {
            if (entry.isDirectory()) {
                final File d = new File(destination, entry.getName());
                if (!d.exists()) {
                    if (!d.mkdirs()) {
                        final String msg = String.format("Unable to create directory '%s'.",
                                d.getAbsolutePath());
                        throw new AnalysisException(msg);
                    }
                }
            } else {
                final File file = new File(destination, entry.getName());
                final String ext = FileUtils.getFileExtension(file.getName());
                if (engine.supportsExtension(ext)) {
                    BufferedOutputStream bos = null;
                    FileOutputStream fos;
                    try {
                        final File parent = file.getParentFile();
                        if (!parent.isDirectory()) {
                            if (!parent.mkdirs()) {
                                final String msg = String.format("Unable to build directory '%s'.",
                                        parent.getAbsolutePath());
                                throw new AnalysisException(msg);
                            }
                        }
                        fos = new FileOutputStream(file);
                        bos = new BufferedOutputStream(fos, BUFFER_SIZE);
                        int count;
                        final byte data[] = new byte[BUFFER_SIZE];
                        while ((count = input.read(data, 0, BUFFER_SIZE)) != -1) {
                            bos.write(data, 0, count);
                        }
                        bos.flush();
                    } catch (FileNotFoundException ex) {
                        LOGGER.log(Level.FINE, null, ex);
                        final String msg = String.format("Unable to find file '%s'.", file.getName());
                        throw new AnalysisException(msg, ex);
                    } catch (IOException ex) {
                        LOGGER.log(Level.FINE, null, ex);
                        final String msg = String.format("IO Exception while parsing file '%s'.",
                                file.getName());
                        throw new AnalysisException(msg, ex);
                    } finally {
                        if (bos != null) {
                            try {
                                bos.close();
                            } catch (IOException ex) {
                                LOGGER.log(Level.FINEST, null, ex);
                            }
                        }
                    }
                }
            }
        }
    } catch (IOException ex) {
        throw new ArchiveExtractionException(ex);
    } catch (Throwable ex) {
        throw new ArchiveExtractionException(ex);
    } finally {
        if (input != null) {
            try {
                input.close();
            } catch (IOException ex) {
                LOGGER.log(Level.FINEST, null, ex);
            }
        }
    }
}

From source file:org.owasp.dependencycheck.utils.ExtractionUtil.java

/**
 * Extracts files from an archive./*from  w ww .  j  a  v a2  s . c om*/
 *
 * @param input the archive to extract files from
 * @param destination the location to write the files too
 * @param filter determines which files get extracted
 * @throws ArchiveExtractionException thrown if there is an exception extracting files from the archive
 */
private static void extractArchive(ArchiveInputStream input, File destination, FilenameFilter filter)
        throws ArchiveExtractionException {
    ArchiveEntry entry;
    try {
        while ((entry = input.getNextEntry()) != null) {
            if (entry.isDirectory()) {
                final File dir = new File(destination, entry.getName());
                if (!dir.exists() && !dir.mkdirs()) {
                    final String msg = String.format("Unable to create directory '%s'.", dir.getAbsolutePath());
                    throw new AnalysisException(msg);
                }
            } else {
                extractFile(input, destination, filter, entry);
            }
        }
    } catch (IOException ex) {
        throw new ArchiveExtractionException(ex);
    } catch (Throwable ex) {
        throw new ArchiveExtractionException(ex);
    } finally {
        closeStream(input);
    }
}