use of org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream in project incubator-gobblin by apache.
the class StreamUtilsTest method testTarFile.
@Test
public void testTarFile() throws IOException {
FileSystem localFs = FileSystem.getLocal(new Configuration());
// Set of expected Paths to be in the resulting tar file
Set<Path> expectedPaths = Sets.newHashSet();
// Create input file path
Path testFile = new Path("testFile");
expectedPaths.add(testFile);
// Create output file path
Path testOutFile = new Path("testTarOut" + UUID.randomUUID() + ".tar.gz");
try {
// Create the input file
FSDataOutputStream testFileOut1 = localFs.create(testFile);
testFileOut1.close();
// tar the input file to the specific output file
StreamUtils.tar(localFs, testFile, testOutFile);
// Confirm the contents of the tar file are valid
try (TarArchiveInputStream tarArchiveInputStream = new TarArchiveInputStream(new GzipCompressorInputStream(localFs.open(testOutFile)))) {
TarArchiveEntry tarArchiveEntry;
while (null != (tarArchiveEntry = tarArchiveInputStream.getNextTarEntry())) {
MatcherAssert.assertThat(new Path(tarArchiveEntry.getName()), Matchers.isIn(expectedPaths));
}
}
} finally {
if (localFs.exists(testFile)) {
localFs.delete(testFile, true);
}
if (localFs.exists(testOutFile)) {
localFs.delete(testOutFile, true);
}
}
}
use of org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream in project twister2 by DSC-SPIDAL.
the class KubernetesWorker method unpackJobPackage.
/**
* unpack the received job package
* job package needs to be a tar.gz package
* it unpacks to the directory where the job package resides
* @param sourceGzip
* @return
*/
private static boolean unpackJobPackage(final String sourceGzip) {
File sourceGzipFile = new File(sourceGzip);
File outputDir = sourceGzipFile.getParentFile();
GzipCompressorInputStream gzIn = null;
TarArchiveInputStream tarInputStream = null;
try {
// construct input stream
InputStream fin = Files.newInputStream(Paths.get(sourceGzip));
BufferedInputStream in = new BufferedInputStream(fin);
gzIn = new GzipCompressorInputStream(in);
tarInputStream = new TarArchiveInputStream(gzIn);
TarArchiveEntry entry = null;
while ((entry = (TarArchiveEntry) tarInputStream.getNextEntry()) != null) {
File outputFile = new File(outputDir, entry.getName());
if (!outputFile.getParentFile().exists()) {
boolean dirCreated = outputFile.getParentFile().mkdirs();
if (!dirCreated) {
LOG.severe("Can not create the output directory: " + outputFile.getParentFile() + "\nFile unpack is unsuccessful.");
return false;
}
}
if (!outputFile.isDirectory()) {
final OutputStream outputFileStream = new FileOutputStream(outputFile);
IOUtils.copy(tarInputStream, outputFileStream);
outputFileStream.close();
// LOG.info("Unpacked the file: " + outputFile.getAbsolutePath());
}
}
tarInputStream.close();
gzIn.close();
return true;
} catch (IOException e) {
LOG.log(Level.SEVERE, "Exception when unpacking job package. ", e);
return false;
}
}
use of org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream in project tomee by apache.
the class TarGzs method untargz.
public static void untargz(final InputStream read, final File destination, final boolean noparent, final FileFilter fileFilter) throws IOException {
Objects.requireNonNull(fileFilter, "'fileFilter' is required.");
Files.dir(destination);
Files.writable(destination);
try {
GzipCompressorInputStream gzipIn = new GzipCompressorInputStream(read);
TarArchiveInputStream tarIn = new TarArchiveInputStream(gzipIn);
TarArchiveEntry entry;
while ((entry = (TarArchiveEntry) tarIn.getNextEntry()) != null) {
String path = entry.getName();
if (noparent) {
path = path.replaceFirst("^[^/]+/", "");
}
File file = new File(destination, path);
if (!fileFilter.accept(file))
continue;
if (entry.isDirectory()) {
Files.mkdir(file);
} else {
Files.mkdir(file.getParentFile());
IO.copy(tarIn, file);
long lastModified = entry.getLastModifiedDate().getTime();
if (lastModified > 0L) {
file.setLastModified(lastModified);
}
// Elasticsearch tar has entries with 33261 that are executable
if (33261 == entry.getMode()) {
file.setExecutable(true);
}
// Kibana tar has entries with 493 that are executable
if (493 == entry.getMode()) {
file.setExecutable(true);
}
}
}
tarIn.close();
} catch (IOException var9) {
throw new IOException("Unable to unzip " + read, var9);
}
}
use of org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream in project Anserini by castorini.
the class AxiomReranker method getReadFileStream.
public InputStream getReadFileStream(String path) throws IOException {
InputStream fin = Files.newInputStream(Paths.get(path), StandardOpenOption.READ);
BufferedInputStream in = new BufferedInputStream(fin);
if (path.endsWith(".bz2")) {
BZip2CompressorInputStream bzIn = new BZip2CompressorInputStream(in);
return bzIn;
} else if (path.endsWith(".gz")) {
GzipCompressorInputStream gzIn = new GzipCompressorInputStream(in);
return gzIn;
} else if (path.endsWith(".zip")) {
GzipCompressorInputStream zipIn = new GzipCompressorInputStream(in);
return zipIn;
}
return in;
}
use of org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream in project spring-boot by spring-projects.
the class TarGzipBuildpack method copyAndRebaseEntries.
private void copyAndRebaseEntries(OutputStream outputStream) throws IOException {
String id = this.coordinates.getSanitizedId();
Path basePath = Paths.get("/cnb/buildpacks/", id, this.coordinates.getVersion());
try (TarArchiveInputStream tar = new TarArchiveInputStream(new GzipCompressorInputStream(Files.newInputStream(this.path)));
TarArchiveOutputStream output = new TarArchiveOutputStream(outputStream)) {
writeBasePathEntries(output, basePath);
TarArchiveEntry entry = tar.getNextTarEntry();
while (entry != null) {
entry.setName(basePath + "/" + entry.getName());
output.putArchiveEntry(entry);
StreamUtils.copy(tar, output);
output.closeArchiveEntry();
entry = tar.getNextTarEntry();
}
output.finish();
}
}
Aggregations