Example usage for org.apache.commons.compress.archivers ArchiveStreamFactory createArchiveInputStream

List of usage examples for org.apache.commons.compress.archivers ArchiveStreamFactory createArchiveInputStream

Introduction

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

Prototype

public ArchiveInputStream createArchiveInputStream(final String archiverName, final InputStream in)
        throws ArchiveException 

Source Link

Document

Create an archive input stream from an archiver name and an input stream.

Usage

From source file:cpcc.vvrte.services.VirtualVehicleMigratorTest.java

private void verifyChunk(Object[] params, ArchiveStreamFactory factory, int chunkNumber, byte[] chunk)
        throws ArchiveException, IOException {
    ByteArrayInputStream bis = new ByteArrayInputStream(chunk);
    ArchiveInputStream inStream = factory.createArchiveInputStream("tar", bis);

    Object[] chunkParams = (Object[]) params[chunkNumber];

    int entryNumber = 0;
    for (TarArchiveEntry entry = readEntry(inStream); entry != null; entry = readEntry(
            inStream), ++entryNumber) {//from w  w  w  . j a  v  a 2s  . c  o  m
        byte[] content = IOUtils.toByteArray(inStream); // readContent(inStream);
        // System.out.print("Content: '");
        // System.out.write(content);
        // System.out.print("'");

        assertThat(chunkParams).describedAs("Chunk parameter").isNotNull();
        assertThat(((Object[]) chunkParams).length).describedAs("Chunk parameter length")
                .isGreaterThan(entryNumber);

        Object[] entryParams = (Object[]) chunkParams[entryNumber];

        String name = (String) entryParams[0];
        int length = (Integer) entryParams[1];
        int storageId = (Integer) entryParams[2];
        int chunkId = (Integer) entryParams[3];
        String userName = (String) entryParams[4];
        String groupName = (String) entryParams[5];

        assertThat(entry.getName()).overridingErrorMessage("Expected name %s but was %s in chunk %d", name,
                entry.getName(), chunkNumber).isNotNull().isEqualTo(name);
        assertThat(entry.getSize()).overridingErrorMessage("Expected size %d but was %d in chunk %d", length,
                entry.getSize(), chunkNumber).isNotNull().isEqualTo(length);
        assertThat(entry.getLongUserId()).isNotNull().isEqualTo(storageId);
        assertThat(entry.getLongGroupId()).isNotNull().isEqualTo(chunkId);
        assertThat(entry.getUserName()).isNotNull().isEqualTo(userName);
        assertThat(entry.getGroupName()).isNotNull().isEqualTo(groupName);
        assertThat(content.length).isEqualTo(length);
    }

    inStream.close();
    bis.close();
}

From source file:cpcc.vvrte.services.TarArchiveDemo.java

@Test
public void shouldWriteTarFile() throws IOException, ArchiveException {
    byte[] c1 = "content1\n".getBytes("UTF-8");
    byte[] c2 = "content2 text\n".getBytes("UTF-8");

    Date t1 = new Date(1000L * (System.currentTimeMillis() / 1000L));
    Date t2 = new Date(t1.getTime() - 30000);

    FileOutputStream fos = new FileOutputStream("bugger1.tar");

    ArchiveStreamFactory factory = new ArchiveStreamFactory("UTF-8");
    ArchiveOutputStream outStream = factory.createArchiveOutputStream("tar", fos);

    TarArchiveEntry archiveEntry1 = new TarArchiveEntry("entry1");
    archiveEntry1.setModTime(t1);//w w  w  .  j  a  va2 s  .c  o m
    archiveEntry1.setSize(c1.length);
    archiveEntry1.setIds(STORAGE_ID_ONE, CHUNK_ID_ONE);
    archiveEntry1.setNames(USER_NAME_ONE, GROUP_NAME_ONE);

    outStream.putArchiveEntry(archiveEntry1);
    outStream.write(c1);
    outStream.closeArchiveEntry();

    TarArchiveEntry archiveEntry2 = new TarArchiveEntry("data/entry2");
    archiveEntry2.setModTime(t2);
    archiveEntry2.setSize(c2.length);
    archiveEntry2.setIds(STORAGE_ID_TWO, CHUNK_ID_TWO);
    archiveEntry2.setNames(USER_NAME_TWO, GROUP_NAME_TWO);

    outStream.putArchiveEntry(archiveEntry2);
    outStream.write(c2);
    outStream.closeArchiveEntry();

    outStream.close();

    FileInputStream fis = new FileInputStream("bugger1.tar");
    ArchiveInputStream inStream = factory.createArchiveInputStream("tar", fis);

    TarArchiveEntry entry1 = (TarArchiveEntry) inStream.getNextEntry();
    assertThat(entry1.getModTime()).isEqualTo(t1);
    assertThat(entry1.getSize()).isEqualTo(c1.length);
    assertThat(entry1.getLongUserId()).isEqualTo(STORAGE_ID_ONE);
    assertThat(entry1.getLongGroupId()).isEqualTo(CHUNK_ID_ONE);
    assertThat(entry1.getUserName()).isEqualTo(USER_NAME_ONE);
    assertThat(entry1.getGroupName()).isEqualTo(GROUP_NAME_ONE);
    ByteArrayOutputStream b1 = new ByteArrayOutputStream();
    IOUtils.copy(inStream, b1);
    b1.close();
    assertThat(b1.toByteArray().length).isEqualTo(c1.length);
    assertThat(b1.toByteArray()).isEqualTo(c1);

    TarArchiveEntry entry2 = (TarArchiveEntry) inStream.getNextEntry();
    assertThat(entry2.getModTime()).isEqualTo(t2);
    assertThat(entry2.getSize()).isEqualTo(c2.length);
    assertThat(entry2.getLongUserId()).isEqualTo(STORAGE_ID_TWO);
    assertThat(entry2.getLongGroupId()).isEqualTo(CHUNK_ID_TWO);
    assertThat(entry2.getUserName()).isEqualTo(USER_NAME_TWO);
    assertThat(entry2.getGroupName()).isEqualTo(GROUP_NAME_TWO);
    ByteArrayOutputStream b2 = new ByteArrayOutputStream();
    IOUtils.copy(inStream, b2);
    b2.close();
    assertThat(b2.toByteArray().length).isEqualTo(c2.length);
    assertThat(b2.toByteArray()).isEqualTo(c2);

    TarArchiveEntry entry3 = (TarArchiveEntry) inStream.getNextEntry();
    assertThat(entry3).isNull();

    inStream.close();
}

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

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

    String sdkToken = sdkProfile.getToken();
    Integer configurationSchemaVersion = sdkProfile.getConfigurationSchemaVersion();
    final Integer profileSchemaVersion = sdkProfile.getProfileSchemaVersion();
    Integer notificationSchemaVersion = sdkProfile.getNotificationSchemaVersion();
    Integer logSchemaVersion = sdkProfile.getLogSchemaVersion();
    String defaultVerifierToken = sdkProfile.getDefaultVerifierToken();

    String sdkTemplateLocation = Environment.getServerHomeDir() + "/" + CPP_SDK_DIR + "/" + CPP_SDK_PREFIX
            + buildVersion + ".tar.gz";

    LOG.debug("Lookup Java 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> cppSources = new ArrayList<>();

    // TODO: remove all version fields and add single sdkToken field
    // create entry for default properties
    TarArchiveEntry entry = new TarArchiveEntry(SDK_DEFAULTS_PATH);
    byte[] data = generateKaaDefaults(bootstrapNodes, sdkToken, defaultConfigurationData, defaultVerifierToken);

    entry.setSize(data.length);//  w  w  w .ja  v a  2  s .c  om
    TarEntryData tarEntry = new TarEntryData(entry, data);
    cppSources.add(tarEntry);

    Map<String, String> profileVars = new HashMap<>();
    profileVars.put(SDK_PROFILE_VERSION_VAR, profileSchemaVersion.toString());
    cppSources.addAll(processFeatureSchema(profileSchemaBody, PROFILE_SCHEMA_AVRO_SRC,
            PROFILE_DEFINITIONS_TEMPLATE, PROFILE_DEFINITIONS_PATH, profileVars));

    cppSources.addAll(processFeatureSchema(notificationSchemaBody, NOTIFICATION_SCHEMA_AVRO_SRC,
            NOTIFICATION_DEFINITIONS_TEMPLATE, NOTIFICATION_DEFINITIONS_PATH, null));
    cppSources.addAll(processFeatureSchema(logSchemaBody, LOG_SCHEMA_AVRO_SRC, LOG_DEFINITIONS_TEMPLATE,
            LOG_DEFINITIONS_PATH, null));
    cppSources.addAll(processFeatureSchema(configurationBaseSchema, CONFIGURATION_SCHEMA_AVRO_SRC,
            CONFIGURATION_DEFINITIONS_TEMPLATE, CONFIGURATION_DEFINITIONS_PATH, null));

    if (eventFamilies != null && !eventFamilies.isEmpty()) {
        cppSources.addAll(CppEventSourcesGenerator.generateEventSources(eventFamilies));
    }

    for (TarEntryData entryData : cppSources) {
        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 = CPP_SDK_PREFIX + sdkProfile.getToken() + ".tar.gz";

    byte[] sdkData = sdkOutput.toByteArray();

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

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

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

    String sdkToken = sdkProfile.getToken();
    Integer profileSchemaVersion = sdkProfile.getProfileSchemaVersion();
    String defaultVerifierToken = sdkProfile.getDefaultVerifierToken();

    String sdkTemplateLocation = Environment.getServerHomeDir() + "/" + C_SDK_DIR + "/" + C_SDK_PREFIX
            + buildVersion + ".tar.gz";

    LOG.debug("Lookup 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> sources = new ArrayList<>();

    if (!StringUtils.isBlank(profileSchemaBody)) {
        sources.addAll(generateProfileSources(profileSchemaBody));
    }/*from   w w w .  j  a  v a2  s  . co m*/

    if (!StringUtils.isBlank(logSchemaBody)) {
        sources.addAll(generateLogSources(logSchemaBody));
    }

    if (!StringUtils.isBlank(configurationBaseSchemaBody)) {
        sources.addAll(generateConfigurationSources(configurationBaseSchemaBody));
    }

    if (!StringUtils.isBlank(notificationSchemaBody)) {
        sources.addAll(generateNotificationSources(notificationSchemaBody));
    }

    if (eventFamilies != null && !eventFamilies.isEmpty()) {
        sources.addAll(CEventSourcesGenerator.generateEventSources(eventFamilies));
    }

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

    ArchiveEntry archiveEntry;
    while ((archiveEntry = templateArchive.getNextEntry()) != null) {
        if (!archiveEntry.isDirectory()) {
            if (archiveEntry.getName().equals(KAA_DEFAULTS_HEADER)) {
                // TODO: eliminate schema versions and substitute them for a single sdkToken
                byte[] kaaDefaultsData = generateKaaDefaults(bootstrapNodes, sdkToken, profileSchemaVersion,
                        configurationProtocolSchemaBody, defaultConfigurationData, defaultVerifierToken);

                TarArchiveEntry kaaDefaultsEntry = new TarArchiveEntry(KAA_DEFAULTS_HEADER);
                kaaDefaultsEntry.setSize(kaaDefaultsData.length);
                sdkFile.putArchiveEntry(kaaDefaultsEntry);
                sdkFile.write(kaaDefaultsData);
            } else if (archiveEntry.getName().equals(KAA_CMAKEGEN)) {
                // Ignore duplicate source names
                List<String> sourceNames = new LinkedList<>();
                for (TarEntryData sourceEntry : sources) {
                    String fileName = sourceEntry.getEntry().getName();
                    if (fileName.endsWith(C_SOURCE_SUFFIX) && !sourceNames.contains(fileName)) {
                        sourceNames.add(fileName);
                    }
                }

                VelocityContext context = new VelocityContext();
                context.put("sourceNames", sourceNames);
                String sourceData = generateSourceFromTemplate(TEMPLATE_DIR + File.separator + "CMakeGen.vm",
                        context);

                TarArchiveEntry kaaCMakeEntry = new TarArchiveEntry(KAA_CMAKEGEN);
                kaaCMakeEntry.setSize(sourceData.length());
                sdkFile.putArchiveEntry(kaaCMakeEntry);
                sdkFile.write(sourceData.getBytes());
            } else 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 = C_SDK_PREFIX + sdkProfile.getToken() + ".tar.gz";

    FileData sdk = new FileData();
    sdk.setFileName(sdkFileName);
    sdk.setFileData(sdkOutput.toByteArray());

    return sdk;
}

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));
    }/*from  w  ww.ja  va  2  s.c  o  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.sonatype.nexus.repository.r.internal.RDescriptionUtils.java

private static Map<String, String> extractMetadataFromArchive(final String archiveType, final InputStream is) {
    final ArchiveStreamFactory archiveFactory = new ArchiveStreamFactory();
    try (ArchiveInputStream ais = archiveFactory.createArchiveInputStream(archiveType, is)) {
        ArchiveEntry entry = ais.getNextEntry();
        while (entry != null) {
            if (!entry.isDirectory() && DESCRIPTION_FILE_PATTERN.matcher(entry.getName()).matches()) {
                return parseDescriptionFile(ais);
            }/*from   w w w  .j av  a 2s  .  co  m*/
            entry = ais.getNextEntry();
        }
    } catch (ArchiveException | IOException e) {
        throw new RException(null, e);
    }
    throw new IllegalStateException("No metadata file found");
}

From source file:org.voyanttools.trombone.input.expand.ArchiveExpander.java

public List<StoredDocumentSource> getExpandedStoredDocumentSources(StoredDocumentSource storedDocumentSource)
        throws IOException {

    // first try to see if we've been here already
    String id = storedDocumentSource.getId();
    List<StoredDocumentSource> archivedStoredDocumentSources = storedDocumentSourceStorage
            .getMultipleExpandedStoredDocumentSources(id);
    if (archivedStoredDocumentSources != null && archivedStoredDocumentSources.isEmpty() == false) {
        return archivedStoredDocumentSources;
    }/*from www . ja v  a  2 s.  co  m*/

    InputStream inputStream = null;
    try {
        ArchiveStreamFactory archiveStreamFactory = new ArchiveStreamFactory();
        inputStream = storedDocumentSourceStorage
                .getStoredDocumentSourceInputStream(storedDocumentSource.getId());
        BufferedInputStream bis = new BufferedInputStream(inputStream);

        String filename = storedDocumentSource.getMetadata().getLocation();
        ArchiveInputStream archiveInputStream;

        if (filename.toLowerCase().endsWith("tgz") || filename.toLowerCase().endsWith("tar.gz")) { // decompress and then untar
            archiveInputStream = archiveStreamFactory.createArchiveInputStream(ArchiveStreamFactory.TAR,
                    new GZIPInputStream(bis));
        } else if (filename.toLowerCase().endsWith("tbz2") || filename.toLowerCase().endsWith("tar.bz2")) { // decompress and then untar
            archiveInputStream = archiveStreamFactory.createArchiveInputStream(ArchiveStreamFactory.TAR,
                    new BZip2CompressorInputStream(bis));
        } else {
            archiveInputStream = archiveStreamFactory.createArchiveInputStream(bis);
        }
        archivedStoredDocumentSources = getExpandedDocumentSources(archiveInputStream, storedDocumentSource);
        storedDocumentSourceStorage.setMultipleExpandedStoredDocumentSources(storedDocumentSource.getId(),
                archivedStoredDocumentSources);
        return archivedStoredDocumentSources;
    } catch (ArchiveException e) {
        throw new IOException("A problem was encountered reading this archive: "
                + storedDocumentSource.getMetadata().getLocation(), e);
    } finally {
        if (inputStream != null) {
            inputStream.close();
        }
    }
}