Search in sources :

Example 1 with ArchiveInputStream

use of org.apache.commons.compress.archivers.ArchiveInputStream in project weave by continuuity.

the class KafkaTest method extractKafka.

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 {
        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;
}
Also used : ArchiveStreamFactory(org.apache.commons.compress.archivers.ArchiveStreamFactory) ArchiveInputStream(org.apache.commons.compress.archivers.ArchiveInputStream) ArchiveInputStream(org.apache.commons.compress.archivers.ArchiveInputStream) InputStream(java.io.InputStream) CompressorStreamFactory(org.apache.commons.compress.compressors.CompressorStreamFactory) ArchiveEntry(org.apache.commons.compress.archivers.ArchiveEntry) File(java.io.File)

Example 2 with ArchiveInputStream

use of org.apache.commons.compress.archivers.ArchiveInputStream 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 3 with ArchiveInputStream

use of org.apache.commons.compress.archivers.ArchiveInputStream 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 4 with ArchiveInputStream

use of org.apache.commons.compress.archivers.ArchiveInputStream 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 5 with ArchiveInputStream

use of org.apache.commons.compress.archivers.ArchiveInputStream in project phoenicis by PhoenicisOrg.

the class Tar method uncompress.

/**
 * Uncompress a tar
 *
 * @param countingInputStream
 *            to count the number of byte extracted
 * @param outputDir
 *            The directory where files should be extracted
 * @return A list of extracted files
 * @throws ArchiveException
 *             if the process fails
 */
private List<File> uncompress(final InputStream inputStream, CountingInputStream countingInputStream, final File outputDir, long finalSize, Consumer<ProgressEntity> stateCallback) {
    final List<File> uncompressedFiles = new LinkedList<>();
    try (ArchiveInputStream debInputStream = new ArchiveStreamFactory().createArchiveInputStream("tar", inputStream)) {
        TarArchiveEntry entry;
        while ((entry = (TarArchiveEntry) debInputStream.getNextEntry()) != null) {
            final File outputFile = new File(outputDir, entry.getName());
            if (entry.isDirectory()) {
                LOGGER.info(String.format("Attempting to write output directory %s.", outputFile.getAbsolutePath()));
                if (!outputFile.exists()) {
                    LOGGER.info(String.format("Attempting to createPrefix output directory %s.", outputFile.getAbsolutePath()));
                    Files.createDirectories(outputFile.toPath());
                }
            } else {
                LOGGER.info(String.format("Creating output file %s (%s).", outputFile.getAbsolutePath(), entry.getMode()));
                if (entry.isSymbolicLink()) {
                    Files.createSymbolicLink(Paths.get(outputFile.getAbsolutePath()), Paths.get(entry.getLinkName()));
                } else {
                    try (final OutputStream outputFileStream = new FileOutputStream(outputFile)) {
                        IOUtils.copy(debInputStream, outputFileStream);
                        Files.setPosixFilePermissions(Paths.get(outputFile.getPath()), fileUtilities.octToPosixFilePermission(entry.getMode()));
                    }
                }
            }
            uncompressedFiles.add(outputFile);
            stateCallback.accept(new ProgressEntity.Builder().withPercent((double) countingInputStream.getCount() / (double) finalSize * (double) 100).withProgressText("Extracting " + outputFile.getName()).build());
        }
        return uncompressedFiles;
    } catch (IOException | org.apache.commons.compress.archivers.ArchiveException e) {
        throw new ArchiveException("Unable to extract the file", e);
    }
}
Also used : LinkedList(java.util.LinkedList) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry) ArchiveStreamFactory(org.apache.commons.compress.archivers.ArchiveStreamFactory) ArchiveInputStream(org.apache.commons.compress.archivers.ArchiveInputStream)

Aggregations

ArchiveInputStream (org.apache.commons.compress.archivers.ArchiveInputStream)27 TarArchiveInputStream (org.apache.commons.compress.archivers.tar.TarArchiveInputStream)14 InputStream (java.io.InputStream)10 ArchiveStreamFactory (org.apache.commons.compress.archivers.ArchiveStreamFactory)10 ArchiveEntry (org.apache.commons.compress.archivers.ArchiveEntry)9 ZipArchiveInputStream (org.apache.commons.compress.archivers.zip.ZipArchiveInputStream)9 TarArchiveEntry (org.apache.commons.compress.archivers.tar.TarArchiveEntry)7 BufferedInputStream (java.io.BufferedInputStream)6 IOException (java.io.IOException)6 LinkedList (java.util.LinkedList)6 BZip2CompressorInputStream (org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream)6 ZipArchiveEntry (org.apache.commons.compress.archivers.zip.ZipArchiveEntry)5 File (java.io.File)4 InputStreamReader (java.io.InputStreamReader)4 BufferedReader (java.io.BufferedReader)3 Path (java.nio.file.Path)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 List (java.util.List)3 ArchiveException (org.apache.commons.compress.archivers.ArchiveException)3