use of org.apache.commons.compress.archivers.tar.TarArchiveInputStream in project alluxio by Alluxio.
the class TarUtils method readTarGz.
/**
* Reads a gzipped tar archive from a stream and writes it to the given path.
*
* @param dirPath the path to write the archive to
* @param input the input stream
*/
public static void readTarGz(Path dirPath, InputStream input) throws IOException {
InputStream zipStream = new GzipCompressorInputStream(input);
TarArchiveInputStream archiveStream = new TarArchiveInputStream(zipStream);
TarArchiveEntry entry;
while ((entry = (TarArchiveEntry) archiveStream.getNextEntry()) != null) {
File outputFile = new File(dirPath.toFile(), entry.getName());
if (entry.isDirectory()) {
outputFile.mkdirs();
} else {
outputFile.getParentFile().mkdirs();
try (FileOutputStream fileOut = new FileOutputStream(outputFile)) {
IOUtils.copy(archiveStream, fileOut);
}
}
}
}
use of org.apache.commons.compress.archivers.tar.TarArchiveInputStream in project stanbol by apache.
the class ConfigUtils method getArchiveInputStream.
public static ArchiveInputStream getArchiveInputStream(String solrArchiveName, InputStream is) throws IOException {
String archiveFormat;
String solrArchiveExtension = FilenameUtils.getExtension(solrArchiveName);
if (solrArchiveExtension == null || solrArchiveExtension.isEmpty()) {
// assume that the archiveExtension was parsed
archiveFormat = solrArchiveName;
} else {
archiveFormat = SUPPORTED_SOLR_ARCHIVE_FORMAT.get(solrArchiveExtension);
}
ArchiveInputStream ais;
if ("zip".equals(archiveFormat)) {
ais = new ZipArchiveInputStream(is);
} else {
if ("gz".equals(archiveFormat)) {
is = new GZIPInputStream(is);
} else if ("bz2".equals(archiveFormat)) {
is = new BZip2CompressorInputStream(is);
} else {
throw new IllegalStateException("Unsupported compression format " + archiveFormat + "!. " + "Please report this to stanbol-dev mailing list!");
}
ais = new TarArchiveInputStream(is);
}
return ais;
}
use of org.apache.commons.compress.archivers.tar.TarArchiveInputStream in project heron by twitter.
the class Extractor method extract.
static void extract(InputStream in, Path destination) throws IOException {
try (final BufferedInputStream bufferedInputStream = new BufferedInputStream(in);
final GzipCompressorInputStream gzipInputStream = new GzipCompressorInputStream(bufferedInputStream);
final TarArchiveInputStream tarInputStream = new TarArchiveInputStream(gzipInputStream)) {
final String destinationAbsolutePath = destination.toFile().getAbsolutePath();
TarArchiveEntry entry;
while ((entry = (TarArchiveEntry) tarInputStream.getNextEntry()) != null) {
if (entry.isDirectory()) {
File f = Paths.get(destinationAbsolutePath, entry.getName()).toFile();
f.mkdirs();
} else {
Path fileDestinationPath = Paths.get(destinationAbsolutePath, entry.getName());
Files.copy(tarInputStream, fileDestinationPath, StandardCopyOption.REPLACE_EXISTING);
}
}
}
}
use of org.apache.commons.compress.archivers.tar.TarArchiveInputStream in project OpenRefine by OpenRefine.
the class FileProjectManager method untar.
protected void untar(File destDir, InputStream inputStream) throws IOException {
TarArchiveInputStream tin = new TarArchiveInputStream(inputStream);
TarArchiveEntry tarEntry = null;
while ((tarEntry = tin.getNextTarEntry()) != null) {
File destEntry = new File(destDir, tarEntry.getName());
File parent = destEntry.getParentFile();
if (!parent.exists()) {
parent.mkdirs();
}
if (tarEntry.isDirectory()) {
destEntry.mkdirs();
} else {
FileOutputStream fout = new FileOutputStream(destEntry);
try {
IOUtils.copy(tin, fout);
} finally {
fout.close();
}
}
}
tin.close();
}
use of org.apache.commons.compress.archivers.tar.TarArchiveInputStream in project spring-boot by spring-projects.
the class TarLayoutWriterTests method writesTarArchive.
@Test
void writesTarArchive() throws Exception {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
try (TarLayoutWriter writer = new TarLayoutWriter(outputStream)) {
writer.directory("/foo", Owner.ROOT);
writer.file("/foo/bar.txt", Owner.of(1, 1), 0777, Content.of("test"));
}
try (TarArchiveInputStream tarInputStream = new TarArchiveInputStream(new ByteArrayInputStream(outputStream.toByteArray()))) {
TarArchiveEntry directoryEntry = tarInputStream.getNextTarEntry();
TarArchiveEntry fileEntry = tarInputStream.getNextTarEntry();
byte[] fileContent = new byte[(int) fileEntry.getSize()];
tarInputStream.read(fileContent);
assertThat(tarInputStream.getNextEntry()).isNull();
assertThat(directoryEntry.getName()).isEqualTo("/foo/");
assertThat(directoryEntry.getMode()).isEqualTo(0755);
assertThat(directoryEntry.getLongUserId()).isEqualTo(0);
assertThat(directoryEntry.getLongGroupId()).isEqualTo(0);
assertThat(directoryEntry.getModTime()).isEqualTo(new Date(TarLayoutWriter.NORMALIZED_MOD_TIME));
assertThat(fileEntry.getName()).isEqualTo("/foo/bar.txt");
assertThat(fileEntry.getMode()).isEqualTo(0777);
assertThat(fileEntry.getLongUserId()).isEqualTo(1);
assertThat(fileEntry.getLongGroupId()).isEqualTo(1);
assertThat(fileEntry.getModTime()).isEqualTo(new Date(TarLayoutWriter.NORMALIZED_MOD_TIME));
assertThat(fileContent).isEqualTo("test".getBytes(StandardCharsets.UTF_8));
}
}
Aggregations