use of org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream in project presto by prestodb.
the class TargzBasedPackageSupplier method getRootDirectory.
private static String getRootDirectory(File packageFile) {
try (TarArchiveInputStream archiveInputStream = new TarArchiveInputStream(new GzipCompressorInputStream(new FileInputStream(packageFile)))) {
ArchiveEntry entry = archiveInputStream.getNextEntry();
if (entry == null) {
throw new IllegalArgumentException(format("Archive is empty: %s", packageFile));
}
Path path = Paths.get(entry.getName());
return path.getName(0).toString();
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
use of org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream in project presto by prestodb.
the class TargzBasedPackageSupplier method extractPackage.
private static void extractPackage(File packageFile, File outputDirectory) {
try (TarArchiveInputStream archiveInputStream = new TarArchiveInputStream(new GzipCompressorInputStream(new FileInputStream(packageFile)))) {
while (true) {
ArchiveEntry entry = archiveInputStream.getNextEntry();
if (entry == null) {
break;
}
File output = new File(outputDirectory, entry.getName());
if (entry.isDirectory()) {
if (output.exists()) {
verify(output.isDirectory(), "package directory is not a directory: %s", output);
continue;
}
createDirectories(output.toPath());
} else {
File directory = output.getParentFile();
if (!directory.exists()) {
createDirectories(directory.toPath());
}
try (OutputStream outputStream = new FileOutputStream(output)) {
ByteStreams.copy(archiveInputStream, outputStream);
}
}
}
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
use of org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream in project flink by apache.
the class CompressionUtils method extractTarFileUsingJava.
// Follow the pattern suggested in
// https://commons.apache.org/proper/commons-compress/examples.html
private static void extractTarFileUsingJava(String inFilePath, String targetDirPath, boolean gzipped) throws IOException {
try (InputStream fi = Files.newInputStream(Paths.get(inFilePath));
InputStream bi = new BufferedInputStream(fi);
final TarArchiveInputStream tai = new TarArchiveInputStream(gzipped ? new GzipCompressorInputStream(bi) : bi)) {
final File targetDir = new File(targetDirPath);
TarArchiveEntry entry;
while ((entry = tai.getNextTarEntry()) != null) {
unpackEntry(tai, entry, targetDir);
}
}
}
use of org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream in project alluxio by Alluxio.
the class TarUtils method decompress.
/**
* Decompresses a tarball to one destination.
*
* @param in the input file path
* @param out destination to decompress files to
*/
public static void decompress(String in, File out) throws IOException {
try (TarArchiveInputStream fin = new TarArchiveInputStream(new GzipCompressorInputStream(new FileInputStream(in)))) {
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.compressors.gzip.GzipCompressorInputStream in project MSEC by Tencent.
the class GzipUtil method unzip.
public static void unzip(String srcFile) throws Exception {
GzipCompressorInputStream in = new GzipCompressorInputStream(new FileInputStream(srcFile));
int index = srcFile.indexOf(".gz");
String destFile = "";
if (index == srcFile.length() - 3) {
destFile = srcFile.substring(0, index);
} else {
destFile = srcFile + ".decompress";
}
FileOutputStream out = new FileOutputStream(destFile);
byte[] buf = new byte[10240];
while (true) {
int len = in.read(buf);
if (len <= 0) {
break;
}
out.write(buf, 0, len);
}
out.flush();
out.close();
in.close();
}
Aggregations