Search in sources :

Example 6 with GzipCompressorInputStream

use of org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream 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 7 with GzipCompressorInputStream

use of org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream 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 8 with GzipCompressorInputStream

use of org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream in project knime-core by knime.

the class ExtractTarGz method execute.

@Override
public IStatus execute(final Map<String, Object> parameters) {
    try {
        String source = readParameter(parameters, SOURCE_ARCHIVE);
        String targetDir = readParameter(parameters, TARGET_DIR);
        File destDir;
        Path targetDirPath = Paths.get(targetDir);
        if (targetDirPath.isAbsolute()) {
            // targetDir is absolute path or contains @artifact
            destDir = targetDirPath.toFile();
        } else {
            // targetDir is something like '.'
            IProfile profile = (IProfile) parameters.get("profile");
            File installFolder = new File(profile.getProperty(IProfile.PROP_INSTALL_FOLDER));
            destDir = new File(installFolder, targetDir);
        }
        try (InputStream fileInputStream = FileUtil.openInputStream(source)) {
            InputStream in;
            if (StringUtils.endsWithIgnoreCase(source, ".tar.gz") || StringUtils.endsWithIgnoreCase(source, ".tgz")) {
                in = new GzipCompressorInputStream(fileInputStream);
            } else if (StringUtils.endsWithIgnoreCase(source, ".tar.bz2")) {
                in = new BZip2CompressorInputStream(fileInputStream);
            } else if (StringUtils.endsWithIgnoreCase(source, ".tar.xz")) {
                in = new XZCompressorInputStream(fileInputStream);
            } else {
                in = fileInputStream;
            }
            untar(in, destDir);
        }
        return Status.OK_STATUS;
    } catch (Throwable e) {
        return new Status(IStatus.ERROR, bundle.getSymbolicName(), e.getMessage(), e);
    }
}
Also used : Path(java.nio.file.Path) GzipCompressorInputStream(org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream) IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) BZip2CompressorInputStream(org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) XZCompressorInputStream(org.apache.commons.compress.compressors.xz.XZCompressorInputStream) BZip2CompressorInputStream(org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream) GzipCompressorInputStream(org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream) InputStream(java.io.InputStream) IProfile(org.eclipse.equinox.p2.engine.IProfile) File(java.io.File) XZCompressorInputStream(org.apache.commons.compress.compressors.xz.XZCompressorInputStream)

Example 9 with GzipCompressorInputStream

use of org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream 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 10 with GzipCompressorInputStream

use of org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream in project twister2 by DSC-SPIDAL.

the class TarGzipPacker method addTarGzipToArchive.

/**
 * given tar.gz file will be copied to this tar.gz file.
 * all files will be transferred to new tar.gz file one by one.
 * original directory structure will be kept intact
 *
 * @param tarGzipFile the archive file to be copied to the new archive
 */
public boolean addTarGzipToArchive(String tarGzipFile) {
    try {
        // construct input stream
        InputStream fin = Files.newInputStream(Paths.get(tarGzipFile));
        BufferedInputStream in = new BufferedInputStream(fin);
        GzipCompressorInputStream gzIn = new GzipCompressorInputStream(in);
        TarArchiveInputStream tarInputStream = new TarArchiveInputStream(gzIn);
        // copy the existing entries from source gzip file
        ArchiveEntry nextEntry;
        while ((nextEntry = tarInputStream.getNextEntry()) != null) {
            tarOutputStream.putArchiveEntry(nextEntry);
            IOUtils.copy(tarInputStream, tarOutputStream);
            tarOutputStream.closeArchiveEntry();
        }
        tarInputStream.close();
        return true;
    } catch (IOException ioe) {
        LOG.log(Level.SEVERE, "Archive File can not be added: " + tarGzipFile, ioe);
        return false;
    }
}
Also used : GzipCompressorInputStream(org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) FileInputStream(java.io.FileInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) GzipCompressorInputStream(org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream) InputStream(java.io.InputStream) ArchiveEntry(org.apache.commons.compress.archivers.ArchiveEntry) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry) IOException(java.io.IOException)

Aggregations

GzipCompressorInputStream (org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream)58 TarArchiveInputStream (org.apache.commons.compress.archivers.tar.TarArchiveInputStream)46 TarArchiveEntry (org.apache.commons.compress.archivers.tar.TarArchiveEntry)40 IOException (java.io.IOException)29 FileInputStream (java.io.FileInputStream)26 File (java.io.File)23 BufferedInputStream (java.io.BufferedInputStream)22 FileOutputStream (java.io.FileOutputStream)20 InputStream (java.io.InputStream)16 OutputStream (java.io.OutputStream)10 Path (java.nio.file.Path)9 ArrayList (java.util.ArrayList)8 BufferedOutputStream (java.io.BufferedOutputStream)7 ByteArrayInputStream (java.io.ByteArrayInputStream)7 BZip2CompressorInputStream (org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream)6 BufferedReader (java.io.BufferedReader)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 InputStreamReader (java.io.InputStreamReader)4 URL (java.net.URL)4 ArchiveEntry (org.apache.commons.compress.archivers.ArchiveEntry)4