Example usage for org.apache.commons.compress.archivers.tar TarArchiveEntry getName

List of usage examples for org.apache.commons.compress.archivers.tar TarArchiveEntry getName

Introduction

In this page you can find the example usage for org.apache.commons.compress.archivers.tar TarArchiveEntry getName.

Prototype

public String getName() 

Source Link

Document

Get this entry's name.

Usage

From source file:com.pinterest.deployservice.common.TarUtils.java

/**
 * Unbundle the given tar bar as a map, with key as file name and value as content.
 */// w w w  .j  av  a  2 s .  c om
public static Map<String, String> untar(InputStream is) throws Exception {
    TarArchiveInputStream tais = new TarArchiveInputStream(new GZIPInputStream(is));
    Map<String, String> data = new HashMap<String, String>();
    TarArchiveEntry entry;
    while ((entry = tais.getNextTarEntry()) != null) {
        String name = entry.getName();
        byte[] content = new byte[(int) entry.getSize()];
        tais.read(content, 0, content.length);
        data.put(name, new String(content, "UTF8"));
    }
    tais.close();
    return data;
}

From source file:de.dentrassi.pm.utils.deb.Packages.java

public static Map<String, String> parseControlFile(final File packageFile) throws IOException, ParserException {
    try (final ArArchiveInputStream in = new ArArchiveInputStream(new FileInputStream(packageFile))) {
        ArchiveEntry ar;//from  w  w w  .j  a  v  a  2 s  .  c  o  m
        while ((ar = in.getNextEntry()) != null) {
            if (!ar.getName().equals("control.tar.gz")) {
                continue;
            }
            try (final TarArchiveInputStream inputStream = new TarArchiveInputStream(new GZIPInputStream(in))) {
                TarArchiveEntry te;
                while ((te = inputStream.getNextTarEntry()) != null) {
                    String name = te.getName();
                    if (name.startsWith("./")) {
                        name = name.substring(2);
                    }
                    if (!name.equals("control")) {
                        continue;
                    }
                    return parseControlFile(inputStream);
                }
            }
        }
    }
    return null;
}

From source file:gobblin.data.management.copy.converter.UnGzipConverterTest.java

private static String readGzipStreamAsString(InputStream is) throws Exception {
    TarArchiveInputStream tarIn = new TarArchiveInputStream(is);
    try {// w w  w  .  j ava 2 s.co m
        TarArchiveEntry tarEntry;
        while ((tarEntry = tarIn.getNextTarEntry()) != null) {
            if (tarEntry.isFile() && tarEntry.getName().endsWith(".txt")) {
                return IOUtils.toString(tarIn, "UTF-8");
            }
        }
    } finally {
        tarIn.close();
    }

    return Strings.EMPTY;
}

From source file:net.rwx.maven.asciidoc.utils.FileUtils.java

public static String uncompress(InputStream is, String destination) throws IOException {

    BufferedInputStream in = new BufferedInputStream(is);
    GzipCompressorInputStream gzIn = new GzipCompressorInputStream(in);
    TarArchiveInputStream tarInput = new TarArchiveInputStream(gzIn);

    TarArchiveEntry entry = tarInput.getNextTarEntry();
    do {/*w  w  w .ja va  2  s.  c o  m*/
        File f = new File(destination + "/" + entry.getName());
        FileUtils.forceMkdir(f.getParentFile());

        if (!f.isDirectory()) {
            OutputStream os = new FileOutputStream(f);
            byte[] content = new byte[(int) entry.getSize()];
            int byteRead = 0;
            while (byteRead < entry.getSize()) {
                byteRead += tarInput.read(content, byteRead, content.length - byteRead);
                os.write(content, 0, byteRead);
            }

            os.close();
            forceDeleteOnExit(f);
        }
        entry = tarInput.getNextTarEntry();
    } while (entry != null);

    gzIn.close();

    return destination;
}

From source file:com.twitter.heron.downloader.Extractor.java

static void extract(InputStream in, Path destination) throws IOException {
    try (final BufferedInputStream bufferedInputStream = new BufferedInputStream(in);
            final GzipCompressorInputStream gzipInputStream = new GzipCompressorInputStream(
                    bufferedInputStream);
            final TarArchiveInputStream tarInputStream = new TarArchiveInputStream(gzipInputStream)) {
        final String destinationAbsolutePath = destination.toFile().getAbsolutePath();

        TarArchiveEntry entry;
        while ((entry = (TarArchiveEntry) tarInputStream.getNextEntry()) != null) {
            if (entry.isDirectory()) {
                File f = Paths.get(destinationAbsolutePath, entry.getName()).toFile();
                f.mkdirs();//from  www .j  a v  a2s . c o  m
            } else {
                Path fileDestinationPath = Paths.get(destinationAbsolutePath, entry.getName());

                Files.copy(tarInputStream, fileDestinationPath, StandardCopyOption.REPLACE_EXISTING);
            }
        }
    }
}

From source file:co.cask.cdap.internal.app.runtime.LocalizationUtils.java

private static void extractTar(final TarArchiveInputStream tis, File targetDir) throws IOException {
    TarArchiveEntry entry = tis.getNextTarEntry();
    while (entry != null) {
        File output = new File(targetDir, new File(entry.getName()).getName());
        if (entry.isDirectory()) {
            //noinspection ResultOfMethodCallIgnored
            output.mkdirs();// w  w  w .  j av  a  2  s  . com
        } else {
            //noinspection ResultOfMethodCallIgnored
            output.getParentFile().mkdirs();
            ByteStreams.copy(tis, Files.newOutputStreamSupplier(output));
        }
        entry = tis.getNextTarEntry();
    }
}

From source file:com.ibm.util.merge.CompareArchives.java

/**
 * @param archive//w ww. ja  v a 2  s  .  c o  m
 * @param name
 * @return
 * @throws IOException
 */
private static final String getTarFile(String archive, String name) throws IOException {
    TarArchiveInputStream input = new TarArchiveInputStream(
            new BufferedInputStream(new FileInputStream(archive)));
    TarArchiveEntry entry;
    while ((entry = input.getNextTarEntry()) != null) {
        if (entry.getName().equals(name)) {
            byte[] content = new byte[(int) entry.getSize()];
            input.read(content, 0, content.length);
            input.close();
            return new String(content);
        }
    }
    input.close();
    return "";
}

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

public static String getVersionFromArtifact(ByteBuffer artifact) throws IOException {
    String versionNumber = null;//  w  ww.  j  av a 2  s . co m
    try (CompressorInputStream uncompressedInput = new GzipCompressorInputStream(
            new ByteArrayInputStream(artifact.array()))) {
        ArchiveInputStream input = new TarArchiveInputStream(uncompressedInput);
        TarArchiveEntry entry;
        while ((entry = (TarArchiveEntry) input.getNextEntry()) != null) {
            if (!entry.isDirectory() && entry.getName().endsWith(VERSION_FILE)) {
                versionNumber = IOUtils.toString(input, Charsets.UTF_8).trim();
                break;
            }
        }
    }
    return versionNumber;
}

From source file:com.streamsets.datacollector.util.UntarUtility.java

/**
 * Untar an input file into an output file. The output file is created in the output folder, having the same name as
 * the input file, minus the '.tar' extension.
 * @param inputFile the input .tar file/*from  w  w w  .j a v  a  2s  .  c  o m*/
 * @param outputDir the output directory file.
 * @throws IOException
 * @throws FileNotFoundException
 * @return The {@link List} of {@link File}s with the untared content.
 * @throws ArchiveException
 */
private static List<File> unTar(final File inputFile, final File outputDir)
        throws FileNotFoundException, IOException, ArchiveException {

    LOG.info(String.format("Untaring %s to dir %s.", inputFile.getAbsolutePath(), outputDir.getAbsolutePath()));

    final List<File> untaredFiles = new LinkedList<File>();
    final InputStream is = new FileInputStream(inputFile);
    final TarArchiveInputStream debInputStream = (TarArchiveInputStream) new ArchiveStreamFactory()
            .createArchiveInputStream("tar", is);
    TarArchiveEntry entry = null;
    while ((entry = (TarArchiveEntry) debInputStream.getNextEntry()) != null) {
        final File outputFile = new File(outputDir, entry.getName());
        if (entry.isDirectory()) {
            LOG.info(String.format("Attempting to write output directory %s.", outputFile.getAbsolutePath()));
            if (!outputFile.exists()) {
                LOG.info(String.format("Attempting to create output directory %s.",
                        outputFile.getAbsolutePath()));
                if (!outputFile.mkdirs()) {
                    throw new IllegalStateException(
                            String.format("Couldn't create directory %s.", outputFile.getAbsolutePath()));
                }
            }
        } else {
            LOG.info(String.format("Creating output file %s.", outputFile.getAbsolutePath()));
            final OutputStream outputFileStream = new FileOutputStream(outputFile);
            IOUtils.copy(debInputStream, outputFileStream);
            outputFileStream.close();
        }
        untaredFiles.add(outputFile);
    }
    debInputStream.close();

    return untaredFiles;
}

From source file:deodex.tools.TarGzUtils.java

/** Untar an input file into an output file.
        //from w  w w .j  a  v  a  2 s. c o m
 * The output file is created in the output folder, having the same name
 * as the input file, minus the '.tar' extension. 
 * 
 * @param inputFile     the input .tar file
 * @param outputDir     the output directory file. 
 * @throws IOException 
 * @throws FileNotFoundException
 *  
 * @return  The {@link List} of {@link File}s with the untared content.
 * @throws ArchiveException 
 */
public static List<File> unTar(final File inputFile, final File outputDir)
        throws FileNotFoundException, IOException, ArchiveException {

    final List<File> untaredFiles = new LinkedList<File>();
    final InputStream is = new FileInputStream(inputFile);
    final TarArchiveInputStream debInputStream = (TarArchiveInputStream) new ArchiveStreamFactory()
            .createArchiveInputStream("tar", is);
    TarArchiveEntry entry = null;
    while ((entry = (TarArchiveEntry) debInputStream.getNextEntry()) != null) {
        final File outputFile = new File(outputDir, entry.getName());
        if (entry.isDirectory()) {
            Logger.appendLog("Attempting to write output directory . " + outputFile.getAbsolutePath());
            if (!outputFile.exists()) {
                Logger.appendLog("Attempting to create output directory ." + outputFile.getAbsolutePath());
                if (!outputFile.mkdirs()) {
                    throw new IllegalStateException(
                            String.format("Couldn't create directory %s.", outputFile.getAbsolutePath()));
                }
            }
        } else {
            outputFile.getParentFile().mkdirs();
            final OutputStream outputFileStream = new FileOutputStream(outputFile);
            IOUtils.copy(debInputStream, outputFileStream);
            outputFileStream.close();
        }
        untaredFiles.add(outputFile);
    }
    debInputStream.close();

    return untaredFiles;
}