use of org.apache.commons.compress.archivers.tar.TarArchiveInputStream in project ats-framework by Axway.
the class LocalFileSystemOperations method extractTar.
private void extractTar(String tarFilePath, String outputDirPath) {
TarArchiveEntry entry = null;
try (TarArchiveInputStream tis = new TarArchiveInputStream(new FileInputStream(tarFilePath))) {
while ((entry = (TarArchiveEntry) tis.getNextEntry()) != null) {
if (log.isDebugEnabled()) {
log.debug("Extracting " + entry.getName());
}
File entryDestination = new File(outputDirPath, entry.getName());
if (entry.isDirectory()) {
entryDestination.mkdirs();
} else {
entryDestination.getParentFile().mkdirs();
OutputStream out = new BufferedOutputStream(new FileOutputStream(entryDestination));
IoUtils.copyStream(tis, out, false, true);
}
if (OperatingSystemType.getCurrentOsType() != OperatingSystemType.WINDOWS) {
// check if the OS is UNIX
// set file/dir permissions, after it is created
Files.setPosixFilePermissions(entryDestination.getCanonicalFile().toPath(), getPosixFilePermission(entry.getMode()));
}
}
} catch (Exception e) {
String errorMsg = null;
if (entry != null) {
errorMsg = "Unable to untar " + StringEscapeUtils.escapeJava(entry.getName()) + " from " + tarFilePath + ".Target directory '" + outputDirPath + "' is in inconsistent state.";
} else {
errorMsg = "Could not read data from " + tarFilePath;
}
throw new FileSystemOperationException(errorMsg, e);
}
}
use of org.apache.commons.compress.archivers.tar.TarArchiveInputStream in project twister2 by DSC-SPIDAL.
the class TarGzipPacker method addTarGzipToArchive.
/**
* given tar.gz file will be copied to this tar.gz file.
* all files will be transferred to new tar.gz file one by one.
* original directory structure will be kept intact
*
* @param tarGzipFile the archive file to be copied to the new archive
*/
public boolean addTarGzipToArchive(String tarGzipFile) {
try {
// construct input stream
InputStream fin = Files.newInputStream(Paths.get(tarGzipFile));
BufferedInputStream in = new BufferedInputStream(fin);
GzipCompressorInputStream gzIn = new GzipCompressorInputStream(in);
TarArchiveInputStream tarInputStream = new TarArchiveInputStream(gzIn);
// copy the existing entries from source gzip file
ArchiveEntry nextEntry;
while ((nextEntry = tarInputStream.getNextEntry()) != null) {
tarOutputStream.putArchiveEntry(nextEntry);
IOUtils.copy(tarInputStream, tarOutputStream);
tarOutputStream.closeArchiveEntry();
}
tarInputStream.close();
return true;
} catch (IOException ioe) {
LOG.log(Level.SEVERE, "Archive File can not be added: " + tarGzipFile, ioe);
return false;
}
}
use of org.apache.commons.compress.archivers.tar.TarArchiveInputStream in project che by eclipse.
the class TarArchiverTest method readArchiveEntries.
private Map<String, String> readArchiveEntries(InputStream archive) throws Exception {
Map<String, String> entries = newHashMap();
try (TarArchiveInputStream tarIn = new TarArchiveInputStream(archive)) {
TarArchiveEntry tarArchiveEntry;
while ((tarArchiveEntry = tarIn.getNextTarEntry()) != null) {
String name = tarArchiveEntry.getName();
String content = tarArchiveEntry.isDirectory() ? "<none>" : new String(ByteStreams.toByteArray(tarIn));
entries.put(name, content);
}
}
return entries;
}
use of org.apache.commons.compress.archivers.tar.TarArchiveInputStream in project che by eclipse.
the class TarArchiverTest method assertThatTarArchiveContainsAllEntries.
private void assertThatTarArchiveContainsAllEntries(InputStream in, Map<String, String> entries) throws Exception {
try (TarArchiveInputStream tarIn = new TarArchiveInputStream(in)) {
TarArchiveEntry tarArchiveEntry;
while ((tarArchiveEntry = tarIn.getNextTarEntry()) != null) {
String name = tarArchiveEntry.getName();
assertTrue(String.format("Unexpected entry %s in TAR", name), entries.containsKey(name));
if (!tarArchiveEntry.isDirectory()) {
String content = new String(ByteStreams.toByteArray(tarIn));
assertEquals(String.format("Invalid content of file %s", name), entries.get(name), content);
}
entries.remove(name);
}
}
assertTrue(String.format("Expected but were not found in TAR %s", entries), entries.isEmpty());
}
use of org.apache.commons.compress.archivers.tar.TarArchiveInputStream in project che by eclipse.
the class TarArchiver method extract.
@Override
public void extract(InputStream tarInput, boolean overwrite, int stripNumber) throws IOException, ForbiddenException, ConflictException, ServerException {
try (TarArchiveInputStream tarInputStream = new TarArchiveInputStream(tarInput)) {
InputStream notClosableInputStream = new NotClosableInputStream(tarInputStream);
TarArchiveEntry tarEntry;
while ((tarEntry = tarInputStream.getNextTarEntry()) != null) {
VirtualFile extractFolder = folder;
Path relativePath = Path.of(tarEntry.getName());
if (stripNumber > 0) {
if (relativePath.length() <= stripNumber) {
continue;
}
relativePath = relativePath.subPath(stripNumber);
}
if (tarEntry.isDirectory()) {
if (!extractFolder.hasChild(relativePath)) {
extractFolder.createFolder(relativePath.toString());
}
continue;
}
if (relativePath.length() > 1) {
Path neededParentPath = relativePath.getParent();
VirtualFile neededParent = extractFolder.getChild(neededParentPath);
if (neededParent == null) {
neededParent = extractFolder.createFolder(neededParentPath.toString());
}
extractFolder = neededParent;
}
String fileName = relativePath.getName();
VirtualFile file = extractFolder.getChild(Path.of(fileName));
if (file == null) {
extractFolder.createFile(fileName, notClosableInputStream);
} else {
if (overwrite) {
file.updateContent(notClosableInputStream);
} else {
throw new ConflictException(String.format("File '%s' already exists", file.getPath()));
}
}
}
}
}
Aggregations