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);
}
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 + ".");
}
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);
}
}
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();
}
}
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;
}
}
Aggregations