use of org.apache.commons.compress.archivers.tar.TarArchiveInputStream in project zeppelin by apache.
the class HeliumBundleFactory method unTgz.
private static List<String> unTgz(File tarFile, File directory) throws IOException {
List<String> result = new ArrayList<>();
try (TarArchiveInputStream in = new TarArchiveInputStream(new GzipCompressorInputStream(new FileInputStream(tarFile)))) {
TarArchiveEntry entry = in.getNextTarEntry();
while (entry != null) {
if (entry.isDirectory()) {
entry = in.getNextTarEntry();
continue;
}
File curfile = new File(directory, entry.getName());
File parent = curfile.getParentFile();
if (!parent.exists()) {
parent.mkdirs();
}
try (OutputStream out = new FileOutputStream(curfile)) {
IOUtils.copy(in, out);
}
result.add(entry.getName());
entry = in.getNextTarEntry();
}
}
return result;
}
use of org.apache.commons.compress.archivers.tar.TarArchiveInputStream in project zeppelin by apache.
the class TarUtils method decompress.
public static void decompress(String in, File out) throws IOException {
FileInputStream fileInputStream = new FileInputStream(in);
GzipCompressorInputStream gzipInputStream = new GzipCompressorInputStream(fileInputStream);
try (TarArchiveInputStream fin = new TarArchiveInputStream(gzipInputStream)) {
TarArchiveEntry entry;
while ((entry = fin.getNextTarEntry()) != null) {
if (entry.isDirectory()) {
continue;
}
File curfile = new File(out, entry.getName());
File parent = curfile.getParentFile();
if (!parent.exists()) {
parent.mkdirs();
}
IOUtils.copy(fin, new FileOutputStream(curfile));
}
}
}
use of org.apache.commons.compress.archivers.tar.TarArchiveInputStream in project hive by apache.
the class CompressionUtils method unTar.
/**
* Untar an input file into an output file.
*
* The output file is created in the output folder, having the same name as the input file, minus
* the '.tar' extension.
*
* @param inputFileName the input .tar file
* @param outputDirName the output directory file.
* @throws IOException
* @throws FileNotFoundException
*
* @return The {@link List} of {@link File}s with the untared content.
* @throws ArchiveException
*/
public static List<File> unTar(final String inputFileName, final String outputDirName, boolean flatten) throws FileNotFoundException, IOException, ArchiveException {
File inputFile = new File(inputFileName);
File outputDir = new File(outputDirName);
final List<File> untaredFiles = new LinkedList<File>();
InputStream is = null;
try {
if (inputFileName.endsWith(".gz")) {
is = new GzipCompressorInputStream(new FileInputStream(inputFile));
} else {
is = new FileInputStream(inputFile);
}
final TarArchiveInputStream debInputStream = (TarArchiveInputStream) new ArchiveStreamFactory().createArchiveInputStream("tar", is);
TarArchiveEntry entry = null;
while ((entry = (TarArchiveEntry) debInputStream.getNextEntry()) != null) {
final File outputFile = new File(outputDir, entry.getName());
if (!outputFile.toPath().toAbsolutePath().normalize().startsWith(outputDir.toPath().toAbsolutePath().normalize())) {
throw new IOException("Untarred file is not under the output directory");
}
if (entry.isDirectory()) {
if (flatten) {
// no sub-directories
continue;
}
LOG.debug(String.format("Attempting to write output directory %s.", outputFile.getAbsolutePath()));
if (!outputFile.exists()) {
LOG.debug(String.format("Attempting to create output directory %s.", outputFile.getAbsolutePath()));
if (!outputFile.mkdirs()) {
throw new IllegalStateException(String.format("Couldn't create directory %s.", outputFile.getAbsolutePath()));
}
}
} else {
final OutputStream outputFileStream;
if (flatten) {
File flatOutputFile = new File(outputDir, outputFile.getName());
LOG.debug(String.format("Creating flat output file %s.", flatOutputFile.getAbsolutePath()));
outputFileStream = new FileOutputStream(flatOutputFile);
} else if (!outputFile.getParentFile().exists()) {
LOG.debug(String.format("Attempting to create output directory %s.", outputFile.getParentFile().getAbsoluteFile()));
if (!outputFile.getParentFile().getAbsoluteFile().mkdirs()) {
throw new IllegalStateException(String.format("Couldn't create directory %s.", outputFile.getParentFile().getAbsolutePath()));
}
LOG.debug(String.format("Creating output file %s.", outputFile.getAbsolutePath()));
outputFileStream = new FileOutputStream(outputFile);
} else {
outputFileStream = new FileOutputStream(outputFile);
}
IOUtils.copy(debInputStream, outputFileStream);
outputFileStream.close();
}
untaredFiles.add(outputFile);
}
debInputStream.close();
return untaredFiles;
} finally {
if (is != null)
is.close();
}
}
use of org.apache.commons.compress.archivers.tar.TarArchiveInputStream in project packr by libgdx.
the class ArchiveUtils method extractTarArchive.
/**
* Extracts a TAR archive. If the current platform supports POSIX permissions, the archive entry permissions are applied to the create file or directory.
* Symbolic and "hard" links are also support.
*
* @param inputStream the archive input stream
* @param extractToDirectory the directory to extract the archive into
* @throws IOException if an IO error occurs
*/
private static void extractTarArchive(InputStream inputStream, Path extractToDirectory) throws IOException {
final TarArchiveInputStream archiveInputStream = new TarArchiveInputStream(inputStream);
TarArchiveEntry entry;
while ((entry = archiveInputStream.getNextTarEntry()) != null) {
if (!archiveInputStream.canReadEntryData(entry)) {
LOG.error("Failed to read archive entry " + entry);
continue;
}
Path entryExtractPath = extractToDirectory.resolve(getEntryAsPath(entry));
if (entry.isLink()) {
Path linkTarget = Paths.get(entry.getLinkName());
Files.deleteIfExists(entryExtractPath);
Files.createLink(entryExtractPath, linkTarget);
} else if (entry.isSymbolicLink()) {
Path linkTarget = Paths.get(entry.getLinkName());
Files.deleteIfExists(entryExtractPath);
Files.createSymbolicLink(entryExtractPath, linkTarget);
} else {
if (entry.isDirectory()) {
Files.createDirectories(entryExtractPath);
} else {
Files.createDirectories(entryExtractPath.getParent());
Files.copy(archiveInputStream, entryExtractPath, StandardCopyOption.REPLACE_EXISTING);
}
}
setLastModifiedTime(entryExtractPath, FileTime.fromMillis(entry.getLastModifiedDate().getTime()));
Set<PosixFilePermission> permissions = getPosixFilePermissions(entry);
setPosixPermissions(entryExtractPath, permissions);
}
}
use of org.apache.commons.compress.archivers.tar.TarArchiveInputStream in project uPortal by Jasig.
the class JaxbPortalDataHandlerService method importDataArchive.
private void importDataArchive(Resource archive, InputStream resourceStream, BatchImportOptions options) {
BufferedInputStream bufferedResourceStream = null;
try {
// Make sure the stream is buffered
if (resourceStream instanceof BufferedInputStream) {
bufferedResourceStream = (BufferedInputStream) resourceStream;
} else {
bufferedResourceStream = new BufferedInputStream(resourceStream);
}
// Buffer up to 100MB, bad things will happen if we bust this buffer.
// TODO see if there is a buffered stream that will write to a file once the buffer
// fills up
bufferedResourceStream.mark(100 * 1024 * 1024);
final MediaType type = getMediaType(bufferedResourceStream, archive.getFilename());
if (MT_JAVA_ARCHIVE.equals(type)) {
final ArchiveInputStream archiveStream = new JarArchiveInputStream(bufferedResourceStream);
importDataArchive(archive, archiveStream, options);
} else if (MediaType.APPLICATION_ZIP.equals(type)) {
final ArchiveInputStream archiveStream = new ZipArchiveInputStream(bufferedResourceStream);
importDataArchive(archive, archiveStream, options);
} else if (MT_CPIO.equals(type)) {
final ArchiveInputStream archiveStream = new CpioArchiveInputStream(bufferedResourceStream);
importDataArchive(archive, archiveStream, options);
} else if (MT_AR.equals(type)) {
final ArchiveInputStream archiveStream = new ArArchiveInputStream(bufferedResourceStream);
importDataArchive(archive, archiveStream, options);
} else if (MT_TAR.equals(type)) {
final ArchiveInputStream archiveStream = new TarArchiveInputStream(bufferedResourceStream);
importDataArchive(archive, archiveStream, options);
} else if (MT_BZIP2.equals(type)) {
final CompressorInputStream compressedStream = new BZip2CompressorInputStream(bufferedResourceStream);
importDataArchive(archive, compressedStream, options);
} else if (MT_GZIP.equals(type)) {
final CompressorInputStream compressedStream = new GzipCompressorInputStream(bufferedResourceStream);
importDataArchive(archive, compressedStream, options);
} else if (MT_PACK200.equals(type)) {
final CompressorInputStream compressedStream = new Pack200CompressorInputStream(bufferedResourceStream);
importDataArchive(archive, compressedStream, options);
} else if (MT_XZ.equals(type)) {
final CompressorInputStream compressedStream = new XZCompressorInputStream(bufferedResourceStream);
importDataArchive(archive, compressedStream, options);
} else {
throw new RuntimeException("Unrecognized archive media type: " + type);
}
} catch (IOException e) {
throw new RuntimeException("Could not load InputStream for resource: " + archive, e);
} finally {
IOUtils.closeQuietly(bufferedResourceStream);
}
}
Aggregations