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:eu.openanalytics.rsb.component.AdminResource.java

private void extractCatalogFiles(final File packageSourceFile) throws IOException {
    final File tempDirectory = packageSourceFile.getParentFile();

    // 1) extract TAR
    final File packageTarFile = File.createTempFile("rsb-install.", ".tar", tempDirectory);
    final GzipCompressorInputStream gzIn = new GzipCompressorInputStream(
            new FileInputStream(packageSourceFile));
    FileOutputStream output = new FileOutputStream(packageTarFile);
    IOUtils.copyLarge(gzIn, output);/*from  w  ww. j  a  v a  2s. c  o m*/
    IOUtils.closeQuietly(output);
    IOUtils.closeQuietly(gzIn);

    // 2) parse TAR and drop files in catalog
    final TarArchiveInputStream tarIn = new TarArchiveInputStream(new FileInputStream(packageTarFile));
    TarArchiveEntry tarEntry = null;
    while ((tarEntry = tarIn.getNextTarEntry()) != null) {
        if (!tarEntry.isFile()) {
            continue;
        }

        final Matcher matcher = TAR_CATALOG_FILE_PATTERN.matcher(tarEntry.getName());
        if (matcher.matches()) {
            final byte[] data = IOUtils.toByteArray(tarIn, tarEntry.getSize());

            final String catalogFile = matcher.group(1);
            final File targetCatalogFile = new File(getConfiguration().getCatalogRootDirectory(), catalogFile);
            output = new FileOutputStream(targetCatalogFile);
            IOUtils.write(data, output);
            IOUtils.closeQuietly(output);

            getLogger().info("Wrote " + data.length + " bytes in catalog file: " + targetCatalogFile);
        }
    }
    IOUtils.closeQuietly(tarIn);
}

From source file:com.vmware.photon.controller.model.adapters.vsphere.ovf.OvfRetriever.java

/**
 * If ovaOrOvfUri points to a OVA it will be download locally and extracted. The uri is considered OVA if it is
 * in tar format and there is at least one .ovf inside.
 *
 * @param ovaOrOvfUri/*from  ww  w.  j  av a2 s  .  com*/
 * @return the first .ovf file from the extracted tar of the input parameter if it's a local file or not a
 * tar archive
 */
public URI downloadIfOva(URI ovaOrOvfUri) throws IOException {
    if (ovaOrOvfUri.getScheme().equals("file")) {
        // local files are assumed to be ovfs
        return ovaOrOvfUri;
    }

    HttpGet get = new HttpGet(ovaOrOvfUri);
    HttpResponse check = null;

    logger.debug("Downloading ovf/ova from {}", ovaOrOvfUri);

    try {
        check = this.client.execute(get);
        byte[] buffer = new byte[TAR_MAGIC_OFFSET + TAR_MAGIC.length];
        int read = IOUtils.read(check.getEntity().getContent(), buffer);
        if (read != buffer.length) {
            // not a tar file, probably OVF, lets caller decide further
            return ovaOrOvfUri;
        }
        for (int i = 0; i < TAR_MAGIC.length; i++) {
            byte b = buffer[TAR_MAGIC_OFFSET + i];
            if (b != TAR_MAGIC[i] && b != TAR_MAGIC2[i]) {
                // magic numbers don't match
                logger.info("{} is not a tar file, assuming OVF", ovaOrOvfUri);
                return ovaOrOvfUri;
            }
        }
    } finally {
        get.abort();
        if (check != null) {
            EntityUtils.consumeQuietly(check.getEntity());
        }
    }

    // it's an OVA (at least a tar file), download to a local folder
    String folderName = hash(ovaOrOvfUri);
    File destination = new File(getBaseOvaExtractionDir(), folderName);
    if (new File(destination, MARKER_FILE).isFile()) {
        // marker file exists so the archive is already downloaded
        logger.info("Marker file for {} exists in {}, not downloading again", ovaOrOvfUri, destination);
        return findFirstOvfInFolder(destination);
    }

    destination.mkdirs();
    logger.info("Downloading OVA to {}", destination);

    get = new HttpGet(ovaOrOvfUri);
    HttpResponse response = null;
    try {
        response = this.client.execute(get);
        TarArchiveInputStream tarStream = new TarArchiveInputStream(response.getEntity().getContent());
        TarArchiveEntry entry;
        while ((entry = tarStream.getNextTarEntry()) != null) {
            extractEntry(tarStream, destination, entry);
        }
    } finally {
        if (response != null) {
            EntityUtils.consumeQuietly(response.getEntity());
        }
    }

    // store download progress
    writeMarkerFile(destination, ovaOrOvfUri);

    return findFirstOvfInFolder(destination);
}

From source file:com.surevine.gateway.scm.IncomingProcessorImpl.java

public boolean tarGzHasExpectedContents(final TarArchiveInputStream archive) {
    Boolean hasMetaData = false;/* w  w  w .j av  a  2  s.c o m*/
    Boolean hasBundle = false;
    Integer fileCount = 0;

    TarArchiveEntry entry = null;

    try {
        while ((entry = archive.getNextTarEntry()) != null) {
            if (".metadata.json".equals(entry.getName())) {
                hasMetaData = true;
            } else if (entry.getName().contains(".bundle")) {
                hasBundle = true;
            }
            fileCount++;
        }
        archive.reset();
        return hasMetaData && hasBundle && fileCount == 2;
    } catch (final IOException e) {
        return false;
    }
}

From source file:algorithm.TarPackaging.java

@Override
protected List<RestoredFile> restore(File tarFile) throws IOException {
    List<RestoredFile> extractedFiles = new ArrayList<RestoredFile>();
    FileInputStream inputStream = new FileInputStream(tarFile);
    if (GzipUtils.isCompressedFilename(tarFile.getName())) {
        TarArchiveInputStream tarInputStream = new TarArchiveInputStream(
                new GzipCompressorInputStream(new BufferedInputStream(inputStream)));
        TarArchiveEntry entry = tarInputStream.getNextTarEntry();
        while (entry != null) {
            RestoredFile outputFile = new RestoredFile(RESTORED_DIRECTORY + entry.getName());
            if (entry.isDirectory()) {
                if (!outputFile.exists()) {
                    outputFile.mkdirs();
                }//www .  jav  a  2s  .com
            } else if (entry.isFile()) {
                FileOutputStream outputFileStream = new FileOutputStream(outputFile);
                IOUtils.copy(tarInputStream, outputFileStream);
                outputFileStream.close();
            }
            extractedFiles.add(outputFile);
            entry = tarInputStream.getNextTarEntry();
        }
        tarInputStream.close();
    } else if (BZip2Utils.isCompressedFilename(tarFile.getName())) {
        TarArchiveInputStream tarInputStream = new TarArchiveInputStream(
                new BZip2CompressorInputStream(new BufferedInputStream(inputStream)));
        TarArchiveEntry entry = tarInputStream.getNextTarEntry();
        while (entry != null) {
            RestoredFile outputFile = new RestoredFile(RESTORED_DIRECTORY + entry.getName());
            if (entry.isDirectory()) {
                if (!outputFile.exists()) {
                    outputFile.mkdirs();
                }
            } else if (entry.isFile()) {
                FileOutputStream outputFileStream = new FileOutputStream(outputFile);
                IOUtils.copy(tarInputStream, outputFileStream);
                outputFileStream.close();
            }
            extractedFiles.add(outputFile);
            entry = tarInputStream.getNextTarEntry();
        }
        tarInputStream.close();
    } else {
        TarArchiveInputStream tarInputStream = new TarArchiveInputStream(new BufferedInputStream(inputStream));
        TarArchiveEntry entry = tarInputStream.getNextTarEntry();
        while (entry != null) {
            RestoredFile outputFile = new RestoredFile(RESTORED_DIRECTORY + entry.getName());
            if (entry.isDirectory()) {
                if (!outputFile.exists()) {
                    outputFile.mkdirs();
                }
            } else if (entry.isFile()) {
                FileOutputStream outputFileStream = new FileOutputStream(outputFile);
                IOUtils.copy(tarInputStream, outputFileStream);
                outputFileStream.close();
            }
            extractedFiles.add(outputFile);
            entry = tarInputStream.getNextTarEntry();
        }
        tarInputStream.close();
    }
    for (RestoredFile file : extractedFiles) {
        file.algorithm = this;
        file.checksumValid = true;
        file.restorationNote = "The algorithm doesn't alter these files, so they are brought to its original state. No checksum validation executed.";
        // All files in a .tar file are payload files:
        file.wasPayload = true;
        for (RestoredFile relatedFile : extractedFiles) {
            if (file != relatedFile) {
                file.relatedFiles.add(relatedFile);
            }
        }
    }
    return extractedFiles;
}

From source file:de.uzk.hki.da.pkg.ArchiveBuilder.java

/**
 * Unpacks the contents of the given archive into the given folder
 * /*from w  w  w  .  jav a2 s  . c  o  m*/
 * @param srcFile The archive container file
 * @param destFolder The destination folder
 * @param uncompress Indicates if the archive file is compressed (tgz file) or not (tar file)
 * @throws Exception
 */
public void unarchiveFolder(File srcFile, File destFolder, boolean uncompress) throws Exception {

    FileInputStream fIn = new FileInputStream(srcFile);
    BufferedInputStream in = new BufferedInputStream(fIn);

    TarArchiveInputStream tIn = null;
    GzipCompressorInputStream gzIn = null;

    if (uncompress) {
        gzIn = new GzipCompressorInputStream(in);
        tIn = new TarArchiveInputStream(gzIn);
    } else
        tIn = new TarArchiveInputStream(fIn);

    TarArchiveEntry entry;
    do {
        entry = tIn.getNextTarEntry();

        if (entry == null)
            break;

        File entryFile = new File(destFolder.getAbsolutePath() + "/" + entry.getName());

        if (entry.isDirectory())
            entryFile.mkdirs();
        else {
            new File(entryFile.getAbsolutePath().substring(0, entryFile.getAbsolutePath().lastIndexOf('/')))
                    .mkdirs();

            FileOutputStream out = new FileOutputStream(entryFile);
            IOUtils.copy(tIn, out);
            out.close();
        }
    } while (true);

    tIn.close();
    if (gzIn != null)
        gzIn.close();
    in.close();
    fIn.close();
}

From source file:com.hortonworks.streamline.streams.service.CustomProcessorUploadHandler.java

private byte[] getFileAsByteArray(File tarFile, String fileName) {
    BufferedInputStream bis = null;
    TarArchiveInputStream tarArchiveInputStream = null;
    byte[] data = null;
    try {/*w ww. jav a2 s  . c o  m*/
        LOG.info("Getting file " + fileName + " from " + tarFile);
        bis = new BufferedInputStream(new FileInputStream(tarFile));
        tarArchiveInputStream = new TarArchiveInputStream(bis);
        TarArchiveEntry tarArchiveEntry = tarArchiveInputStream.getNextTarEntry();
        while (tarArchiveEntry != null) {
            if (tarArchiveEntry.getName().equals(fileName)) {
                long size = tarArchiveEntry.getSize();
                data = new byte[(int) size];
                tarArchiveInputStream.read(data, 0, data.length);
                break;
            }
            tarArchiveEntry = tarArchiveInputStream.getNextTarEntry();
        }
    } catch (IOException e) {
        LOG.warn("Exception occured while getting file: " + fileName + " from " + tarFile, e);
    } finally {
        try {
            if (bis != null) {
                bis.close();
            }
            if (tarArchiveInputStream != null) {
                tarArchiveInputStream.close();
            }
        } catch (IOException e) {
            LOG.warn("Error closing input stream for the tar file: " + tarFile, e);
        }
    }
    return data;
}

From source file:com.buaa.cfs.utils.FileUtil.java

private static void unTarUsingJava(File inFile, File untarDir, boolean gzipped) throws IOException {
    InputStream inputStream = null;
    TarArchiveInputStream tis = null;
    try {/*from www  .ja  v  a2 s .  c  o  m*/
        if (gzipped) {
            inputStream = new BufferedInputStream(new GZIPInputStream(new FileInputStream(inFile)));
        } else {
            inputStream = new BufferedInputStream(new FileInputStream(inFile));
        }

        tis = new TarArchiveInputStream(inputStream);

        for (TarArchiveEntry entry = tis.getNextTarEntry(); entry != null;) {
            unpackEntries(tis, entry, untarDir);
            entry = tis.getNextTarEntry();
        }
    } finally {
        IOUtils.cleanup(LOG, tis, inputStream);
    }
}

From source file:com.streamsets.datacollector.restapi.StageLibraryResource.java

@POST
@Path("/stageLibraries/install")
@ApiOperation(value = "Install Stage libraries", response = Object.class, authorizations = @Authorization(value = "basic"))
@Produces(MediaType.APPLICATION_JSON)//from  w ww  .  j a  v  a  2s .  c  o  m
@RolesAllowed({ AuthzRole.ADMIN, AuthzRole.ADMIN_REMOTE })
public Response installLibraries(List<String> libraryIdList) throws IOException {
    String runtimeDir = runtimeInfo.getRuntimeDir();
    String version = buildInfo.getVersion();

    List<String> libraryUrlList = new ArrayList<>();
    List<RepositoryManifestJson> repoManifestList = stageLibrary.getRepositoryManifestList();
    repoManifestList.forEach(repositoryManifestJson -> {
        List<String> libraryFilePathList = repositoryManifestJson.getStageLibraries().stream()
                .filter(stageLibrariesJson -> stageLibrariesJson.getStageLibraryManifest() != null
                        && libraryIdList.contains(stageLibrariesJson.getStageLibraryManifest().getStageLibId()))
                .map(stageLibrariesJson -> stageLibrariesJson.getStageLibraryManifest().getStageLibFile())
                .collect(Collectors.toList());
        libraryUrlList.addAll(libraryFilePathList);
    });

    if (libraryUrlList.size() != libraryIdList.size()) {
        throw new RuntimeException(Utils
                .format("Unable to find to stage library {} in configured repository list", libraryIdList));
    }

    for (String libraryUrl : libraryUrlList) {
        try (Response response = ClientBuilder.newClient().target(libraryUrl).request().get()) {

            String runtimeDirParent = runtimeDir + "/..";
            String[] runtimeDirStrSplitArr = runtimeDir.split("/");
            String installDirName = runtimeDirStrSplitArr[runtimeDirStrSplitArr.length - 1];
            String tarDirRootName = STREAMSETS_ROOT_DIR_PREFIX + version;

            InputStream inputStream = response.readEntity(InputStream.class);

            TarArchiveInputStream myTarFile = new TarArchiveInputStream(
                    new GzipCompressorInputStream(inputStream));

            TarArchiveEntry entry = myTarFile.getNextTarEntry();

            String directory = null;

            while (entry != null) {
                if (entry.isDirectory()) {
                    entry = myTarFile.getNextTarEntry();
                    if (directory == null) {
                        // Initialize root folder
                        if (entry.getName().startsWith(STREAMSETS_LIBS_FOLDER_NAME)) {
                            directory = runtimeDir;
                        } else {
                            directory = runtimeDirParent;
                        }
                    }
                    continue;
                }

                File curFile = new File(directory, entry.getName().replace(tarDirRootName, installDirName));
                File parent = curFile.getParentFile();
                if (!parent.exists() && !parent.mkdirs()) {
                    // Failed to create directory
                    throw new RuntimeException(
                            Utils.format("Failed to create directory: {}", parent.getPath()));
                }
                OutputStream out = new FileOutputStream(curFile);
                IOUtils.copy(myTarFile, out);
                out.close();
                entry = myTarFile.getNextTarEntry();
            }
            myTarFile.close();

        }
    }

    return Response.ok().build();
}

From source file:heigit.ors.routing.graphhopper.extensions.reader.borders.CountryBordersReader.java

/**
 * Method to read the geometries from a GeoJSON file that represent the boundaries of different countries. Ideally
 * it should be written using many small objects split into hierarchies.
 *
 * If the file is a .tar.gz format, it will decompress it and then store the reulting data to be read into the
 * JSON object./*  ww  w .  j av  a2 s  .co  m*/
 *
 * @return      A (Geo)JSON object representing the contents of the file
 */
private JSONObject readBordersData() throws IOException {
    String data = "";

    InputStream is = null;
    BufferedReader buf = null;
    try {
        is = new FileInputStream(BORDER_FILE);

        if (BORDER_FILE.endsWith(".tar.gz")) {
            // We are working with a compressed file
            TarArchiveInputStream tis = new TarArchiveInputStream(
                    new GzipCompressorInputStream(new BufferedInputStream(is)));

            TarArchiveEntry entry;
            StringBuilder sb = new StringBuilder();

            while ((entry = tis.getNextTarEntry()) != null) {
                if (!entry.isDirectory()) {
                    byte[] bytes = new byte[(int) entry.getSize()];
                    tis.read(bytes);
                    String str = new String(bytes);
                    sb.append(str);
                }
            }
            data = sb.toString();
        } else {
            // Assume a normal file so read line by line

            buf = new BufferedReader(new InputStreamReader(is));

            String line = "";
            StringBuilder sb = new StringBuilder();

            while ((line = buf.readLine()) != null) {
                sb.append(line);
            }

            data = sb.toString();
        }
    } catch (IOException ioe) {
        LOGGER.warn("Cannot access borders file!");
        throw ioe;
    } finally {
        try {
            if (is != null)
                is.close();
            if (buf != null)
                buf.close();
        } catch (IOException ioe) {
            LOGGER.warn("Error closing file reader buffers!");
        } catch (NullPointerException npe) {
            // This can happen if the file itself wasn't available
            throw new IOException("Borders file " + BORDER_FILE + " not found!");
        }
    }

    JSONObject json = new JSONObject(data);

    return json;
}

From source file:ezbake.deployer.publishers.artifact.PythonRequirementsPublisher.java

@Override
public Collection<ArtifactDataEntry> generateEntries(DeploymentArtifact artifact) throws DeploymentException {

    // load the tarball
    TarArchiveInputStream tarIn = null;
    try {/*from   w ww .ja v  a2  s  .c  o  m*/
        byte[] a = artifact.getArtifact();
        tarIn = new TarArchiveInputStream(new GZIPInputStream(new ByteArrayInputStream(a)));
    } catch (IOException e) {
        e.printStackTrace();
    }

    // collect the packages
    // (pip fails if there are duplicate package names, so we're using map
    // to avoid repeated packages)
    TarArchiveEntry entry = null;
    Map<String, String> requirements = new HashMap<String, String>();
    do {
        try {
            if (tarIn != null) {
                entry = tarIn.getNextTarEntry();
            }
        } catch (IOException e) {
            entry = null;
        }

        // get the name of the current entry
        String name = null;
        if (entry != null) {
            name = entry.getName();
        }

        if ((entry != null) && name.startsWith("./wheels/")) {
            String[] parts = name.split("/");
            String basename = parts[parts.length - 1];

            String[] p = extractPackageInfo(basename);

            if ((p != null) && (p.length == 2)) {
                requirements.put(p[0], p[1]);
            }
        }
    } while (entry != null);

    // create requirements.txt output
    StringBuffer out = new StringBuffer();
    out.append("# commands for pip\n");
    out.append("--no-index\n");
    out.append("--use-wheels\n");
    out.append("--find-links ./wheels/\n\n");

    out.append("# requirements\n");
    for (Map.Entry<String, String> p : requirements.entrySet()) {
        out.append(p.getKey()); // package name
        out.append("==");
        out.append(p.getValue()); // package version
        out.append("\n");
    }

    return Lists.newArrayList(
            new ArtifactDataEntry(new TarArchiveEntry("requirements.txt"), out.toString().getBytes()));
}