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:examples.utils.CifarReader.java

public static void downloadAndExtract() {

    if (new File("data", TEST_DATA_FILE).exists() == false) {
        try {/*ww  w  .j a  v a  2s  .c  o  m*/
            if (new File("data", ARCHIVE_BINARY_FILE).exists() == false) {
                URL website = new URL("http://www.cs.toronto.edu/~kriz/" + ARCHIVE_BINARY_FILE);
                FileOutputStream fos = new FileOutputStream("data/" + ARCHIVE_BINARY_FILE);
                fos.getChannel().transferFrom(Channels.newChannel(website.openStream()), 0, Long.MAX_VALUE);
                fos.close();
            }
            TarArchiveInputStream tar = new TarArchiveInputStream(
                    new GZIPInputStream(new FileInputStream("data/" + ARCHIVE_BINARY_FILE)));
            TarArchiveEntry entry = null;
            while ((entry = tar.getNextTarEntry()) != null) {
                if (entry.isDirectory()) {
                    new File("data", entry.getName()).mkdirs();
                } else {
                    byte data[] = new byte[2048];
                    int count;
                    BufferedOutputStream bos = new BufferedOutputStream(
                            new FileOutputStream(new File("data/", entry.getName())), 2048);

                    while ((count = tar.read(data, 0, 2048)) != -1) {
                        bos.write(data, 0, count);
                    }
                    bos.close();
                }
            }
            tar.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

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

/**
 * @param archive/* ww w  .j  a 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:io.crate.testing.Utils.java

static void uncompressTarGZ(File tarFile, File dest) throws IOException {
    TarArchiveInputStream tarIn = new TarArchiveInputStream(
            new GzipCompressorInputStream(new BufferedInputStream(new FileInputStream(tarFile))));

    TarArchiveEntry tarEntry = tarIn.getNextTarEntry();
    // tarIn is a TarArchiveInputStream
    while (tarEntry != null) {
        Path entryPath = Paths.get(tarEntry.getName());

        if (entryPath.getNameCount() == 1) {
            tarEntry = tarIn.getNextTarEntry();
            continue;
        }//from  w  w  w. ja v a2s  .c o  m

        Path strippedPath = entryPath.subpath(1, entryPath.getNameCount());
        File destPath = new File(dest, strippedPath.toString());

        if (tarEntry.isDirectory()) {
            destPath.mkdirs();
        } else {
            destPath.createNewFile();
            byte[] btoRead = new byte[1024];
            BufferedOutputStream bout = new BufferedOutputStream(new FileOutputStream(destPath));
            int len;
            while ((len = tarIn.read(btoRead)) != -1) {
                bout.write(btoRead, 0, len);
            }

            bout.close();
            if (destPath.getParent().equals(dest.getPath() + "/bin")) {
                destPath.setExecutable(true);
            }
        }
        tarEntry = tarIn.getNextTarEntry();
    }
    tarIn.close();
}

From source file:com.puppetlabs.geppetto.forge.util.TarUtils.java

/**
 * Unpack the content read from <i>source</i> into <i>targetFolder</i>. If the
 * <i>skipTopFolder</i> is set, then don't assume that the archive contains one
 * single folder and unpack the content of that folder, not including the folder
 * itself.//from  www .  j  a va2  s .  co  m
 * 
 * @param source
 *            The input source. Must be in <i>TAR</i> format.
 * @param targetFolder
 *            The destination folder for the unpack. Not used when a <tt>fileCatcher</tt> is provided
 * @param skipTopFolder
 *            Set to <code>true</code> to unpack beneath the top folder
 *            of the archive. The archive must consist of one single folder and nothing else
 *            in order for this to work.
 * @param fileCatcher
 *            Used when specific files should be picked from the archive without writing them to disk. Can be
 *            <tt>null</tt>.
 * @throws IOException
 */
public static void unpack(InputStream source, File targetFolder, boolean skipTopFolder, FileCatcher fileCatcher)
        throws IOException {
    String topFolderName = null;
    Map<File, Map<Integer, List<String>>> chmodMap = new HashMap<File, Map<Integer, List<String>>>();
    TarArchiveInputStream in = new TarArchiveInputStream(source);
    try {
        TarArchiveEntry te = in.getNextTarEntry();
        if (te == null) {
            throw new IOException("No entry in the tar file");
        }
        do {
            if (te.isGlobalPaxHeader())
                continue;

            String name = te.getName();
            if (skipTopFolder) {
                int firstSlash = name.indexOf('/');
                if (firstSlash < 0)
                    throw new IOException("Archive doesn't contain one single folder");

                String tfName = name.substring(0, firstSlash);
                if (topFolderName == null)
                    topFolderName = tfName;
                else if (!tfName.equals(topFolderName))
                    throw new IOException("Archive doesn't contain one single folder");
                name = name.substring(firstSlash + 1);
            }
            if (name.length() == 0)
                continue;

            String linkName = te.getLinkName();
            if (linkName != null) {
                if (linkName.trim().equals(""))
                    linkName = null;
            }

            if (fileCatcher != null) {
                if (linkName == null && !te.isDirectory() && fileCatcher.accept(name)) {
                    if (fileCatcher.catchData(name, in))
                        // We're done here
                        return;
                }
                continue;
            }

            File outFile = new File(targetFolder, name);
            if (linkName != null) {
                if (!OsUtil.link(targetFolder, name, te.getLinkName()))
                    throw new IOException("Archive contains links but they are not supported on this platform");
            } else {
                if (te.isDirectory()) {
                    outFile.mkdirs();
                } else {
                    outFile.getParentFile().mkdirs();
                    OutputStream target = new FileOutputStream(outFile);
                    StreamUtil.copy(in, target);
                    target.close();
                    outFile.setLastModified(te.getModTime().getTime());
                }
                registerChmodFile(chmodMap, targetFolder, Integer.valueOf(te.getMode()), name);
            }
        } while ((te = in.getNextTarEntry()) != null);
    } finally {
        StreamUtil.close(in);
    }
    chmod(chmodMap);
}

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

/**
 * @param archive/* w w  w  . j  av  a 2  s  .  c om*/
 * @return
 * @throws IOException
 */
private static final HashMap<String, TarArchiveEntry> getMembers(String archive) throws IOException {
    TarArchiveInputStream input = new TarArchiveInputStream(
            new BufferedInputStream(new FileInputStream(archive)));

    TarArchiveEntry entry;
    HashMap<String, TarArchiveEntry> map = new HashMap<>();
    while ((entry = input.getNextTarEntry()) != null) {
        map.put(entry.getName(), entry);
    }
    input.close();
    return map;
}

From source file:com.dubture.symfony.core.util.UncompressUtils.java

/**
 * Uncompress all entries found in a tar archive file into the given output
 * directory. Entry names are translated using the translator before being
 * put on the file system.//from   ww w  .ja  v  a  2  s  .  c o  m
 *
 * @param archiveFile The tar archive file to uncompress
 * @param outputDirectory The output directory where to put uncompressed entries
 * @param entryNameTranslator The entry name translator to use
 *
 * @throws IOException When an error occurs while uncompressing the tar archive
 */
public static void uncompressTarArchive(File archiveFile, File outputDirectory,
        EntryNameTranslator entryNameTranslator) throws IOException {
    FileInputStream fileInputStream = new FileInputStream(archiveFile);
    BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
    TarArchiveInputStream tarInputStream = new TarArchiveInputStream(bufferedInputStream);

    try {
        TarArchiveEntry tarEntry = null;
        while ((tarEntry = tarInputStream.getNextTarEntry()) != null) {
            uncompressTarArchiveEntry(tarInputStream, tarEntry, outputDirectory, entryNameTranslator);
        }
    } finally {
        tarInputStream.close();
    }
}

From source file:com.impetus.ankush.common.utils.FileNameUtils.java

/**
 * Gets the extracted directory name./*  www  . ja va 2  s. c o  m*/
 * 
 * @param archiveFile
 *            the archive file
 * @return Directory name after extraction of archiveFile
 */
public static String getExtractedDirectoryName(String archiveFile) {
    String path = null;
    ONSFileType fileType = getFileType(archiveFile);

    if (fileType == ONSFileType.TAR_GZ || fileType == ONSFileType.GZ) {
        try {
            GZIPInputStream gzipInputStream = null;
            gzipInputStream = new GZIPInputStream(new FileInputStream(archiveFile));
            TarArchiveInputStream tarInput = new TarArchiveInputStream(gzipInputStream);

            return tarInput.getNextTarEntry().getName();

        } catch (FileNotFoundException e) {
            LOG.error(e.getMessage(), e);
        } catch (IOException e) {
            LOG.error(e.getMessage(), e);
        }
    } else if (fileType == ONSFileType.ZIP || fileType == ONSFileType.BIN) {
        ZipFile zf;
        try {
            zf = new ZipFile(archiveFile);
            Enumeration<? extends ZipEntry> entries = zf.entries();

            return entries.nextElement().getName();
        } catch (IOException e) {
            LOG.error(e.getMessage(), e);
        }
    }

    return path;
}

From source file:it.geosolutions.tools.compress.file.reader.TarReader.java

/**
 * A method to read tar file://from w  ww.j a v a  2s  . c  o  m
 * extract its content to the 'destDir' file (which could be
 * a directory or a file depending on the srcF file content)
 * @throws CompressorException 
 */
public static void readTar(File srcF, File destDir) throws Exception {
    if (destDir == null)
        throw new IllegalArgumentException("Unable to extract to a null destination dir");
    if (!destDir.canWrite() && !destDir.mkdirs())
        throw new IllegalArgumentException("Unable to extract to a not writeable destination dir: " + destDir);

    FileInputStream fis = null;
    TarArchiveInputStream tis = null;
    BufferedInputStream bis = null;
    try {
        fis = new FileInputStream(srcF);
        bis = new BufferedInputStream(fis);
        tis = new TarArchiveInputStream(bis);

        TarArchiveEntry te = null;
        while ((te = tis.getNextTarEntry()) != null) {

            File curr_dest = new File(destDir, te.getName());
            if (te.isDirectory()) {
                // create destination folder
                if (!curr_dest.exists())
                    curr_dest.mkdirs();
            } else {
                writeFile(curr_dest, tis);
            }
        }
    } finally {
        if (tis != null) {
            try {
                tis.close();
            } catch (IOException ioe) {
            }
        }
        if (bis != null) {
            try {
                bis.close();
            } catch (IOException ioe) {
            }
        }
        if (fis != null) {
            try {
                fis.close();
            } catch (IOException ioe) {
            }
        }

    }
}

From source file:cgs_lda_multicore.DataModel.DataPreparation.java

public static LDADataset readDatasetCORE(String fileName) throws Exception {
    try {/*from   ww w  . j a va2 s. c o m*/
        // Read document file.
        BufferedReader reader = null;

        if (fileName.endsWith(".tar.gz")) {
            // This case read from .tar.gz file.
            TarArchiveInputStream tAIS = new TarArchiveInputStream(
                    new GZIPInputStream(new FileInputStream(fileName)));
            TarArchiveEntry tarArchiveEntry;

            while ((tarArchiveEntry = tAIS.getNextTarEntry()) != null) {
                if (tarArchiveEntry.isFile()) {
                    reader = new BufferedReader(
                            new InputStreamReader(new FileInputStream(tarArchiveEntry.getFile()), "UTF-8"));
                    String line;

                    while ((line = reader.readLine()) != null) {
                        // Process line, each line is a json of a document.
                    }
                    reader.close();
                }
            }
            tAIS.close();
        }
        return null;
    } catch (Exception e) {
        System.out.println("Read Dataset Error: " + e.getMessage());
        e.printStackTrace();
        return null;
    }
}

From source file:com.impetus.ankush.common.utils.FileNameUtils.java

/**
 * Gets the path from archive.//from www.j a  v a  2 s.  c  o m
 * 
 * @param archiveFile
 *            the archive file
 * @param charSequence
 *            the char sequence
 * @return the path from archive
 */
public static String getPathFromArchive(String archiveFile, String charSequence) {
    String path = null;
    ONSFileType fileType = getFileType(archiveFile);

    if (fileType == ONSFileType.TAR_GZ) {
        try {
            GZIPInputStream gzipInputStream = null;
            gzipInputStream = new GZIPInputStream(new FileInputStream(archiveFile));
            TarArchiveInputStream tarInput = new TarArchiveInputStream(gzipInputStream);

            TarArchiveEntry entry;
            while (null != (entry = tarInput.getNextTarEntry())) {
                if (entry.getName().contains(charSequence)) {
                    path = entry.getName();
                    break;
                }
            }

        } catch (FileNotFoundException e) {
            LOG.error(e.getMessage(), e);
        } catch (IOException e) {
            LOG.error(e.getMessage(), e);
        }
    } else if (fileType == ONSFileType.ZIP) {
        ZipFile zf;
        try {
            zf = new ZipFile(archiveFile);
            Enumeration<? extends ZipEntry> entries = zf.entries();
            String fileName;
            while (entries.hasMoreElements()) {
                fileName = entries.nextElement().getName();
                if (fileName.contains(charSequence)) {
                    path = fileName;
                    break;
                }
            }
        } catch (IOException e) {
            LOG.error(e.getMessage(), e);
        }
    }

    return path;
}