Search in sources :

Example 26 with TarArchiveInputStream

use of org.apache.commons.compress.archivers.tar.TarArchiveInputStream in project BWAPI4J by OpenBW.

the class DummyDataUtils method readMultiLinesAsStringTokensFromArchiveFile.

public static List<List<String>> readMultiLinesAsStringTokensFromArchiveFile(final String archiveFilename, final String mapHash, final String regex) throws IOException {
    final InputStream inputStream = createInputStreamForDummyDataSet(archiveFilename);
    final String mapShortHash = determineMapShortHash(mapHash);
    try (final ArchiveInputStream tarIn = new TarArchiveInputStream(new BZip2CompressorInputStream(inputStream));
        final BufferedReader buffer = new BufferedReader(new InputStreamReader(tarIn))) {
        final ArchiveEntry nextEntry = getArchiveEntry(tarIn, mapShortHash);
        Assert.assertNotNull(nextEntry);
        final List<List<String>> data = new ArrayList<>();
        String line;
        while ((line = buffer.readLine()) != null) {
            if (line.isEmpty()) {
                continue;
            }
            final String[] tokens = line.split(regex);
            final List<String> strTokens = new ArrayList<>();
            for (final String token : tokens) {
                final String tokenTrimmed = token.trim();
                if (tokenTrimmed.isEmpty()) {
                    continue;
                }
                strTokens.add(tokenTrimmed);
            }
            data.add(strTokens);
        }
        int valuesReadCount = 0;
        for (final List<String> list : data) {
            valuesReadCount += list.size();
        }
        logger.debug("Read " + valuesReadCount + " values from " + archiveFilename);
        return data;
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) ArchiveInputStream(org.apache.commons.compress.archivers.ArchiveInputStream) BZip2CompressorInputStream(org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) ArchiveEntry(org.apache.commons.compress.archivers.ArchiveEntry) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) ArchiveInputStream(org.apache.commons.compress.archivers.ArchiveInputStream) BZip2CompressorInputStream(org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream) BufferedReader(java.io.BufferedReader) ArrayList(java.util.ArrayList) List(java.util.List)

Example 27 with TarArchiveInputStream

use of org.apache.commons.compress.archivers.tar.TarArchiveInputStream in project BWAPI4J by OpenBW.

the class DummyDataUtils method readMultiLineIntegerArraysFromArchiveFile.

public static List<List<Integer>> readMultiLineIntegerArraysFromArchiveFile(final String archiveFilename, final String mapHash, final String regex) throws IOException {
    final InputStream inputStream = createInputStreamForDummyDataSet(archiveFilename);
    final String mapShortHash = determineMapShortHash(mapHash);
    try (final ArchiveInputStream tarIn = new TarArchiveInputStream(new BZip2CompressorInputStream(inputStream));
        final BufferedReader buffer = new BufferedReader(new InputStreamReader(tarIn))) {
        final ArchiveEntry nextEntry = getArchiveEntry(tarIn, mapShortHash);
        Assert.assertNotNull(nextEntry);
        final List<List<Integer>> data = new ArrayList<>();
        String line;
        while ((line = buffer.readLine()) != null) {
            if (line.isEmpty()) {
                continue;
            }
            final String[] tokens = line.split(regex);
            final List<Integer> intTokens = new ArrayList<>();
            for (final String token : tokens) {
                final String tokenTrimmed = token.trim();
                if (tokenTrimmed.isEmpty()) {
                    continue;
                }
                int intToken = Integer.parseInt(tokenTrimmed);
                intTokens.add(intToken);
            }
            data.add(intTokens);
        }
        int valuesReadCount = 0;
        for (final List<Integer> list : data) {
            valuesReadCount += list.size();
        }
        logger.debug("Read " + valuesReadCount + " values from " + archiveFilename);
        return data;
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) ArchiveInputStream(org.apache.commons.compress.archivers.ArchiveInputStream) BZip2CompressorInputStream(org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) ArchiveEntry(org.apache.commons.compress.archivers.ArchiveEntry) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) ArchiveInputStream(org.apache.commons.compress.archivers.ArchiveInputStream) BZip2CompressorInputStream(org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream) BufferedReader(java.io.BufferedReader) ArrayList(java.util.ArrayList) List(java.util.List)

Example 28 with TarArchiveInputStream

use of org.apache.commons.compress.archivers.tar.TarArchiveInputStream in project BWAPI4J by OpenBW.

the class DummyDataUtils method readIntegerArrayFromArchiveFile.

public static int[] readIntegerArrayFromArchiveFile(final String archiveFilename, final String mapHash, final String regex) throws IOException {
    final InputStream inputStream = createInputStreamForDummyDataSet(archiveFilename);
    try (final ArchiveInputStream tarIn = new TarArchiveInputStream(new BZip2CompressorInputStream(inputStream));
        final BufferedReader buffer = new BufferedReader(new InputStreamReader(tarIn))) {
        final String mapShortHash = determineMapShortHash(mapHash);
        final ArchiveEntry nextEntry = getArchiveEntry(tarIn, mapShortHash);
        Assert.assertNotNull(nextEntry);
        final int[] read = buffer.lines().flatMap(line -> (Stream<String>) Stream.of(line.split(regex))).map(String::trim).mapToInt(Integer::parseInt).toArray();
        logger.debug("Read " + read.length + " values from " + archiveFilename);
        return read;
    }
}
Also used : TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) ArchiveEntry(org.apache.commons.compress.archivers.ArchiveEntry) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) IOException(java.io.IOException) InputStreamReader(java.io.InputStreamReader) ArrayList(java.util.ArrayList) List(java.util.List) Logger(org.apache.logging.log4j.Logger) Stream(java.util.stream.Stream) Paths(java.nio.file.Paths) ArchiveInputStream(org.apache.commons.compress.archivers.ArchiveInputStream) BufferedReader(java.io.BufferedReader) BZip2CompressorInputStream(org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream) Assert(org.junit.Assert) LogManager(org.apache.logging.log4j.LogManager) InputStream(java.io.InputStream) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) ArchiveInputStream(org.apache.commons.compress.archivers.ArchiveInputStream) BZip2CompressorInputStream(org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream) InputStreamReader(java.io.InputStreamReader) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) ArchiveInputStream(org.apache.commons.compress.archivers.ArchiveInputStream) BZip2CompressorInputStream(org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream) InputStream(java.io.InputStream) BufferedReader(java.io.BufferedReader) ArchiveEntry(org.apache.commons.compress.archivers.ArchiveEntry) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) Stream(java.util.stream.Stream) ArchiveInputStream(org.apache.commons.compress.archivers.ArchiveInputStream) BZip2CompressorInputStream(org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream) InputStream(java.io.InputStream)

Example 29 with TarArchiveInputStream

use of org.apache.commons.compress.archivers.tar.TarArchiveInputStream in project zalenium by zalando.

the class DockerSeleniumRemoteProxy method copyVideos.

@SuppressWarnings("ResultOfMethodCallIgnored")
@VisibleForTesting
void copyVideos(final String containerId) {
    boolean videoWasCopied = false;
    TarArchiveInputStream tarStream = new TarArchiveInputStream(containerClient.copyFiles(containerId, "/videos/"));
    try {
        TarArchiveEntry entry;
        while ((entry = tarStream.getNextTarEntry()) != null) {
            if (entry.isDirectory()) {
                continue;
            }
            String fileExtension = entry.getName().substring(entry.getName().lastIndexOf('.'));
            testInformation.setFileExtension(fileExtension);
            File videoFile = new File(testInformation.getVideoFolderPath(), testInformation.getFileName());
            File parent = videoFile.getParentFile();
            if (!parent.exists()) {
                parent.mkdirs();
            }
            OutputStream outputStream = new FileOutputStream(videoFile);
            IOUtils.copy(tarStream, outputStream);
            outputStream.close();
            videoWasCopied = true;
            LOGGER.info(String.format("%s Video file copied to: %s/%s", getId(), testInformation.getVideoFolderPath(), testInformation.getFileName()));
        }
    } catch (IOException e) {
        // This error happens in k8s, but the file is ok, nevertheless the size is not accurate
        boolean isPipeClosed = e.getMessage().toLowerCase().contains("pipe closed");
        if (ContainerFactory.getIsKubernetes().get() && isPipeClosed) {
            LOGGER.info(String.format("%s Video file copied to: %s/%s", getId(), testInformation.getVideoFolderPath(), testInformation.getFileName()));
        } else {
            LOGGER.warn(getId() + " Error while copying the video", e);
        }
        ga.trackException(e);
    } finally {
        if (!videoWasCopied) {
            testInformation.setVideoRecorded(false);
        }
    }
}
Also used : TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) File(java.io.File) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 30 with TarArchiveInputStream

use of org.apache.commons.compress.archivers.tar.TarArchiveInputStream in project syndesis by syndesisio.

the class ProjectGeneratorTest method generate.

// *****************************
// Helpers
// *****************************
private Path generate(Integration integration, ProjectGeneratorConfiguration generatorConfiguration, TestResourceManager resourceManager) throws IOException {
    final IntegrationProjectGenerator generator = new ProjectGenerator(generatorConfiguration, resourceManager, getMavenProperties());
    try (InputStream is = generator.generate(integration)) {
        Path ret = testFolder.newFolder("integration-project").toPath();
        try (TarArchiveInputStream tis = new TarArchiveInputStream(is)) {
            TarArchiveEntry tarEntry = tis.getNextTarEntry();
            // tarIn is a TarArchiveInputStream
            while (tarEntry != null) {
                // create a file with the same name as the tarEntry
                File destPath = new File(ret.toFile(), tarEntry.getName());
                if (tarEntry.isDirectory()) {
                    destPath.mkdirs();
                } else {
                    destPath.getParentFile().mkdirs();
                    destPath.createNewFile();
                    try (BufferedOutputStream bout = new BufferedOutputStream(new FileOutputStream(destPath))) {
                        IOUtils.copy(tis, bout);
                    }
                }
                tarEntry = tis.getNextTarEntry();
            }
        }
        return ret;
    }
}
Also used : Path(java.nio.file.Path) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) IntegrationProjectGenerator(io.syndesis.integration.api.IntegrationProjectGenerator) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) InputStream(java.io.InputStream) FileOutputStream(java.io.FileOutputStream) IntegrationProjectGenerator(io.syndesis.integration.api.IntegrationProjectGenerator) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream) 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