use of org.apache.commons.compress.archivers.tar.TarArchiveInputStream in project pinot by linkedin.
the class TarGzCompressionUtils method unTarOneFile.
public static InputStream unTarOneFile(InputStream tarGzInputStream, final String filename) throws FileNotFoundException, IOException, ArchiveException {
TarArchiveInputStream debInputStream = null;
InputStream is = null;
try {
is = new GzipCompressorInputStream(tarGzInputStream);
debInputStream = (TarArchiveInputStream) new ArchiveStreamFactory().createArchiveInputStream("tar", is);
TarArchiveEntry entry = null;
while ((entry = (TarArchiveEntry) debInputStream.getNextEntry()) != null) {
if (entry.getName().contains(filename)) {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
IOUtils.copy(debInputStream, byteArrayOutputStream);
return new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
}
}
} finally {
IOUtils.closeQuietly(debInputStream);
IOUtils.closeQuietly(is);
}
return null;
}
use of org.apache.commons.compress.archivers.tar.TarArchiveInputStream in project pinot by linkedin.
the class TarGzCompressionUtils 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 inputFile the input .tar file
* @param outputDir 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 File inputFile, final File outputDir) throws FileNotFoundException, IOException, ArchiveException {
LOGGER.debug(String.format("Untaring %s to dir %s.", inputFile.getAbsolutePath(), outputDir.getAbsolutePath()));
TarArchiveInputStream debInputStream = null;
InputStream is = null;
final List<File> untaredFiles = new LinkedList<File>();
try {
is = new GzipCompressorInputStream(new BufferedInputStream(new FileInputStream(inputFile)));
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 (entry.isDirectory()) {
LOGGER.debug(String.format("Attempting to write output directory %s.", outputFile.getAbsolutePath()));
if (!outputFile.exists()) {
LOGGER.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 {
LOGGER.error("The directory already there. Deleting - " + outputFile.getAbsolutePath());
FileUtils.deleteDirectory(outputFile);
}
} else {
LOGGER.debug(String.format("Creating output file %s.", outputFile.getAbsolutePath()));
File directory = outputFile.getParentFile();
if (!directory.exists()) {
directory.mkdirs();
}
OutputStream outputFileStream = null;
try {
outputFileStream = new FileOutputStream(outputFile);
IOUtils.copy(debInputStream, outputFileStream);
} finally {
IOUtils.closeQuietly(outputFileStream);
}
}
untaredFiles.add(outputFile);
}
} finally {
IOUtils.closeQuietly(debInputStream);
IOUtils.closeQuietly(is);
}
return untaredFiles;
}
use of org.apache.commons.compress.archivers.tar.TarArchiveInputStream in project che by eclipse.
the class TarUtils method untar.
public static void untar(InputStream in, File targetDir) throws IOException {
final TarArchiveInputStream tarIn = new TarArchiveInputStream(in);
byte[] b = new byte[BUF_SIZE];
TarArchiveEntry tarEntry;
while ((tarEntry = tarIn.getNextTarEntry()) != null) {
final File file = new File(targetDir, tarEntry.getName());
if (tarEntry.isDirectory()) {
if (!file.mkdirs()) {
throw new IOException("Unable to create folder " + file.getAbsolutePath());
}
} else {
final File parent = file.getParentFile();
if (!parent.exists()) {
if (!parent.mkdirs()) {
throw new IOException("Unable to create folder " + parent.getAbsolutePath());
}
}
try (FileOutputStream fos = new FileOutputStream(file)) {
int r;
while ((r = tarIn.read(b)) != -1) {
fos.write(b, 0, r);
}
}
}
}
}
use of org.apache.commons.compress.archivers.tar.TarArchiveInputStream in project gerrit by GerritCodeReview.
the class SubmitByMergeIfNecessaryIT method testPreviewSubmitTgz.
@Test
public void testPreviewSubmitTgz() throws Exception {
Project.NameKey p1 = createProject("project-name");
TestRepository<?> repo1 = cloneProject(p1);
PushOneCommit.Result change1 = createChange(repo1, "master", "test", "a.txt", "1", "topic");
approve(change1.getChangeId());
// get a preview before submitting:
File tempfile;
try (BinaryResult request = submitPreview(change1.getChangeId(), "tgz")) {
assertThat(request.getContentType()).isEqualTo("application/x-gzip");
tempfile = File.createTempFile("test", null);
request.writeTo(Files.newOutputStream(tempfile.toPath()));
}
InputStream is = new GZIPInputStream(Files.newInputStream(tempfile.toPath()));
List<String> untarredFiles = new ArrayList<>();
try (TarArchiveInputStream tarInputStream = (TarArchiveInputStream) new ArchiveStreamFactory().createArchiveInputStream("tar", is)) {
TarArchiveEntry entry = null;
while ((entry = (TarArchiveEntry) tarInputStream.getNextEntry()) != null) {
untarredFiles.add(entry.getName());
}
}
assertThat(untarredFiles).containsExactly(name("project-name") + ".git");
}
use of org.apache.commons.compress.archivers.tar.TarArchiveInputStream in project bazel by bazelbuild.
the class CompressedTarFunction method decompress.
@Override
public Path decompress(DecompressorDescriptor descriptor) throws RepositoryFunctionException {
Optional<String> prefix = descriptor.prefix();
boolean foundPrefix = false;
try (InputStream decompressorStream = getDecompressorStream(descriptor)) {
TarArchiveInputStream tarStream = new TarArchiveInputStream(decompressorStream);
TarArchiveEntry entry;
while ((entry = tarStream.getNextTarEntry()) != null) {
StripPrefixedPath entryPath = StripPrefixedPath.maybeDeprefix(entry.getName(), prefix);
foundPrefix = foundPrefix || entryPath.foundPrefix();
if (entryPath.skip()) {
continue;
}
Path filename = descriptor.repositoryPath().getRelative(entryPath.getPathFragment());
FileSystemUtils.createDirectoryAndParents(filename.getParentDirectory());
if (entry.isDirectory()) {
FileSystemUtils.createDirectoryAndParents(filename);
} else {
if (entry.isSymbolicLink() || entry.isLink()) {
PathFragment linkName = new PathFragment(entry.getLinkName());
boolean wasAbsolute = linkName.isAbsolute();
// Strip the prefix from the link path if set.
linkName = StripPrefixedPath.maybeDeprefix(linkName.getPathString(), prefix).getPathFragment();
if (wasAbsolute) {
// Recover the path to an absolute path as maybeDeprefix() relativize the path
// even if the prefix is not set
linkName = descriptor.repositoryPath().getRelative(linkName).asFragment();
}
if (entry.isSymbolicLink()) {
FileSystemUtils.ensureSymbolicLink(filename, linkName);
} else {
FileSystemUtils.createHardLink(filename, descriptor.repositoryPath().getRelative(linkName));
}
} else {
Files.copy(tarStream, filename.getPathFile().toPath(), StandardCopyOption.REPLACE_EXISTING);
filename.chmod(entry.getMode());
// This can only be done on real files, not links, or it will skip the reader to
// the next "real" file to try to find the mod time info.
Date lastModified = entry.getLastModifiedDate();
filename.setLastModifiedTime(lastModified.getTime());
}
}
}
} catch (IOException e) {
throw new RepositoryFunctionException(e, Transience.TRANSIENT);
}
if (prefix.isPresent() && !foundPrefix) {
throw new RepositoryFunctionException(new IOException("Prefix " + prefix.get() + " was given, but not found in the archive"), Transience.PERSISTENT);
}
return descriptor.repositoryPath();
}
Aggregations