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

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

Introduction

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

Prototype

String TAR

To view the source code for org.apache.commons.compress.archivers ArchiveStreamFactory TAR.

Click Source Link

Document

Constant used to identify the TAR archive format.

Usage

From source file:ezbake.deployer.utilities.Utilities.java

public static void appendFilesInTarArchive(OutputStream output, byte[] currentArchive,
        Iterable<ArtifactDataEntry> filesToAdd) throws DeploymentException {
    ArchiveStreamFactory asf = new ArchiveStreamFactory();

    try (GZIPOutputStream gzs = new GZIPOutputStream(output)) {
        try (ArchiveOutputStream aos = asf.createArchiveOutputStream(ArchiveStreamFactory.TAR, gzs)) {
            try (GZIPInputStream gzip = new GZIPInputStream(new ByteArrayInputStream(currentArchive))) {
                try (TarArchiveInputStream tarInputStream = new TarArchiveInputStream(gzip)) {
                    TarArchiveEntry tarEntry = null;

                    while ((tarEntry = tarInputStream.getNextTarEntry()) != null) {
                        aos.putArchiveEntry(tarEntry);
                        IOUtils.copy(tarInputStream, aos);
                        aos.closeArchiveEntry();
                    }//from  w w w  .j a v a  2s.  c  o m
                }
            }

            for (ArtifactDataEntry entry : filesToAdd) {
                aos.putArchiveEntry(entry.getEntry());
                IOUtils.write(entry.getData(), aos);
                aos.closeArchiveEntry();
            }
        }
    } catch (ArchiveException e) {
        log.error(e.getMessage(), e);
        throw new DeploymentException(e.getMessage());
    } catch (IOException e) {
        log.error(e.getMessage(), e);
        throw new DeploymentException(e.getMessage());
    }
}

From source file:net.orpiske.ssps.common.archive.tbz.TbzArchive.java

public long unpack(final String source, final String destination) throws SspsArchiveException {
    File compressedFileSource = new File(source);

    String uncompressedArchiveFileName = destination + File.separator
            + replaceCompressedFileExtension(compressedFileSource.getName());

    File uncompressedArchiveFile = new File(uncompressedArchiveFileName);
    if (uncompressedArchiveFile.exists()) {
        if (!uncompressedArchiveFile.delete()) {
            throw new SspsArchiveException("A previously decompressed file exists: " + uncompressedArchiveFile);
        }//from w  w  w.jav  a2s. c  om
    }

    if (logger.isDebugEnabled()) {
        logger.debug("Unpacking " + source + " to " + uncompressedArchiveFile.getPath());
    } else {
        logger.info("Unpacking " + compressedFileSource.getName());
    }

    try {
        CompressedArchiveUtils.bzipDecompress(compressedFileSource, uncompressedArchiveFile);
    } catch (IOException e) {
        throw new SspsArchiveException("Unable to decompress archive file: I/O error", e);
    }

    File destinationDirectory = new File(destination);

    try {
        return TarArchiveUtils.unpack(uncompressedArchiveFile, destinationDirectory, ArchiveStreamFactory.TAR);
    } catch (SspsException e) {
        throw new SspsArchiveException("Unable to unpack file", e);
    } catch (ArchiveException e) {
        throw new SspsArchiveException("Unable to unpack file", e);
    } catch (IOException e) {
        throw new SspsArchiveException(
                "Unable to decompress archive file due to an I/O error: " + e.getMessage(), e);
    } finally {
        if (uncompressedArchiveFile.exists()) {
            if (!uncompressedArchiveFile.delete()) {
                uncompressedArchiveFile.deleteOnExit();
            }
        }
    }

}

From source file:net.orpiske.ssps.common.archive.tgz.TgzArchive.java

public long unpack(String source, String destination) throws SspsArchiveException {
    File compressedFileSource = new File(source);

    String uncompressedArchiveFileName = destination + File.separator
            + replaceCompressedFileExtension(compressedFileSource.getName());

    File uncompressedArchiveFile = new File(uncompressedArchiveFileName);
    if (uncompressedArchiveFile.exists()) {
        if (!uncompressedArchiveFile.delete()) {
            throw new SspsArchiveException("A previously decompressed file exists: " + uncompressedArchiveFile
                    + " and couldn't be deleted");
        }//from   w  ww  .  j  av a 2s  .co m
    }

    if (logger.isDebugEnabled()) {
        logger.debug("Unpacking " + source + " to " + uncompressedArchiveFile.getPath());
    } else {
        logger.info("Unpacking " + compressedFileSource.getName());
    }

    try {
        CompressedArchiveUtils.gzDecompress(compressedFileSource, uncompressedArchiveFile);
    } catch (IOException e) {
        throw new SspsArchiveException("Unable to decompress archive file: I/O error", e);
    }

    File destinationDirectory = new File(destination);

    try {
        return TarArchiveUtils.unpack(uncompressedArchiveFile, destinationDirectory, ArchiveStreamFactory.TAR);
    } catch (SspsException e) {
        throw new SspsArchiveException("Unable to unpack file", e);
    } catch (ArchiveException e) {
        throw new SspsArchiveException("Unable to unpack file", e);
    } catch (IOException e) {
        throw new SspsArchiveException("Unable to decompress archive file: I/O error", e);
    } finally {
        if (uncompressedArchiveFile.exists()) {
            if (!uncompressedArchiveFile.delete()) {
                uncompressedArchiveFile.deleteOnExit();
            }
        }
    }
}

From source file:net.sf.util.zip.FileNameUtil.java

/**
 *
 * @param fileName the file name//from w  w w .  ja v  a2 s .  c om
 * @return  Archive file type, as defined in ArchiveStreamFactory
 */
public static String getArchieveFileType(String fileName) {
    String s = fileName.toLowerCase();
    if (s.endsWith(".jar") || s.endsWith(".war") || s.endsWith(".ear"))
        return ArchiveStreamFactory.JAR;
    else if (s.endsWith(".tar"))
        return ArchiveStreamFactory.TAR;
    else if (s.endsWith(".zip"))
        return ArchiveStreamFactory.ZIP;

    return null;
}

From source file:ezbake.deployer.cli.commands.SSLCertsCommand.java

@Override
public void call() throws IOException, TException {
    String[] args = globalParameters.unparsedArgs;
    minExpectedArgs(2, args, this);
    String securityId = args[0];//from  w ww . j  a v  a  2 s.c  om
    String filePath = args[1];

    List<ArtifactDataEntry> certs = new ArrayList<>();
    EzSecurityRegistration.Client client = null;
    ThriftClientPool pool = poolSupplier.get();
    try {
        client = pool.getClient(EzSecurityRegistrationConstants.SERVICE_NAME,
                EzSecurityRegistration.Client.class);

        AppCerts s = client.getAppCerts(
                getSecurityToken(pool.getSecurityId(EzSecurityRegistrationConstants.SERVICE_NAME)), securityId);
        for (AppCerts._Fields fields : AppCerts._Fields.values()) {
            Object o = s.getFieldValue(fields);
            if (o instanceof byte[]) {
                String fieldName = fields.getFieldName().replace("_", ".");
                TarArchiveEntry tae = new TarArchiveEntry(
                        new File(new File(SSL_CONFIG_DIRECTORY, securityId), fieldName));
                certs.add(new ArtifactDataEntry(tae, (byte[]) o));
            }
        }

        ArchiveStreamFactory asf = new ArchiveStreamFactory();
        FileOutputStream fos = new FileOutputStream(filePath);
        GZIPOutputStream gzs = new GZIPOutputStream(fos);
        try (TarArchiveOutputStream aos = (TarArchiveOutputStream) asf
                .createArchiveOutputStream(ArchiveStreamFactory.TAR, gzs)) {
            aos.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);

            for (ArtifactDataEntry entry : certs) {
                aos.putArchiveEntry(entry.getEntry());
                IOUtils.write(entry.getData(), aos);
                aos.closeArchiveEntry();
            }
            aos.finish();
            gzs.finish();
        } catch (ArchiveException ex) {
            throw new DeploymentException(ex.getMessage());
        } finally {
            IOUtils.closeQuietly(fos);
        }
    } finally {
        pool.returnToPool(client);
    }
}

From source file:com.aliyun.odps.local.common.utils.ArchiveUtils.java

public static void unTar(File inFile, File untarDir) throws IOException, ArchiveException {

    final InputStream is = new FileInputStream(inFile);
    final TarArchiveInputStream in = (TarArchiveInputStream) new ArchiveStreamFactory()
            .createArchiveInputStream(ArchiveStreamFactory.TAR, is);
    TarArchiveEntry entry = null;// w  ww .  ja  v  a2 s .co  m
    untarDir.mkdirs();
    while ((entry = (TarArchiveEntry) in.getNextEntry()) != null) {
        byte[] content = new byte[(int) entry.getSize()];
        in.read(content);
        final File entryFile = new File(untarDir, entry.getName());
        if (entry.isDirectory() && !entryFile.exists()) {
            if (!entryFile.mkdirs()) {
                throw new IOException("Create directory failed: " + entryFile.getAbsolutePath());
            }
        } else {
            final OutputStream out = new FileOutputStream(entryFile);
            IOUtils.write(content, out);
            out.close();
        }
    }
    in.close();
}

From source file:consulo.cold.runner.execute.target.artifacts.Generator.java

public void buildDistributionInArchive(String distZip, @Nullable String jdkArchivePath, String path,
        String archiveOutType) throws Exception {
    myListener.info("Build: " + path);

    ArchiveStreamFactory factory = new ArchiveStreamFactory();

    final File fileZip = new File(myDistPath, distZip);

    final List<String> executables = Arrays.asList(ourExecutable);

    try (OutputStream pathStream = createOutputStream(archiveOutType, path)) {
        ArchiveOutputStream archiveOutputStream = factory.createArchiveOutputStream(archiveOutType, pathStream);
        if (archiveOutputStream instanceof TarArchiveOutputStream) {
            ((TarArchiveOutputStream) archiveOutputStream).setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
        }/*from w w w . jav  a 2 s .  co  m*/

        // move Consulo to archive, and change permissions
        try (InputStream is = new FileInputStream(fileZip)) {
            try (ArchiveInputStream ais = factory.createArchiveInputStream(ArchiveStreamFactory.ZIP, is)) {
                ArchiveEntry tempEntry = ais.getNextEntry();
                while (tempEntry != null) {
                    final ArchiveEntryWrapper newEntry = createEntry(archiveOutType, tempEntry.getName(),
                            tempEntry);

                    newEntry.setMode(extractMode(tempEntry));
                    newEntry.setTime(tempEntry.getLastModifiedDate().getTime());

                    if (executables.contains(tempEntry.getName())) {
                        newEntry.setMode(0b111_101_101);
                    }

                    copyEntry(archiveOutputStream, ais, tempEntry, newEntry);

                    tempEntry = ais.getNextEntry();
                }
            }
        }

        boolean mac = distZip.contains("mac");

        // jdk check
        if (jdkArchivePath != null) {
            try (InputStream is = new FileInputStream(jdkArchivePath)) {
                try (GzipCompressorInputStream gz = new GzipCompressorInputStream(is)) {
                    try (ArchiveInputStream ais = factory.createArchiveInputStream(ArchiveStreamFactory.TAR,
                            gz)) {
                        ArchiveEntry tempEntry = ais.getNextEntry();
                        while (tempEntry != null) {
                            final String name = tempEntry.getName();

                            // is our path
                            if (!mac && name.startsWith("jre/")) {
                                final ArchiveEntryWrapper jdkEntry = createEntry(archiveOutType,
                                        "Consulo/platform/buildSNAPSHOT/" + name, tempEntry);
                                jdkEntry.setMode(extractMode(tempEntry));
                                jdkEntry.setTime(tempEntry.getLastModifiedDate().getTime());

                                copyEntry(archiveOutputStream, ais, tempEntry, jdkEntry);
                            } else if (mac && name.startsWith("jdk")) {
                                boolean needAddToArchive = true;
                                for (String prefix : ourMacSkipJdkList) {
                                    if (name.startsWith(prefix)) {
                                        needAddToArchive = false;
                                    }
                                }

                                if (needAddToArchive) {
                                    final ArchiveEntryWrapper jdkEntry = createEntry(archiveOutType,
                                            "Consulo.app/Contents/platform/buildSNAPSHOT/jre/" + name,
                                            tempEntry);
                                    jdkEntry.setMode(extractMode(tempEntry));
                                    jdkEntry.setTime(tempEntry.getLastModifiedDate().getTime());

                                    copyEntry(archiveOutputStream, ais, tempEntry, jdkEntry);
                                }
                            }

                            tempEntry = ais.getNextEntry();
                        }
                    }
                }
            }
        }

        archiveOutputStream.finish();
    }
}

From source file:com.continuuity.weave.kafka.client.KafkaTest.java

private static File extractKafka() throws IOException, ArchiveException, CompressorException {
    File kafkaExtract = TMP_FOLDER.newFolder();
    InputStream kakfaResource = KafkaTest.class.getClassLoader().getResourceAsStream("kafka-0.7.2.tgz");
    ArchiveInputStream archiveInput = new ArchiveStreamFactory()
            .createArchiveInputStream(ArchiveStreamFactory.TAR, new CompressorStreamFactory()
                    .createCompressorInputStream(CompressorStreamFactory.GZIP, kakfaResource));

    try {/*  ww  w. j av a  2  s . c  o  m*/
        ArchiveEntry entry = archiveInput.getNextEntry();
        while (entry != null) {
            File file = new File(kafkaExtract, entry.getName());
            if (entry.isDirectory()) {
                file.mkdirs();
            } else {
                ByteStreams.copy(archiveInput, Files.newOutputStreamSupplier(file));
            }
            entry = archiveInput.getNextEntry();
        }
    } finally {
        archiveInput.close();
    }
    return kafkaExtract;
}

From source file:io.github.retz.executor.FileManager.java

private static ArchiveInputStream createAIS(File file)
        throws FileNotFoundException, IOException, ArchiveException {
    ArchiveStreamFactory factory = new ArchiveStreamFactory();
    InputStream in = new BufferedInputStream(new FileInputStream(file));

    if (file.getName().endsWith(".tar.gz") || file.getName().endsWith(".tgz")) {
        return factory.createArchiveInputStream(ArchiveStreamFactory.TAR, new GZIPInputStream(in));
    } else if (file.getName().endsWith(".tar.bz2") || file.getName().endsWith(".tar.xz")) { // TODO: "tar, tbz2, txz. See mesos/src/launcher/fetcher.cpp for supported formats
        LOG.error("TODO: compression on {} must be supported", file.getName());
        throw new RuntimeException();
    }//from   w  ww . ja v  a 2  s  .c  om
    LOG.error("Not decompressing. File with unsupported suffix: {}", file);
    return null;
}

From source file:ezbake.deployer.utilities.ArtifactHelpers.java

/**
 * Append to the given ArchiveInputStream writing to the given outputstream, the given entries to add.
 * This will duplicate the InputStream to the Output.
 *
 * @param inputStream - archive input to append to
 * @param output      - what to copy the modified archive to
 * @param filesToAdd  - what entries to append.
 *///from  ww  w.j  a  v  a 2  s.  c  o  m
private static void appendFilesInTarArchive(ArchiveInputStream inputStream, OutputStream output,
        Iterable<ArtifactDataEntry> filesToAdd) throws DeploymentException {
    ArchiveStreamFactory asf = new ArchiveStreamFactory();

    try {
        HashMap<String, ArtifactDataEntry> newFiles = new HashMap<>();
        for (ArtifactDataEntry entry : filesToAdd) {
            newFiles.put(entry.getEntry().getName(), entry);
        }
        GZIPOutputStream gzs = new GZIPOutputStream(output);
        TarArchiveOutputStream aos = (TarArchiveOutputStream) asf
                .createArchiveOutputStream(ArchiveStreamFactory.TAR, gzs);
        aos.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
        // copy the existing entries
        ArchiveEntry nextEntry;
        while ((nextEntry = inputStream.getNextEntry()) != null) {
            //If we're passing in the same file, don't copy into the new archive
            if (!newFiles.containsKey(nextEntry.getName())) {
                aos.putArchiveEntry(nextEntry);
                IOUtils.copy(inputStream, aos);
                aos.closeArchiveEntry();
            }
        }

        for (ArtifactDataEntry entry : filesToAdd) {
            aos.putArchiveEntry(entry.getEntry());
            IOUtils.write(entry.getData(), aos);
            aos.closeArchiveEntry();
        }
        aos.finish();
        gzs.finish();
    } catch (ArchiveException | IOException e) {
        log.error(e.getMessage(), e);
        throw new DeploymentException(e.getMessage());
    }
}