Search in sources :

Example 11 with TarArchiveInputStream

use of org.apache.commons.compress.archivers.tar.TarArchiveInputStream in project incubator-systemml by apache.

the class ValidateLicAndNotice method getFilesFromTGZ.

/**
 * 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.
 */
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);
}
Also used : TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) GzipCompressorInputStream(org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream) BufferedInputStream(java.io.BufferedInputStream) ArrayList(java.util.ArrayList) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry)

Example 12 with TarArchiveInputStream

use of org.apache.commons.compress.archivers.tar.TarArchiveInputStream in project ignite by apache.

the class IgniteHadoopTestSuite method download.

/**
 *  Downloads and extracts an Apache product.
 *
 * @param appName Name of application for log messages.
 * @param homeVariable Pointer to home directory of the component.
 * @param downloadPath Relative download path of tar package.
 * @param destName Local directory name to install component.
 * @throws Exception If failed.
 */
private static void download(String appName, String homeVariable, String downloadPath, String destName) throws Exception {
    String homeVal = IgniteSystemProperties.getString(homeVariable);
    if (!F.isEmpty(homeVal) && new File(homeVal).isDirectory()) {
        X.println(homeVariable + " is set to: " + homeVal);
        return;
    }
    List<String> urls = F.asList("http://archive.apache.org/dist/", "http://apache-mirror.rbc.ru/pub/apache/", "http://www.eu.apache.org/dist/", "http://www.us.apache.org/dist/");
    String tmpPath = System.getProperty("java.io.tmpdir");
    X.println("tmp: " + tmpPath);
    final File install = new File(tmpPath + File.separatorChar + "__hadoop");
    final File home = new File(install, destName);
    X.println("Setting " + homeVariable + " to " + home.getAbsolutePath());
    System.setProperty(homeVariable, home.getAbsolutePath());
    final File successFile = new File(home, "__success");
    if (home.exists()) {
        if (successFile.exists()) {
            X.println(appName + " distribution already exists.");
            return;
        }
        X.println(appName + " distribution is invalid and it will be deleted.");
        if (!U.delete(home))
            throw new IOException("Failed to delete directory: " + home.getAbsolutePath());
    }
    for (String url : urls) {
        if (!(install.exists() || install.mkdirs()))
            throw new IOException("Failed to create directory: " + install.getAbsolutePath());
        URL u = new URL(url + downloadPath);
        X.println("Attempting to download from: " + u);
        try {
            URLConnection c = u.openConnection();
            c.connect();
            try (TarArchiveInputStream in = new TarArchiveInputStream(new GzipCompressorInputStream(new BufferedInputStream(c.getInputStream(), 32 * 1024)))) {
                TarArchiveEntry entry;
                while ((entry = in.getNextTarEntry()) != null) {
                    File dest = new File(install, entry.getName());
                    if (entry.isDirectory()) {
                        if (!dest.mkdirs())
                            throw new IllegalStateException();
                    } else if (entry.isSymbolicLink()) {
                        // Important: in Hadoop installation there are symlinks, we need to create them:
                        Path theLinkItself = Paths.get(install.getAbsolutePath(), entry.getName());
                        Path linkTarget = Paths.get(entry.getLinkName());
                        Files.createSymbolicLink(theLinkItself, linkTarget);
                    } else {
                        File parent = dest.getParentFile();
                        if (!(parent.exists() || parent.mkdirs()))
                            throw new IllegalStateException();
                        X.print(" [" + dest);
                        try (BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(dest, false), 128 * 1024)) {
                            U.copy(in, out);
                            out.flush();
                        }
                        Files.setPosixFilePermissions(dest.toPath(), modeToPermissionSet(entry.getMode()));
                        X.println("]");
                    }
                }
            }
            if (successFile.createNewFile())
                return;
        } catch (Exception e) {
            e.printStackTrace();
            U.delete(home);
        }
    }
    throw new IllegalStateException("Failed to install " + appName + ".");
}
Also used : GzipCompressorInputStream(org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream) Path(java.nio.file.Path) IOException(java.io.IOException) URL(java.net.URL) URLConnection(java.net.URLConnection) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry) IOException(java.io.IOException) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) BufferedInputStream(java.io.BufferedInputStream) FileOutputStream(java.io.FileOutputStream) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream)

Example 13 with TarArchiveInputStream

use of org.apache.commons.compress.archivers.tar.TarArchiveInputStream in project knime-core by knime.

the class ExtractTarGz method untar.

private static void untar(final InputStream in, final File destDir) throws IOException {
    try (TarArchiveInputStream tarInS = new TarArchiveInputStream(in)) {
        TarArchiveEntry entry;
        while ((entry = tarInS.getNextTarEntry()) != null) {
            String name = entry.getName();
            File destFile = new File(destDir, name);
            if (entry.isSymbolicLink()) {
                Files.createSymbolicLink(destFile.toPath(), Paths.get(entry.getLinkName()));
            } else if (entry.isDirectory()) {
                destFile.mkdirs();
                chmod(destFile, entry.getMode());
            } else {
                try (FileOutputStream out = new FileOutputStream(destFile)) {
                    long size = entry.getSize();
                    IOUtils.copyLarge(tarInS, out, 0, size);
                }
                chmod(destFile, entry.getMode());
            }
        }
    }
}
Also used : TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) FileOutputStream(java.io.FileOutputStream) File(java.io.File) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry)

Example 14 with TarArchiveInputStream

use of org.apache.commons.compress.archivers.tar.TarArchiveInputStream in project AmazeFileManager by TeamAmaze.

the class GzipHelperTask method addElements.

@Override
void addElements(ArrayList<CompressedObjectParcelable> elements) {
    TarArchiveInputStream tarInputStream = null;
    try {
        tarInputStream = new TarArchiveInputStream(new GzipCompressorInputStream(new FileInputStream(filePath)));
        TarArchiveEntry entry;
        while ((entry = tarInputStream.getNextTarEntry()) != null) {
            String name = entry.getName();
            if (name.endsWith(SEPARATOR))
                name = name.substring(0, name.length() - 1);
            boolean isInBaseDir = relativePath.equals("") && !name.contains(SEPARATOR);
            boolean isInRelativeDir = name.contains(SEPARATOR) && name.substring(0, name.lastIndexOf(SEPARATOR)).equals(relativePath);
            if (isInBaseDir || isInRelativeDir) {
                elements.add(new CompressedObjectParcelable(entry.getName(), entry.getLastModifiedDate().getTime(), entry.getSize(), entry.isDirectory()));
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Also used : TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) GzipCompressorInputStream(org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream) CompressedObjectParcelable(com.amaze.filemanager.adapters.data.CompressedObjectParcelable) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry)

Example 15 with TarArchiveInputStream

use of org.apache.commons.compress.archivers.tar.TarArchiveInputStream in project AmazeFileManager by TeamAmaze.

the class TarHelperTask method addElements.

@Override
void addElements(ArrayList<CompressedObjectParcelable> elements) {
    TarArchiveInputStream tarInputStream = null;
    try {
        tarInputStream = new TarArchiveInputStream(new FileInputStream(filePath));
        TarArchiveEntry entry;
        while ((entry = tarInputStream.getNextTarEntry()) != null) {
            String name = entry.getName();
            if (name.endsWith(SEPARATOR))
                name = name.substring(0, name.length() - 1);
            boolean isInBaseDir = relativePath.equals("") && !name.contains(SEPARATOR);
            boolean isInRelativeDir = name.contains(SEPARATOR) && name.substring(0, name.lastIndexOf(SEPARATOR)).equals(relativePath);
            if (isInBaseDir || isInRelativeDir) {
                elements.add(new CompressedObjectParcelable(entry.getName(), entry.getLastModifiedDate().getTime(), entry.getSize(), entry.isDirectory()));
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Also used : TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) CompressedObjectParcelable(com.amaze.filemanager.adapters.data.CompressedObjectParcelable) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry)

Aggregations

TarArchiveInputStream (org.apache.commons.compress.archivers.tar.TarArchiveInputStream)131 TarArchiveEntry (org.apache.commons.compress.archivers.tar.TarArchiveEntry)98 File (java.io.File)51 IOException (java.io.IOException)49 FileInputStream (java.io.FileInputStream)45 GzipCompressorInputStream (org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream)45 InputStream (java.io.InputStream)34 FileOutputStream (java.io.FileOutputStream)33 BufferedInputStream (java.io.BufferedInputStream)30 ByteArrayInputStream (java.io.ByteArrayInputStream)27 Test (org.junit.Test)23 ArrayList (java.util.ArrayList)20 GZIPInputStream (java.util.zip.GZIPInputStream)20 ByteArrayOutputStream (java.io.ByteArrayOutputStream)19 ArchiveEntry (org.apache.commons.compress.archivers.ArchiveEntry)17 Path (java.nio.file.Path)16 OutputStream (java.io.OutputStream)15 BufferedOutputStream (java.io.BufferedOutputStream)12 ArchiveStreamFactory (org.apache.commons.compress.archivers.ArchiveStreamFactory)12 ArchiveInputStream (org.apache.commons.compress.archivers.ArchiveInputStream)7