Example usage for org.apache.commons.compress.archivers.tar TarArchiveInputStream getNextTarEntry

List of usage examples for org.apache.commons.compress.archivers.tar TarArchiveInputStream getNextTarEntry

Introduction

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

Prototype

public TarArchiveEntry getNextTarEntry() throws IOException 

Source Link

Document

Get the next entry in this tar archive.

Usage

From source file:org.apache.nifi.util.FlowFileUnpackagerV1.java

@Override
public Map<String, String> unpackageFlowFile(final InputStream in, final OutputStream out) throws IOException {
    flowFilesRead++;/* ww  w . ja  v  a2 s .c o m*/
    final TarArchiveInputStream tarIn = new TarArchiveInputStream(in);
    final TarArchiveEntry attribEntry = tarIn.getNextTarEntry();
    if (attribEntry == null) {
        return null;
    }

    final Map<String, String> attributes;
    if (attribEntry.getName().equals(FlowFilePackagerV1.FILENAME_ATTRIBUTES)) {
        attributes = getAttributes(tarIn);
    } else {
        throw new IOException("Expected two tar entries: " + FlowFilePackagerV1.FILENAME_CONTENT + " and "
                + FlowFilePackagerV1.FILENAME_ATTRIBUTES);
    }

    final TarArchiveEntry contentEntry = tarIn.getNextTarEntry();

    if (contentEntry != null && contentEntry.getName().equals(FlowFilePackagerV1.FILENAME_CONTENT)) {
        final byte[] buffer = new byte[512 << 10];//512KB
        int bytesRead = 0;
        while ((bytesRead = tarIn.read(buffer)) != -1) { //still more data to read
            if (bytesRead > 0) {
                out.write(buffer, 0, bytesRead);
            }
        }
        out.flush();
    } else {
        throw new IOException("Expected two tar entries: " + FlowFilePackagerV1.FILENAME_CONTENT + " and "
                + FlowFilePackagerV1.FILENAME_ATTRIBUTES);
    }

    return attributes;
}

From source file:org.apache.sysml.validation.ValidateLicAndNotice.java

/**
 * This will return the file from tgz file and store it in specified location.
 *
 * @param   tgzFileName is the name of tgz file from which file to be extracted.
 * @param   fileName is the name of the file to be extracted.
 * @param   strDestLoc is the location where file will be extracted.
 * @param    bFirstDirLevel to indicate to get file from first directory level.
 * @return   Sucess or Failure//from   w ww . jav a2  s.c  om
 */
public static boolean extractFileFromTGZ(String tgzFileName, String fileName, String strDestLoc,
        boolean bFirstDirLevel) {

    boolean bRetCode = Constants.bFAILURE;

    TarArchiveInputStream tarIn = null;

    try {

        tarIn = new TarArchiveInputStream(
                new GzipCompressorInputStream(new BufferedInputStream(new FileInputStream(tgzFileName))));
    } catch (Exception e) {
        Utility.debugPrint(Constants.DEBUG_ERROR, "Exception in unzipping tar file: " + e);
        return bRetCode;
    }

    try {
        BufferedOutputStream bufOut = null;
        BufferedInputStream bufIn = null;
        TarArchiveEntry tarEntry = null;
        while ((tarEntry = tarIn.getNextTarEntry()) != null) {
            if (!tarEntry.getName().endsWith(fileName))
                continue;
            //Get file at root (in single directory) level. This is for License in root location.
            if (bFirstDirLevel && (tarEntry.getName().indexOf('/') != tarEntry.getName().lastIndexOf('/')))
                continue;
            bufIn = new BufferedInputStream(tarIn);
            int count;
            byte data[] = new byte[Constants.BUFFER];
            String strOutFileName = strDestLoc == null ? tarEntry.getName() : strDestLoc + "/" + fileName;
            FileOutputStream fos = new FileOutputStream(strOutFileName);
            bufOut = new BufferedOutputStream(fos, Constants.BUFFER);
            while ((count = bufIn.read(data, 0, Constants.BUFFER)) != -1) {
                bufOut.write(data, 0, count);
            }
            bufOut.flush();
            bufOut.close();
            bufIn.close();
            bRetCode = Constants.bSUCCESS;
            break;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return bRetCode;
}

From source file:org.apache.sysml.validation.ValidateLicAndNotice.java

/**
 * This will return the list of files from tgz file.
 *
 * @param   tgzFileName is the name of tgz file from which list of files with specified file extension will be returned.
 * @param   fileExt is the file extension to be used to get list of files to be returned.
 * @return   Returns list of files having specified extension from tgz file.
 *///  w ww.j a v a  2  s .  com
public static List<String> getFilesFromTGZ(String tgzFileName, String fileExt) {

    TarArchiveInputStream tarIn = null;

    try {

        tarIn = new TarArchiveInputStream(
                new GzipCompressorInputStream(new BufferedInputStream(new FileInputStream(tgzFileName))));
    } catch (Exception e) {
        Utility.debugPrint(Constants.DEBUG_ERROR, "Exception in unzipping tar file: " + e);
        return null;
    }

    List<String> files = new ArrayList<String>();
    try {
        TarArchiveEntry tarEntry = null;
        while ((tarEntry = tarIn.getNextTarEntry()) != null) {
            if (tarEntry.getName().endsWith("." + fileExt)) {
                int iPos = tarEntry.getName().lastIndexOf("/");
                if (iPos == 0)
                    --iPos;
                String strFileName = tarEntry.getName().substring(iPos + 1);
                files.add(strFileName);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return (files);
}

From source file:org.apache.tajo.yarn.command.LaunchCommand.java

private String getTajoRootInTar(String tar) throws IOException {
    TarArchiveInputStream inputStream = null;
    try {/*w  w w  .  j av  a2 s . c om*/
        inputStream = new TarArchiveInputStream(new GZIPInputStream(new FileInputStream(tar)));
        for (TarArchiveEntry entry = inputStream.getNextTarEntry(); entry != null;) {
            String entryName = entry.getName();
            if (entry.isDirectory() && entryName.startsWith("tajo-")) {
                return entryName.substring(0, entryName.length() - 1);
            }
            entry = inputStream.getNextTarEntry();
        }
    } finally {
        if (inputStream != null) {
            inputStream.close();
        }
    }

    throw new IOException("Unable to get tajo home dir from " + tar);
}

From source file:org.apache.tika.parser.pkg.TikaArchiveStreamFactory.java

/**
 * Try to determine the type of Archiver
 * @param in input stream//from  w  ww.j  a va 2s .  c o  m
 * @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.");
    }

    if (ZipArchiveInputStream.matches(signature, signatureLength)) {
        return ZIP;
    } else if (JarArchiveInputStream.matches(signature, signatureLength)) {
        return JAR;
    }
    if (ArArchiveInputStream.matches(signature, signatureLength)) {
        return AR;
    } else if (CpioArchiveInputStream.matches(signature, signatureLength)) {
        return CPIO;
    } else if (ArjArchiveInputStream.matches(signature, signatureLength)) {
        return ARJ;
    } else if (SevenZFile.matches(signature, signatureLength)) {
        return SEVEN_Z;
    }

    // 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");
    }
    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");
    }
    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
            // 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");
}

From source file:org.cloudfoundry.client.ApplicationsTest.java

@Test
public void downloadDroplet() {
    String applicationName = getApplicationName();

    this.spaceId.then(spaceId -> createApplicationId(this.cloudFoundryClient, spaceId, applicationName))
            .then(applicationId -> uploadAndStartApplication(this.cloudFoundryClient, applicationId))
            .flatMap(applicationId -> this.cloudFoundryClient.applicationsV2().downloadDroplet(
                    DownloadApplicationDropletRequest.builder().applicationId(applicationId).build()))
            .reduceWith(ByteArrayOutputStream::new, ApplicationsTest::collectIntoByteArrayInputStream)
            .map(bytes -> {/*from   w  ww  .j  av a  2s . co  m*/
                boolean staticFile = false;
                boolean indexFile = false;

                try {
                    TarArchiveInputStream tis = new TarArchiveInputStream(
                            new GZIPInputStream(new ByteArrayInputStream(bytes.toByteArray())));
                    for (TarArchiveEntry entry = tis.getNextTarEntry(); entry != null; entry = tis
                            .getNextTarEntry()) {
                        if (entry.getName().contains("Staticfile")) {
                            staticFile = true;
                        }
                        if (entry.getName().contains("index.html")) {
                            indexFile = true;
                        }
                    }
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
                return staticFile && indexFile;
            }).subscribe(testSubscriber().assertEquals(true));
}

From source file:org.codehaus.plexus.archiver.tar.TarArchiveContentLister.java

protected List<ArchiveContentEntry> execute() throws ArchiverException {
    ArrayList<ArchiveContentEntry> archiveContentList = new ArrayList<ArchiveContentEntry>();
    getLogger().debug("listing: " + getSourceFile());
    TarArchiveInputStream tis = null;
    try {// w ww.  j av  a2s.  c o m
        TarFile tarFile = new TarFile(getSourceFile());
        tis = new TarArchiveInputStream(decompress(compression, getSourceFile(),
                new BufferedInputStream(new FileInputStream(getSourceFile()))));
        TarArchiveEntry te;
        while ((te = tis.getNextTarEntry()) != null) {
            MyTarResource fileInfo = new MyTarResource(tarFile, te);
            if (isSelected(te.getName(), fileInfo)) {
                ArchiveContentEntry ae = fileInfo.asArchiveContentEntry();
                archiveContentList.add(ae);
            }
        }
        getLogger().debug("listing complete");
    } catch (final IOException ioe) {
        throw new ArchiverException("Error while listing " + getSourceFile().getAbsolutePath(), ioe);
    } finally {
        IOUtil.close(tis);
    }
    return archiveContentList;
}

From source file:org.codehaus.plexus.archiver.tar.TarArchiverTest.java

public void createArchive(final int directoryMode, final int fileModes[]) throws Exception {
    int defaultFileMode = fileModes[0];
    int oneFileMode = fileModes[1];
    int twoFileMode = fileModes[2];

    TarArchiver archiver = getPosixTarArchiver();

    archiver.setDirectoryMode(directoryMode);

    archiver.setFileMode(defaultFileMode);

    archiver.addDirectory(getTestFile("src/main"));
    archiver.setFileMode(oneFileMode);//from   w  ww .j  ava2 s.  c o m

    archiver.addFile(getTestFile("src/test/resources/manifests/manifest1.mf"), "one.txt");
    archiver.addFile(getTestFile("src/test/resources/manifests/manifest2.mf"), "two.txt", twoFileMode);
    archiver.setDestFile(getTestFile("target/output/archive.tar"));

    archiver.addSymlink("link_to_test_destinaton", "../test_destination/");

    archiver.createArchive();

    TarArchiveInputStream tis;

    tis = new TarArchiveInputStream(bufferedInputStream(new FileInputStream(archiver.getDestFile())));
    TarArchiveEntry te;

    while ((te = tis.getNextTarEntry()) != null) {
        if (te.isDirectory()) {
            assertEquals("un-expected tar-entry mode for [te.name=" + te.getName() + "]", directoryMode,
                    te.getMode() & UnixStat.PERM_MASK);
        } else if (te.isSymbolicLink()) {
            assertEquals("../test_destination/", te.getLinkName());
            assertEquals("link_to_test_destinaton", te.getName());
            assertEquals(0640, te.getMode() & UnixStat.PERM_MASK);
        } else {
            if (te.getName().equals("one.txt")) {
                assertEquals(oneFileMode, te.getMode() & UnixStat.PERM_MASK);
            } else if (te.getName().equals("two.txt")) {
                assertEquals(twoFileMode, te.getMode() & UnixStat.PERM_MASK);
            } else {
                assertEquals("un-expected tar-entry mode for [te.name=" + te.getName() + "]", defaultFileMode,
                        te.getMode() & UnixStat.PERM_MASK);
            }

        }
    }
    IOUtil.close(tis);

}

From source file:org.codehaus.plexus.archiver.tar.TarArchiverTest.java

public void testCreateArchiveWithJiustASymlink() throws Exception {
    TarArchiver archiver = getPosixTarArchiver();

    archiver.setDirectoryMode(0500);/*ww w.  j  a v a2s. c o  m*/

    archiver.setFileMode(0400);

    archiver.setFileMode(0640);

    archiver.setDestFile(getTestFile("target/output/symlinkarchive.tar"));

    archiver.addSymlink("link_to_test_destinaton", "../test_destination/");

    archiver.createArchive();

    TarArchiveInputStream tis;

    tis = new TarArchiveInputStream(new BufferedInputStream(new FileInputStream(archiver.getDestFile())));
    TarArchiveEntry te;

    while ((te = tis.getNextTarEntry()) != null) {
        if (te.isDirectory()) {
            assertEquals("un-expected tar-entry mode for [te.name=" + te.getName() + "]", 0500,
                    te.getMode() & UnixStat.PERM_MASK);
        } else if (te.isSymbolicLink()) {
            assertEquals("../test_destination/", te.getLinkName());
            assertEquals("link_to_test_destinaton", te.getName());
            assertEquals(0640, te.getMode() & UnixStat.PERM_MASK);
        } else {
            assertEquals("un-expected tar-entry mode for [te.name=" + te.getName() + "]", 0400,
                    te.getMode() & UnixStat.PERM_MASK);
        }
    }
    tis.close();

}

From source file:org.codehaus.plexus.archiver.tar.TarRoundTripTest.java

/**
 * test round-tripping long (GNU) entries
 *///from   w  ww .  j ava  2 s  .c  o  m
public void testLongRoundTripping() throws IOException {
    TarArchiveEntry original = new TarArchiveEntry(LONG_NAME);
    assertEquals("over 100 chars", true, LONG_NAME.length() > 100);
    assertEquals("original name", LONG_NAME, original.getName());

    ByteArrayOutputStream buff = new ByteArrayOutputStream();
    TarArchiveOutputStream tos = new TarArchiveOutputStream(buff);
    tos.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
    tos.putArchiveEntry(original);
    tos.closeArchiveEntry();
    tos.close();

    TarArchiveInputStream tis = new TarArchiveInputStream(new ByteArrayInputStream(buff.toByteArray()));
    TarArchiveEntry tripped = tis.getNextTarEntry();
    assertEquals("round-tripped name", LONG_NAME, tripped.getName());
    assertNull("no more entries", tis.getNextEntry());
    tis.close();
}