use of org.apache.commons.compress.archivers.tar.TarArchiveInputStream in project jbosstools-openshift by jbosstools.
the class FileHelper method extractTarGz.
public static void extractTarGz(File archive, File outputDirectory) {
InputStream inputStream = null;
try {
logger.info("Opening stream to gzip archive");
inputStream = new GzipCompressorInputStream(new FileInputStream(archive));
} catch (IOException ex) {
throw new OpenShiftToolsException("Exception occured while processing tar.gz file.\n" + ex.getMessage());
}
logger.info("Opening stream to tar archive");
BufferedOutputStream outputStream = null;
TarArchiveInputStream tarArchiveInputStream = new TarArchiveInputStream(inputStream);
TarArchiveEntry currentEntry = null;
try {
while ((currentEntry = tarArchiveInputStream.getNextTarEntry()) != null) {
if (currentEntry.isDirectory()) {
logger.info("Creating directory: " + currentEntry.getName());
createDirectory(new File(outputDirectory, currentEntry.getName()));
} else {
File outputFile = new File(outputDirectory, currentEntry.getName());
if (!outputFile.getParentFile().exists()) {
logger.info("Creating directory: " + outputFile.getParentFile());
createDirectory(outputFile.getParentFile());
}
outputStream = new BufferedOutputStream(new FileOutputStream(outputFile));
logger.info("Extracting file: " + currentEntry.getName());
copy(tarArchiveInputStream, outputStream, (int) currentEntry.getSize());
outputStream.close();
outputFile.setExecutable(true);
outputFile.setReadable(true);
outputFile.setWritable(true);
}
}
} catch (IOException e) {
throw new OpenShiftToolsException("Exception occured while processing tar.gz file.\n" + e.getMessage());
} finally {
try {
tarArchiveInputStream.close();
} catch (Exception ex) {
}
try {
outputStream.close();
} catch (Exception ex) {
}
}
}
use of org.apache.commons.compress.archivers.tar.TarArchiveInputStream in project LibreraReader by foobnix.
the class ArchiveStreamFactory method detect.
/**
* Try to determine the type of Archiver
* @param in input stream
* @return type of archiver if found
* @throws ArchiveException if an archiver cannot be detected in the stream
* @since 1.14
*/
public static String detect(InputStream in) throws ArchiveException {
if (in == null) {
throw new IllegalArgumentException("Stream must not be null.");
}
if (!in.markSupported()) {
throw new IllegalArgumentException("Mark is not supported.");
}
final byte[] signature = new byte[SIGNATURE_SIZE];
in.mark(signature.length);
int signatureLength = -1;
try {
signatureLength = IOUtils.readFully(in, signature);
in.reset();
} catch (IOException e) {
throw new ArchiveException("IOException while reading signature.", e);
}
if (ZipArchiveInputStream.matches(signature, signatureLength)) {
return ZIP;
} else if (JarArchiveInputStream.matches(signature, signatureLength)) {
return JAR;
} else if (ArArchiveInputStream.matches(signature, signatureLength)) {
return AR;
} else if (CpioArchiveInputStream.matches(signature, signatureLength)) {
return CPIO;
} else if (ArjArchiveInputStream.matches(signature, signatureLength)) {
return ARJ;
}
// Dump needs a bigger buffer to check the signature;
final byte[] dumpsig = new byte[DUMP_SIGNATURE_SIZE];
in.mark(dumpsig.length);
try {
signatureLength = IOUtils.readFully(in, dumpsig);
in.reset();
} catch (IOException e) {
throw new ArchiveException("IOException while reading dump signature", e);
}
if (DumpArchiveInputStream.matches(dumpsig, signatureLength)) {
return DUMP;
}
// Tar needs an even bigger buffer to check the signature; read the first block
final byte[] tarHeader = new byte[TAR_HEADER_SIZE];
in.mark(tarHeader.length);
try {
signatureLength = IOUtils.readFully(in, tarHeader);
in.reset();
} catch (IOException e) {
throw new ArchiveException("IOException while reading tar signature", e);
}
if (TarArchiveInputStream.matches(tarHeader, signatureLength)) {
return TAR;
}
// COMPRESS-117 - improve auto-recognition
if (signatureLength >= TAR_HEADER_SIZE) {
TarArchiveInputStream tais = null;
try {
tais = new TarArchiveInputStream(new ByteArrayInputStream(tarHeader));
// COMPRESS-191 - verify the header checksum
if (tais.getNextTarEntry().isCheckSumOK()) {
return TAR;
}
} catch (final Exception e) {
// NOPMD // NOSONAR
// can generate IllegalArgumentException as well
// as IOException
// autodetection, simply not a TAR
// ignored
} finally {
IOUtils.closeQuietly(tais);
}
}
throw new ArchiveException("No Archiver found for the stream signature");
}
use of org.apache.commons.compress.archivers.tar.TarArchiveInputStream in project testcontainers-java by testcontainers.
the class GenericContainer method copyFileFromContainer.
/**
* {@inheritDoc}
*/
@Override
public void copyFileFromContainer(String containerPath, String destinationPath) throws IOException {
if (!isRunning()) {
throw new IllegalStateException("copyFileToContainer can only be used while the Container is running");
}
try (final TarArchiveInputStream tarInputStream = new TarArchiveInputStream(this.dockerClient.copyArchiveFromContainerCmd(this.containerId, containerPath).exec())) {
tarInputStream.getNextTarEntry();
IOUtils.copy(tarInputStream, new FileOutputStream(destinationPath));
}
}
use of org.apache.commons.compress.archivers.tar.TarArchiveInputStream in project testcontainers-java by testcontainers.
the class VncRecordingContainer method streamRecording.
@SneakyThrows
public InputStream streamRecording() {
TarArchiveInputStream archiveInputStream = new TarArchiveInputStream(dockerClient.copyArchiveFromContainerCmd(containerId, RECORDING_FILE_NAME).exec());
archiveInputStream.getNextEntry();
return archiveInputStream;
}
use of org.apache.commons.compress.archivers.tar.TarArchiveInputStream in project streamline by hortonworks.
the class CustomProcessorUploadHandler method getFileAsByteArray.
private byte[] getFileAsByteArray(File tarFile, Function<String, Boolean> filterFunc) {
byte[] data = null;
LOG.info("Looking in to file {} ", tarFile);
try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(tarFile));
TarArchiveInputStream tarArchiveInputStream = new TarArchiveInputStream(bis)) {
TarArchiveEntry tarArchiveEntry = tarArchiveInputStream.getNextTarEntry();
while (tarArchiveEntry != null) {
if (filterFunc.apply(tarArchiveEntry.getName())) {
data = IOUtils.toByteArray(tarArchiveInputStream);
break;
}
tarArchiveEntry = tarArchiveInputStream.getNextTarEntry();
}
} catch (IOException e) {
LOG.warn("Exception occured while looking in to tar file [] ", filterFunc, tarFile, e);
}
return data;
}
Aggregations