Search in sources :

Example 41 with TarArchiveInputStream

use of org.apache.commons.compress.archivers.tar.TarArchiveInputStream in project jbosstools-openshift by jbosstools.

the class FileHelper method extractTarGz.

public static void extractTarGz(File archive, File outputDirectory) {
    InputStream inputStream = null;
    try {
        logger.info("Opening stream to gzip archive");
        inputStream = new GzipCompressorInputStream(new FileInputStream(archive));
    } catch (IOException ex) {
        throw new OpenShiftToolsException("Exception occured while processing tar.gz file.\n" + ex.getMessage());
    }
    logger.info("Opening stream to tar archive");
    BufferedOutputStream outputStream = null;
    TarArchiveInputStream tarArchiveInputStream = new TarArchiveInputStream(inputStream);
    TarArchiveEntry currentEntry = null;
    try {
        while ((currentEntry = tarArchiveInputStream.getNextTarEntry()) != null) {
            if (currentEntry.isDirectory()) {
                logger.info("Creating directory: " + currentEntry.getName());
                createDirectory(new File(outputDirectory, currentEntry.getName()));
            } else {
                File outputFile = new File(outputDirectory, currentEntry.getName());
                if (!outputFile.getParentFile().exists()) {
                    logger.info("Creating directory: " + outputFile.getParentFile());
                    createDirectory(outputFile.getParentFile());
                }
                outputStream = new BufferedOutputStream(new FileOutputStream(outputFile));
                logger.info("Extracting file: " + currentEntry.getName());
                copy(tarArchiveInputStream, outputStream, (int) currentEntry.getSize());
                outputStream.close();
                outputFile.setExecutable(true);
                outputFile.setReadable(true);
                outputFile.setWritable(true);
            }
        }
    } catch (IOException e) {
        throw new OpenShiftToolsException("Exception occured while processing tar.gz file.\n" + e.getMessage());
    } finally {
        try {
            tarArchiveInputStream.close();
        } catch (Exception ex) {
        }
        try {
            outputStream.close();
        } catch (Exception ex) {
        }
    }
}
Also used : GzipCompressorInputStream(org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) BufferedInputStream(java.io.BufferedInputStream) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) FileInputStream(java.io.FileInputStream) GzipCompressorInputStream(org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream) InputStream(java.io.InputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) BufferedOutputStream(java.io.BufferedOutputStream) File(java.io.File) ZipFile(java.util.zip.ZipFile) FileInputStream(java.io.FileInputStream) OpenShiftToolsException(org.jboss.tools.openshift.reddeer.exception.OpenShiftToolsException) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry) OpenShiftToolsException(org.jboss.tools.openshift.reddeer.exception.OpenShiftToolsException) IOException(java.io.IOException)

Example 42 with TarArchiveInputStream

use of org.apache.commons.compress.archivers.tar.TarArchiveInputStream in project LibreraReader by foobnix.

the class ArchiveStreamFactory method detect.

/**
 * Try to determine the type of Archiver
 * @param in input stream
 * @return type of archiver if found
 * @throws ArchiveException if an archiver cannot be detected in the stream
 * @since 1.14
 */
public static String detect(InputStream in) throws ArchiveException {
    if (in == null) {
        throw new IllegalArgumentException("Stream must not be null.");
    }
    if (!in.markSupported()) {
        throw new IllegalArgumentException("Mark is not supported.");
    }
    final byte[] signature = new byte[SIGNATURE_SIZE];
    in.mark(signature.length);
    int signatureLength = -1;
    try {
        signatureLength = IOUtils.readFully(in, signature);
        in.reset();
    } catch (IOException e) {
        throw new ArchiveException("IOException while reading signature.", e);
    }
    if (ZipArchiveInputStream.matches(signature, signatureLength)) {
        return ZIP;
    } else if (JarArchiveInputStream.matches(signature, signatureLength)) {
        return JAR;
    } else if (ArArchiveInputStream.matches(signature, signatureLength)) {
        return AR;
    } else if (CpioArchiveInputStream.matches(signature, signatureLength)) {
        return CPIO;
    } else if (ArjArchiveInputStream.matches(signature, signatureLength)) {
        return ARJ;
    }
    // Dump needs a bigger buffer to check the signature;
    final byte[] dumpsig = new byte[DUMP_SIGNATURE_SIZE];
    in.mark(dumpsig.length);
    try {
        signatureLength = IOUtils.readFully(in, dumpsig);
        in.reset();
    } catch (IOException e) {
        throw new ArchiveException("IOException while reading dump signature", e);
    }
    if (DumpArchiveInputStream.matches(dumpsig, signatureLength)) {
        return DUMP;
    }
    // Tar needs an even bigger buffer to check the signature; read the first block
    final byte[] tarHeader = new byte[TAR_HEADER_SIZE];
    in.mark(tarHeader.length);
    try {
        signatureLength = IOUtils.readFully(in, tarHeader);
        in.reset();
    } catch (IOException e) {
        throw new ArchiveException("IOException while reading tar signature", e);
    }
    if (TarArchiveInputStream.matches(tarHeader, signatureLength)) {
        return TAR;
    }
    // COMPRESS-117 - improve auto-recognition
    if (signatureLength >= TAR_HEADER_SIZE) {
        TarArchiveInputStream tais = null;
        try {
            tais = new TarArchiveInputStream(new ByteArrayInputStream(tarHeader));
            // COMPRESS-191 - verify the header checksum
            if (tais.getNextTarEntry().isCheckSumOK()) {
                return TAR;
            }
        } catch (final Exception e) {
        // NOPMD // NOSONAR
        // can generate IllegalArgumentException as well
        // as IOException
        // autodetection, simply not a TAR
        // ignored
        } finally {
            IOUtils.closeQuietly(tais);
        }
    }
    throw new ArchiveException("No Archiver found for the stream signature");
}
Also used : TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) IOException(java.io.IOException) IOException(java.io.IOException)

Example 43 with TarArchiveInputStream

use of org.apache.commons.compress.archivers.tar.TarArchiveInputStream in project testcontainers-java by testcontainers.

the class GenericContainer method copyFileFromContainer.

/**
 * {@inheritDoc}
 */
@Override
public void copyFileFromContainer(String containerPath, String destinationPath) throws IOException {
    if (!isRunning()) {
        throw new IllegalStateException("copyFileToContainer can only be used while the Container is running");
    }
    try (final TarArchiveInputStream tarInputStream = new TarArchiveInputStream(this.dockerClient.copyArchiveFromContainerCmd(this.containerId, containerPath).exec())) {
        tarInputStream.getNextTarEntry();
        IOUtils.copy(tarInputStream, new FileOutputStream(destinationPath));
    }
}
Also used : TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) FileOutputStream(java.io.FileOutputStream)

Example 44 with TarArchiveInputStream

use of org.apache.commons.compress.archivers.tar.TarArchiveInputStream in project testcontainers-java by testcontainers.

the class VncRecordingContainer method streamRecording.

@SneakyThrows
public InputStream streamRecording() {
    TarArchiveInputStream archiveInputStream = new TarArchiveInputStream(dockerClient.copyArchiveFromContainerCmd(containerId, RECORDING_FILE_NAME).exec());
    archiveInputStream.getNextEntry();
    return archiveInputStream;
}
Also used : TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) SneakyThrows(lombok.SneakyThrows)

Example 45 with TarArchiveInputStream

use of org.apache.commons.compress.archivers.tar.TarArchiveInputStream in project streamline by hortonworks.

the class CustomProcessorUploadHandler method getFileAsByteArray.

private byte[] getFileAsByteArray(File tarFile, Function<String, Boolean> filterFunc) {
    byte[] data = null;
    LOG.info("Looking in to file {} ", tarFile);
    try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(tarFile));
        TarArchiveInputStream tarArchiveInputStream = new TarArchiveInputStream(bis)) {
        TarArchiveEntry tarArchiveEntry = tarArchiveInputStream.getNextTarEntry();
        while (tarArchiveEntry != null) {
            if (filterFunc.apply(tarArchiveEntry.getName())) {
                data = IOUtils.toByteArray(tarArchiveInputStream);
                break;
            }
            tarArchiveEntry = tarArchiveInputStream.getNextTarEntry();
        }
    } catch (IOException e) {
        LOG.warn("Exception occured while looking in to tar file [] ", filterFunc, tarFile, e);
    }
    return data;
}
Also used : TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) BufferedInputStream(java.io.BufferedInputStream) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry)

Aggregations

TarArchiveInputStream (org.apache.commons.compress.archivers.tar.TarArchiveInputStream)131 TarArchiveEntry (org.apache.commons.compress.archivers.tar.TarArchiveEntry)98 File (java.io.File)51 IOException (java.io.IOException)49 FileInputStream (java.io.FileInputStream)45 GzipCompressorInputStream (org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream)45 InputStream (java.io.InputStream)34 FileOutputStream (java.io.FileOutputStream)33 BufferedInputStream (java.io.BufferedInputStream)30 ByteArrayInputStream (java.io.ByteArrayInputStream)27 Test (org.junit.Test)23 ArrayList (java.util.ArrayList)20 GZIPInputStream (java.util.zip.GZIPInputStream)20 ByteArrayOutputStream (java.io.ByteArrayOutputStream)19 ArchiveEntry (org.apache.commons.compress.archivers.ArchiveEntry)17 Path (java.nio.file.Path)16 OutputStream (java.io.OutputStream)15 BufferedOutputStream (java.io.BufferedOutputStream)12 ArchiveStreamFactory (org.apache.commons.compress.archivers.ArchiveStreamFactory)12 ArchiveInputStream (org.apache.commons.compress.archivers.ArchiveInputStream)7