use of org.apache.commons.compress.archivers.ArchiveEntry 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.ArchiveEntry in project arduino-eclipse-plugin by Sloeber.
the class InternalPackageManager method extract.
public static IStatus extract(ArchiveInputStream in, File destFolder, int stripPath, boolean overwrite, IProgressMonitor pMonitor) throws IOException, InterruptedException {
// Folders timestamps must be set at the end of archive extraction
// (because creating a file in a folder alters the folder's timestamp)
Map<File, Long> foldersTimestamps = new HashMap<>();
// $NON-NLS-1$
String pathPrefix = "";
Map<File, File> hardLinks = new HashMap<>();
Map<File, Integer> hardLinksMode = new HashMap<>();
Map<File, String> symLinks = new HashMap<>();
Map<File, Long> symLinksModifiedTimes = new HashMap<>();
// Cycle through all the archive entries
while (true) {
ArchiveEntry entry = in.getNextEntry();
if (entry == null) {
break;
}
// Extract entry info
long size = entry.getSize();
String name = entry.getName();
boolean isDirectory = entry.isDirectory();
boolean isLink = false;
boolean isSymLink = false;
String linkName = null;
Integer mode = null;
Long modifiedTime = new Long(entry.getLastModifiedDate().getTime());
// $NON-NLS-1$
pMonitor.subTask("Processing " + name);
{
// Skip MacOSX metadata
// http://superuser.com/questions/61185/why-do-i-get-files-like-foo-in-my-tarball-on-os-x
int slash = name.lastIndexOf('/');
if (slash == -1) {
if (name.startsWith("._")) {
// $NON-NLS-1$
continue;
}
} else {
if (name.substring(slash + 1).startsWith("._")) {
// $NON-NLS-1$
continue;
}
}
}
// http://www.unix.com/unix-for-dummies-questions-and-answers/124958-file-pax_global_header-means-what.html
if (name.contains("pax_global_header")) {
// $NON-NLS-1$
continue;
}
if (entry instanceof TarArchiveEntry) {
TarArchiveEntry tarEntry = (TarArchiveEntry) entry;
mode = new Integer(tarEntry.getMode());
isLink = tarEntry.isLink();
isSymLink = tarEntry.isSymbolicLink();
linkName = tarEntry.getLinkName();
}
// On the first archive entry, if requested, detect the common path
// prefix to be stripped from filenames
int localstripPath = stripPath;
if (localstripPath > 0 && pathPrefix.isEmpty()) {
int slash = 0;
while (localstripPath > 0) {
// $NON-NLS-1$
slash = name.indexOf("/", slash);
if (slash == -1) {
throw new IOException(Messages.Manager_no_single_root_folder);
}
slash++;
localstripPath--;
}
pathPrefix = name.substring(0, slash);
}
// Strip the common path prefix when requested
if (!name.startsWith(pathPrefix)) {
throw new IOException(Messages.Manager_no_single_root_folder_while_file + name + Messages.Manager_is_outside + pathPrefix);
}
name = name.substring(pathPrefix.length());
if (name.isEmpty()) {
continue;
}
File outputFile = new File(destFolder, name);
File outputLinkedFile = null;
if (isLink && linkName != null) {
if (!linkName.startsWith(pathPrefix)) {
throw new IOException(Messages.Manager_no_single_root_folder_while_file + linkName + Messages.Manager_is_outside + pathPrefix);
}
linkName = linkName.substring(pathPrefix.length());
outputLinkedFile = new File(destFolder, linkName);
}
if (isSymLink) {
// Symbolic links are referenced with relative paths
outputLinkedFile = new File(linkName);
if (outputLinkedFile.isAbsolute()) {
System.err.println(Messages.Manager_Warning_file + outputFile + Messages.Manager_links_to_absolute_path + outputLinkedFile);
System.err.println();
}
}
// Safety check
if (isDirectory) {
if (outputFile.isFile() && !overwrite) {
throw new IOException(Messages.Manager_Cant_create_folder + outputFile + Messages.Manager_File_exists);
}
} else {
// - anything else
if (outputFile.exists() && !overwrite) {
throw new IOException(Messages.Manager_Cant_extract_file + outputFile + Messages.Manager_File_already_exists);
}
}
// Extract the entry
if (isDirectory) {
if (!outputFile.exists() && !outputFile.mkdirs()) {
throw new IOException(Messages.Manager_Cant_create_folder + outputFile);
}
foldersTimestamps.put(outputFile, modifiedTime);
} else if (isLink) {
hardLinks.put(outputFile, outputLinkedFile);
hardLinksMode.put(outputFile, mode);
} else if (isSymLink) {
symLinks.put(outputFile, linkName);
symLinksModifiedTimes.put(outputFile, modifiedTime);
} else {
// Create the containing folder if not exists
if (!outputFile.getParentFile().isDirectory()) {
outputFile.getParentFile().mkdirs();
}
copyStreamToFile(in, size, outputFile);
outputFile.setLastModified(modifiedTime.longValue());
}
// Set file/folder permission
if (mode != null && !isSymLink && outputFile.exists()) {
chmod(outputFile, mode.intValue());
}
}
for (Map.Entry<File, File> entry : hardLinks.entrySet()) {
if (entry.getKey().exists() && overwrite) {
entry.getKey().delete();
}
link(entry.getValue(), entry.getKey());
Integer mode = hardLinksMode.get(entry.getKey());
if (mode != null) {
chmod(entry.getKey(), mode.intValue());
}
}
for (Map.Entry<File, String> entry : symLinks.entrySet()) {
if (entry.getKey().exists() && overwrite) {
entry.getKey().delete();
}
symlink(entry.getValue(), entry.getKey());
entry.getKey().setLastModified(symLinksModifiedTimes.get(entry.getKey()).longValue());
}
// Set folders timestamps
for (Map.Entry<File, Long> entry : foldersTimestamps.entrySet()) {
entry.getKey().setLastModified(entry.getValue().longValue());
}
return Status.OK_STATUS;
}
use of org.apache.commons.compress.archivers.ArchiveEntry in project halyard by spinnaker.
the class BackupService method untarHalconfig.
private void untarHalconfig(String halconfigDir, String halconfigTar) {
FileInputStream tarInput = null;
TarArchiveInputStream tarArchiveInputStream = null;
try {
tarInput = new FileInputStream(new File(halconfigTar));
tarArchiveInputStream = (TarArchiveInputStream) new ArchiveStreamFactory().createArchiveInputStream("tar", tarInput);
} catch (IOException | ArchiveException e) {
throw new HalException(Problem.Severity.FATAL, "Failed to open backup: " + e.getMessage(), e);
}
try {
ArchiveEntry archiveEntry = tarArchiveInputStream.getNextEntry();
while (archiveEntry != null) {
String entryName = archiveEntry.getName();
Path outputPath = Paths.get(halconfigDir, entryName);
File outputFile = outputPath.toFile();
if (!outputFile.getParentFile().exists()) {
outputFile.getParentFile().mkdirs();
}
if (archiveEntry.isDirectory()) {
outputFile.mkdir();
} else {
Files.copy(tarArchiveInputStream, outputPath, REPLACE_EXISTING);
}
archiveEntry = tarArchiveInputStream.getNextEntry();
}
} catch (IOException e) {
throw new HalException(Problem.Severity.FATAL, "Failed to read archive entry: " + e.getMessage(), e);
}
}
use of org.apache.commons.compress.archivers.ArchiveEntry in project halyard by spinnaker.
the class RegistryBackedArchiveProfileBuilder method build.
public List<Profile> build(DeploymentConfiguration deploymentConfiguration, String baseOutputPath, SpinnakerArtifact artifact, String archiveName) {
String version = artifactService.getArtifactVersion(deploymentConfiguration.getName(), artifact);
InputStream is;
try {
is = profileRegistry.readArchiveProfile(artifact.getName(), version, archiveName);
} catch (IOException e) {
throw new HalException(Problem.Severity.FATAL, "Error retrieving contents of archive " + archiveName + ".tar.gz", e);
}
TarArchiveInputStream tis;
try {
tis = (TarArchiveInputStream) new ArchiveStreamFactory().createArchiveInputStream("tar", is);
} catch (ArchiveException e) {
throw new HalException(Problem.Severity.FATAL, "Failed to unpack tar archive", e);
}
try {
List<Profile> result = new ArrayList<>();
ArchiveEntry profileEntry = tis.getNextEntry();
while (profileEntry != null) {
if (profileEntry.isDirectory()) {
profileEntry = tis.getNextEntry();
continue;
}
String entryName = profileEntry.getName();
String profileName = String.join("/", artifact.getName(), archiveName, entryName);
String outputPath = Paths.get(baseOutputPath, archiveName, entryName).toString();
String contents = IOUtils.toString(tis);
result.add((new ProfileFactory() {
@Override
protected void setProfile(Profile profile, DeploymentConfiguration deploymentConfiguration, SpinnakerRuntimeSettings endpoints) {
profile.setContents(profile.getBaseContents());
}
@Override
protected Profile getBaseProfile(String name, String version, String outputFile) {
return new Profile(name, version, outputFile, contents);
}
@Override
protected boolean showEditWarning() {
return false;
}
@Override
protected ArtifactService getArtifactService() {
return artifactService;
}
@Override
public SpinnakerArtifact getArtifact() {
return artifact;
}
@Override
protected String commentPrefix() {
return null;
}
}).getProfile(profileName, outputPath, deploymentConfiguration, null));
profileEntry = tis.getNextEntry();
}
return result;
} catch (IOException e) {
throw new HalException(Problem.Severity.FATAL, "Failed to read profile entry", e);
}
}
use of org.apache.commons.compress.archivers.ArchiveEntry in project BWAPI4J by OpenBW.
the class DummyDataUtils method readMultiLinesAsStringTokensFromArchiveFile.
public static List<List<String>> readMultiLinesAsStringTokensFromArchiveFile(final String archiveFilename, final String mapHash, final String regex) throws IOException {
final InputStream inputStream = createInputStreamForDummyDataSet(archiveFilename);
final String mapShortHash = determineMapShortHash(mapHash);
try (final ArchiveInputStream tarIn = new TarArchiveInputStream(new BZip2CompressorInputStream(inputStream));
final BufferedReader buffer = new BufferedReader(new InputStreamReader(tarIn))) {
final ArchiveEntry nextEntry = getArchiveEntry(tarIn, mapShortHash);
Assert.assertNotNull(nextEntry);
final List<List<String>> data = new ArrayList<>();
String line;
while ((line = buffer.readLine()) != null) {
if (line.isEmpty()) {
continue;
}
final String[] tokens = line.split(regex);
final List<String> strTokens = new ArrayList<>();
for (final String token : tokens) {
final String tokenTrimmed = token.trim();
if (tokenTrimmed.isEmpty()) {
continue;
}
strTokens.add(tokenTrimmed);
}
data.add(strTokens);
}
int valuesReadCount = 0;
for (final List<String> list : data) {
valuesReadCount += list.size();
}
logger.debug("Read " + valuesReadCount + " values from " + archiveFilename);
return data;
}
}
Aggregations