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:edu.wisc.doit.tcrypt.BouncyCastleFileDecrypter.java

protected String readAndDecrypt(TarArchiveInputStream inputStream, final String fileName)
        throws IOException, InvalidCipherTextException {
    //Read keyfile.enc from the TAR  
    final TarArchiveEntry keyFileEntry = inputStream.getNextTarEntry();

    //Verify file name
    final String keyFileName = keyFileEntry.getName();
    if (!fileName.equals(keyFileName)) {
        throw new IllegalArgumentException("The first entry in the TAR must be name: " + fileName);
    }/*from www. j av  a 2s  . c  o m*/

    //Verify file size
    if (keyFileEntry.getSize() > MAX_ENCRYPTED_KEY_FILE_SIZE) {
        throw new IllegalArgumentException("The encrypted archive's key file cannot be longer than "
                + MAX_ENCRYPTED_KEY_FILE_SIZE + " bytes");
    }

    //Decode the base64 keyfile
    final byte[] encKeyFileBase64Bytes = IOUtils.toByteArray(inputStream);
    final byte[] encKeyFileBytes = Base64.decodeBase64(encKeyFileBase64Bytes);

    //Decrypt the keyfile into UTF-8 String
    final AsymmetricBlockCipher decryptCipher = this.getDecryptCipher();
    final byte[] keyFileBytes = decryptCipher.processBlock(encKeyFileBytes, 0, encKeyFileBytes.length);
    return new String(keyFileBytes, FileEncrypter.CHARSET);
}

From source file:io.cloudslang.content.vmware.services.DeployOvfTemplateService.java

private ITransferVmdkFrom getTransferVmdK(final String templateFilePathStr, final String vmdkName)
        throws IOException {
    final Path templateFilePath = Paths.get(templateFilePathStr);
    if (isOva(templateFilePath)) {
        final TarArchiveInputStream tar = new TarArchiveInputStream(new FileInputStream(templateFilePathStr));
        TarArchiveEntry entry;/*from   ww w.  j  a  v a 2s.  c  o  m*/
        while ((entry = tar.getNextTarEntry()) != null) {
            if (new File(entry.getName()).getName().startsWith(vmdkName)) {
                return new TransferVmdkFromInputStream(tar, entry.getSize());
            }
        }
    } else if (isOvf(templateFilePath)) {
        final Path vmdkPath = templateFilePath.getParent().resolve(vmdkName);
        return new TransferVmdkFromFile(vmdkPath.toFile());
    }
    throw new RuntimeException(NOT_OVA_OR_OVF);
}

From source file:hudson.gridmaven.gridlayer.HadoopInstance.java

/**
 * This method decompress filesystem structure from HDFS archive
 *//*w ww  . java  2  s  .  co m*/
public void getAndUntar(String src, String targetPath) throws FileNotFoundException, IOException {
    BufferedOutputStream dest = null;
    InputStream tarArchiveStream = new FSDataInputStream(fs.open(new Path(src)));
    TarArchiveInputStream tis = new TarArchiveInputStream(new BufferedInputStream(tarArchiveStream));
    TarArchiveEntry entry = null;
    try {
        while ((entry = tis.getNextTarEntry()) != null) {
            int count;
            File outputFile = new File(targetPath, entry.getName());

            if (entry.isDirectory()) { // entry is a directory
                if (!outputFile.exists()) {
                    outputFile.mkdirs();
                }
            } else { // entry is a file
                byte[] data = new byte[BUFFER_MAX];
                FileOutputStream fos = new FileOutputStream(outputFile);
                dest = new BufferedOutputStream(fos, BUFFER_MAX);
                while ((count = tis.read(data, 0, BUFFER_MAX)) != -1) {
                    dest.write(data, 0, count);
                }
                dest.flush();
                dest.close();
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (dest != null) {
            dest.flush();
            dest.close();
        }
        tis.close();
    }
}

From source file:edu.wisc.doit.tcrypt.BouncyCastleFileDecrypter.java

@Override
public InputStream decrypt(InputStream inputStream)
        throws InvalidCipherTextException, IOException, DecoderException {
    final TarArchiveInputStream tarInputStream = new TarArchiveInputStream(inputStream, FileEncrypter.ENCODING);

    final BufferedBlockCipher cipher = createCipher(tarInputStream);

    //Advance to the next entry in the tar file
    tarInputStream.getNextTarEntry();

    //Protect the underlying TAR stream from being closed by the cipher stream
    final CloseShieldInputStream is = new CloseShieldInputStream(tarInputStream);

    //Setup the decrypting cipher stream
    final CipherInputStream stream = new CipherInputStream(is, cipher);

    //Generate a digest of the decrypted data
    final GeneralDigest digest = this.createDigester();
    final DigestInputStream digestInputStream = new DigestInputStream(stream, digest);

    return new DecryptingInputStream(digestInputStream, tarInputStream, digest);
}

From source file:edu.wisc.doit.tcrypt.BouncyCastleFileDecrypter.java

@Override
public void decrypt(InputStream inputStream, OutputStream outputStream)
        throws InvalidCipherTextException, IOException, DecoderException {
    final TarArchiveInputStream tarInputStream = new TarArchiveInputStream(inputStream, FileEncrypter.ENCODING);

    final BufferedBlockCipher cipher = createCipher(tarInputStream);

    //Advance to the next entry in the tar file
    tarInputStream.getNextTarEntry();

    //Create digest output stream used to generate digest while decrypting
    final DigestOutputStream digestOutputStream = new DigestOutputStream(this.createDigester());

    //Do a streaming decryption of the file output
    final CipherOutputStream cipherOutputStream = new CipherOutputStream(
            new TeeOutputStream(outputStream, digestOutputStream), cipher);
    IOUtils.copy(tarInputStream, cipherOutputStream);
    cipherOutputStream.close();//from w ww  .  ja  va 2s . c o m

    //Capture the hash of the decrypted output
    final byte[] hashBytes = digestOutputStream.getDigest();
    verifyOutputHash(tarInputStream, hashBytes);
}

From source file:com.blackducksoftware.integration.hub.docker.tar.DockerTarParser.java

public List<File> extractLayerTars(final File dockerTar) throws IOException {
    final File tarExtractionDirectory = getTarExtractionDirectory();
    final List<File> untaredFiles = new ArrayList<>();
    final File outputDir = new File(tarExtractionDirectory, dockerTar.getName());
    final TarArchiveInputStream tarArchiveInputStream = new TarArchiveInputStream(
            new FileInputStream(dockerTar));
    try {//w w  w . j av  a  2s.com
        TarArchiveEntry tarArchiveEntry = null;
        while (null != (tarArchiveEntry = tarArchiveInputStream.getNextTarEntry())) {
            final File outputFile = new File(outputDir, tarArchiveEntry.getName());
            if (tarArchiveEntry.isFile()) {
                if (!outputFile.getParentFile().exists()) {
                    outputFile.getParentFile().mkdirs();
                }
                final OutputStream outputFileStream = new FileOutputStream(outputFile);
                try {
                    IOUtils.copy(tarArchiveInputStream, outputFileStream);
                    if (tarArchiveEntry.getName().contains(DOCKER_LAYER_TAR_FILENAME)) {
                        untaredFiles.add(outputFile);
                    }
                } finally {
                    outputFileStream.close();
                }
            }
        }
    } finally {
        IOUtils.closeQuietly(tarArchiveInputStream);
    }
    return untaredFiles;
}

From source file:ezbake.deployer.publishers.local.BaseLocalPublisher.java

protected String getConfigPath(DeploymentArtifact artifact) throws Exception {
    TarArchiveInputStream tarIn = new TarArchiveInputStream(
            new GZIPInputStream(new ByteArrayInputStream(artifact.getArtifact())));
    File tmpDir = new File("user-conf", ArtifactHelpers.getServiceId(artifact));
    tmpDir.mkdirs();// w w  w .  j ava  2s  . c  o  m
    String configPath = tmpDir.getAbsolutePath();

    try {
        TarArchiveEntry entry = tarIn.getNextTarEntry();

        while (entry != null) {
            if (entry.getName().startsWith("config/")) {
                File tmpFile = new File(configPath,
                        Files.getNameWithoutExtension(entry.getName()) + ".properties");
                FileOutputStream fos = new FileOutputStream(tmpFile);
                try {
                    IOUtils.copy(tarIn, fos);
                } finally {
                    IOUtils.closeQuietly(fos);
                }
            }
            entry = tarIn.getNextTarEntry();
        }
    } finally {
        IOUtils.closeQuietly(tarIn);
    }
    return configPath;
}

From source file:edu.mit.lib.bagit.Loader.java

private void inflate(InputStream in, String fmt) throws IOException {
    switch (fmt) {
    case "zip":
        ZipInputStream zin = new ZipInputStream(in);
        ZipEntry entry = null;//  ww w. j  a v a2 s.  c  om
        while ((entry = zin.getNextEntry()) != null) {
            File outFile = new File(base.getParent(), entry.getName());
            outFile.getParentFile().mkdirs();
            Files.copy(zin, outFile.toPath());
        }
        zin.close();
        break;
    case "tgz":
        TarArchiveInputStream tin = new TarArchiveInputStream(new GzipCompressorInputStream(in));
        TarArchiveEntry tentry = null;
        while ((tentry = tin.getNextTarEntry()) != null) {
            File outFile = new File(base.getParent(), tentry.getName());
            outFile.getParentFile().mkdirs();
            Files.copy(tin, outFile.toPath());
        }
        tin.close();
        break;
    default:
        throw new IOException("Unsupported archive format: " + fmt);
    }
}

From source file:com.alcatel_lucent.nz.wnmsextract.reader.FileSelector.java

/**
 * extract tarfile to constituent parts processing gzips along the way
 * yyyyMMdd.tar->/yyyyMMdd/INode-CH_RNC01/A2010...gz
 *///from  w w w.  java 2 s.  c o m
protected void untar(File tf) throws FileNotFoundException {

    try {
        TarArchiveInputStream tais = new TarArchiveInputStream(new FileInputStream(tf));
        TarArchiveEntry t1 = null;
        while ((t1 = tais.getNextTarEntry()) != null) {
            if (t1.isDirectory()) {
                if (t1.getName().contains("account"))
                    identifier = ".vcc";
                else
                    identifier = "";
            } else {
                String fn = t1.getName().substring(t1.getName().lastIndexOf("/"));
                File f = new File(getCalTempPath() + fn);
                FileOutputStream fos = new FileOutputStream(f);
                BufferedOutputStream bos = new BufferedOutputStream(fos, BUFFER);

                int n = 0;
                byte[] content = new byte[BUFFER];
                while (-1 != (n = tais.read(content))) {
                    fos.write(content, 0, n);
                }

                bos.flush();
                bos.close();
                fos.close();

                File unz = null;
                if (f.getName().endsWith("zip"))
                    unz = unzip3(f);
                else
                    unz = ungzip(f);

                if (unz != null)
                    allfiles.add(unz);
                f.delete();
            }
        }
        tais.close();
    } catch (IOException ioe) {
        jlog.fatal("IO read error :: " + ioe);
    }

}

From source file:ca.nrc.cadc.sc2pkg.PackageIntTest.java

private Content getEntry(TarArchiveInputStream tar) throws IOException, NoSuchAlgorithmException {
    Content ret = new Content();

    TarArchiveEntry entry = tar.getNextTarEntry();
    ret.name = entry.getName();//from   w  ww.ja  v  a2s . c  om

    if (ret.name.endsWith("README")) {
        byte[] buf = new byte[(int) entry.getSize()];
        tar.read(buf);
        ByteArrayInputStream bis = new ByteArrayInputStream(buf);
        LineNumberReader r = new LineNumberReader(new InputStreamReader(bis));
        String line = r.readLine();
        while (line != null) {
            String[] tokens = line.split(" ");
            // status [md5 filename url]
            String status = tokens[0];
            if ("OK".equals(status)) {
                String fname = tokens[1];
                String md5 = tokens[2];
                ret.md5map.put(fname, md5);
            } else {
                throw new RuntimeException("tar content failure: " + line);
            }
            line = r.readLine();
        }
    } else {
        MessageDigest md5 = MessageDigest.getInstance("MD5");
        byte[] buf = new byte[8192];
        int n = tar.read(buf);
        while (n > 0) {
            md5.update(buf, 0, n);
            n = tar.read(buf);
        }
        byte[] md5sum = md5.digest();
        ret.contentMD5 = HexUtil.toHex(md5sum);
    }

    return ret;
}