List of usage examples for org.apache.commons.compress.archivers.tar TarArchiveEntry isLink
public boolean isLink()
From source file:com.opentable.db.postgres.embedded.BundledPostgresBinaryResolver.java
/** * Unpack archive compressed by tar with bzip2 compression. By default system tar is used * (faster). If not found, then the java implementation takes place. * * @param tbzPath/*from w w w .j ava2s.c om*/ * The archive path. * @param targetDir * The directory to extract the content to. */ private static void extractTxz(String tbzPath, String targetDir) throws IOException { try (InputStream in = Files.newInputStream(Paths.get(tbzPath)); XZInputStream xzIn = new XZInputStream(in); TarArchiveInputStream tarIn = new TarArchiveInputStream(xzIn)) { TarArchiveEntry entry; while ((entry = tarIn.getNextTarEntry()) != null) { final String individualFile = entry.getName(); final File fsObject = new File(targetDir + "/" + individualFile); if (entry.isSymbolicLink() || entry.isLink()) { Path target = FileSystems.getDefault().getPath(entry.getLinkName()); Files.createSymbolicLink(fsObject.toPath(), target); } else if (entry.isFile()) { byte[] content = new byte[(int) entry.getSize()]; int read = tarIn.read(content, 0, content.length); Verify.verify(read != -1, "could not read %s", individualFile); mkdirs(fsObject.getParentFile()); try (OutputStream outputFile = new FileOutputStream(fsObject)) { IOUtils.write(content, outputFile); } } else if (entry.isDirectory()) { mkdirs(fsObject); } else { throw new UnsupportedOperationException( String.format("Unsupported entry found: %s", individualFile)); } if (individualFile.startsWith("bin/") || individualFile.startsWith("./bin/")) { fsObject.setExecutable(true); } } } }
From source file:io.sloeber.core.managers.InternalPackageManager.java
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<>(); String pathPrefix = new String(); 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; }// www . ja va 2s.co m // 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()); pMonitor.subTask("Processing " + name); //$NON-NLS-1$ { // 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; } } } // Skip git metadata // 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) { slash = name.indexOf("/", slash); //$NON-NLS-1$ if (slash == -1) { throw new IOException(Messages.Manager_archiver_eror_single_root_folder_required); } slash++; localstripPath--; } pathPrefix = name.substring(0, slash); } // Strip the common path prefix when requested if (!name.startsWith(pathPrefix)) { throw new IOException(Messages.Manager_archive_error_root_folder_name_mismatch.replace(FILE, name) .replace(FOLDER, 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_archive_error_root_folder_name_mismatch .replace(FILE, name).replace(FOLDER, 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_archive_error_symbolic_link_to_absolute_path .replace(FILE, outputFile.toString()).replace(FOLDER, outputLinkedFile.toString())); System.err.println(); } } // Safety check if (isDirectory) { if (outputFile.isFile() && !overwrite) { throw new IOException( Messages.Manager_Cant_create_folder_exists.replace(FILE, outputFile.getPath())); } } else { // - isLink // - isSymLink // - anything else if (outputFile.exists() && !overwrite) { throw new IOException( Messages.Manager_Cant_extract_file_exist.replace(FILE, outputFile.getPath())); } } // Extract the entry if (isDirectory) { if (!outputFile.exists() && !outputFile.mkdirs()) { throw new IOException(Messages.Manager_Cant_create_folder.replace(FILE, outputFile.getPath())); } 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; }
From source file:io.sloeber.core.managers.Manager.java
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<>(); String pathPrefix = ""; //$NON-NLS-1$ 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; }//from w ww. j a v a 2 s. co m // 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()); pMonitor.subTask("Processing " + name); //$NON-NLS-1$ { // 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; } } } // Skip git metadata // 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) { slash = name.indexOf("/", slash); //$NON-NLS-1$ 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 { // - isLink // - isSymLink // - 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; }
From source file:cc.arduino.utils.ArchiveExtractor.java
public void extract(File archiveFile, File destFolder, int stripPath, boolean overwrite) 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<>(); ArchiveInputStream in = null;// w w w . j ava 2 s . co m try { // Create an ArchiveInputStream with the correct archiving algorithm if (archiveFile.getName().endsWith("tar.bz2")) { in = new TarArchiveInputStream(new BZip2CompressorInputStream(new FileInputStream(archiveFile))); } else if (archiveFile.getName().endsWith("zip")) { in = new ZipArchiveInputStream(new FileInputStream(archiveFile)); } else if (archiveFile.getName().endsWith("tar.gz")) { in = new TarArchiveInputStream(new GzipCompressorInputStream(new FileInputStream(archiveFile))); } else if (archiveFile.getName().endsWith("tar")) { in = new TarArchiveInputStream(new FileInputStream(archiveFile)); } else { throw new IOException("Archive format not supported."); } 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 = entry.getLastModifiedDate().getTime(); { // 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("._")) { continue; } } else { if (name.substring(slash + 1).startsWith("._")) { continue; } } } // Skip git metadata // http://www.unix.com/unix-for-dummies-questions-and-answers/124958-file-pax_global_header-means-what.html if (name.contains("pax_global_header")) { continue; } if (entry instanceof TarArchiveEntry) { TarArchiveEntry tarEntry = (TarArchiveEntry) entry; mode = 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 if (stripPath > 0 && pathPrefix.isEmpty()) { int slash = 0; while (stripPath > 0) { slash = name.indexOf("/", slash); if (slash == -1) { throw new IOException("Invalid archive: it must contain a single root folder"); } slash++; stripPath--; } pathPrefix = name.substring(0, slash); } // Strip the common path prefix when requested if (!name.startsWith(pathPrefix)) { throw new IOException("Invalid archive: it must contain a single root folder while file " + name + " is outside " + pathPrefix); } name = name.substring(pathPrefix.length()); if (name.isEmpty()) { continue; } File outputFile = new File(destFolder, name); File outputLinkedFile = null; if (isLink) { if (!linkName.startsWith(pathPrefix)) { throw new IOException("Invalid archive: it must contain a single root folder while file " + linkName + " 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(I18n.format(tr("Warning: file {0} links to an absolute path {1}"), outputFile, outputLinkedFile)); System.err.println(); } } // Safety check if (isDirectory) { if (outputFile.isFile() && !overwrite) { throw new IOException( "Can't create folder " + outputFile + ", a file with the same name exists!"); } } else { // - isLink // - isSymLink // - anything else if (outputFile.exists() && !overwrite) { throw new IOException("Can't extract file " + outputFile + ", file already exists!"); } } // Extract the entry if (isDirectory) { if (!outputFile.exists() && !outputFile.mkdirs()) { throw new IOException("Could not 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); } // Set file/folder permission if (mode != null && !isSymLink && outputFile.exists()) { platform.chmod(outputFile, mode); } } for (Map.Entry<File, File> entry : hardLinks.entrySet()) { if (entry.getKey().exists() && overwrite) { entry.getKey().delete(); } platform.link(entry.getValue(), entry.getKey()); Integer mode = hardLinksMode.get(entry.getKey()); if (mode != null) { platform.chmod(entry.getKey(), mode); } } for (Map.Entry<File, String> entry : symLinks.entrySet()) { if (entry.getKey().exists() && overwrite) { entry.getKey().delete(); } platform.symlink(entry.getValue(), entry.getKey()); entry.getKey().setLastModified(symLinksModifiedTimes.get(entry.getKey())); } } finally { IOUtils.closeQuietly(in); } // Set folders timestamps for (File folder : foldersTimestamps.keySet()) { folder.setLastModified(foldersTimestamps.get(folder)); } }
From source file:com.google.devtools.build.lib.bazel.repository.CompressedTarFunction.java
@Override public Path decompress(DecompressorDescriptor descriptor) throws RepositoryFunctionException { Optional<String> prefix = descriptor.prefix(); boolean foundPrefix = false; try (InputStream decompressorStream = getDecompressorStream(descriptor)) { TarArchiveInputStream tarStream = new TarArchiveInputStream(decompressorStream); TarArchiveEntry entry; while ((entry = tarStream.getNextTarEntry()) != null) { StripPrefixedPath entryPath = StripPrefixedPath.maybeDeprefix(entry.getName(), prefix); foundPrefix = foundPrefix || entryPath.foundPrefix(); if (entryPath.skip()) { continue; }// w w w . j a va 2s .c o m Path filename = descriptor.repositoryPath().getRelative(entryPath.getPathFragment()); FileSystemUtils.createDirectoryAndParents(filename.getParentDirectory()); if (entry.isDirectory()) { FileSystemUtils.createDirectoryAndParents(filename); } else { if (entry.isSymbolicLink() || entry.isLink()) { PathFragment linkName = new PathFragment(entry.getLinkName()); boolean wasAbsolute = linkName.isAbsolute(); // Strip the prefix from the link path if set. linkName = StripPrefixedPath.maybeDeprefix(linkName.getPathString(), prefix) .getPathFragment(); if (wasAbsolute) { // Recover the path to an absolute path as maybeDeprefix() relativize the path // even if the prefix is not set linkName = descriptor.repositoryPath().getRelative(linkName).asFragment(); } if (entry.isSymbolicLink()) { FileSystemUtils.ensureSymbolicLink(filename, linkName); } else { FileSystemUtils.createHardLink(filename, descriptor.repositoryPath().getRelative(linkName)); } } else { Files.copy(tarStream, filename.getPathFile().toPath(), StandardCopyOption.REPLACE_EXISTING); filename.chmod(entry.getMode()); // This can only be done on real files, not links, or it will skip the reader to // the next "real" file to try to find the mod time info. Date lastModified = entry.getLastModifiedDate(); filename.setLastModifiedTime(lastModified.getTime()); } } } } catch (IOException e) { throw new RepositoryFunctionException(e, Transience.TRANSIENT); } if (prefix.isPresent() && !foundPrefix) { throw new RepositoryFunctionException( new IOException("Prefix " + prefix.get() + " was given, but not found in the archive"), Transience.PERSISTENT); } return descriptor.repositoryPath(); }
From source file:org.eclipse.cdt.arduino.core.internal.board.ArduinoManager.java
public static void downloadAndInstall(String url, String archiveFileName, Path installPath, IProgressMonitor monitor) throws IOException { Exception error = null;/*w w w.java2 s .c o m*/ for (int retries = 3; retries > 0 && !monitor.isCanceled(); --retries) { try { URL dl = new URL(url); Path dlDir = ArduinoPreferences.getArduinoHome().resolve("downloads"); //$NON-NLS-1$ Files.createDirectories(dlDir); Path archivePath = dlDir.resolve(archiveFileName); URLConnection conn = dl.openConnection(); conn.setConnectTimeout(10000); conn.setReadTimeout(10000); Files.copy(conn.getInputStream(), archivePath, StandardCopyOption.REPLACE_EXISTING); boolean isWin = Platform.getOS().equals(Platform.OS_WIN32); // extract ArchiveInputStream archiveIn = null; try { String compressor = null; String archiver = null; if (archiveFileName.endsWith("tar.bz2")) { //$NON-NLS-1$ compressor = CompressorStreamFactory.BZIP2; archiver = ArchiveStreamFactory.TAR; } else if (archiveFileName.endsWith(".tar.gz") || archiveFileName.endsWith(".tgz")) { //$NON-NLS-1$ //$NON-NLS-2$ compressor = CompressorStreamFactory.GZIP; archiver = ArchiveStreamFactory.TAR; } else if (archiveFileName.endsWith(".tar.xz")) { //$NON-NLS-1$ compressor = CompressorStreamFactory.XZ; archiver = ArchiveStreamFactory.TAR; } else if (archiveFileName.endsWith(".zip")) { //$NON-NLS-1$ archiver = ArchiveStreamFactory.ZIP; } InputStream in = new BufferedInputStream(new FileInputStream(archivePath.toFile())); if (compressor != null) { in = new CompressorStreamFactory().createCompressorInputStream(compressor, in); } archiveIn = new ArchiveStreamFactory().createArchiveInputStream(archiver, in); for (ArchiveEntry entry = archiveIn.getNextEntry(); entry != null; entry = archiveIn .getNextEntry()) { if (entry.isDirectory()) { continue; } // Magic file for git tarballs Path path = Paths.get(entry.getName()); if (path.endsWith("pax_global_header")) { //$NON-NLS-1$ continue; } // Strip the first directory of the path Path entryPath; switch (path.getName(0).toString()) { case "i586": case "i686": // Cheat for Intel entryPath = installPath.resolve(path); break; default: entryPath = installPath.resolve(path.subpath(1, path.getNameCount())); } Files.createDirectories(entryPath.getParent()); if (entry instanceof TarArchiveEntry) { TarArchiveEntry tarEntry = (TarArchiveEntry) entry; if (tarEntry.isLink()) { Path linkPath = Paths.get(tarEntry.getLinkName()); linkPath = installPath.resolve(linkPath.subpath(1, linkPath.getNameCount())); Files.deleteIfExists(entryPath); Files.createSymbolicLink(entryPath, entryPath.getParent().relativize(linkPath)); } else if (tarEntry.isSymbolicLink()) { Path linkPath = Paths.get(tarEntry.getLinkName()); Files.deleteIfExists(entryPath); Files.createSymbolicLink(entryPath, linkPath); } else { Files.copy(archiveIn, entryPath, StandardCopyOption.REPLACE_EXISTING); } if (!isWin && !tarEntry.isSymbolicLink()) { int mode = tarEntry.getMode(); Files.setPosixFilePermissions(entryPath, toPerms(mode)); } } else { Files.copy(archiveIn, entryPath, StandardCopyOption.REPLACE_EXISTING); } } } finally { if (archiveIn != null) { archiveIn.close(); } } return; } catch (IOException | CompressorException | ArchiveException e) { error = e; // retry } } // out of retries if (error instanceof IOException) { throw (IOException) error; } else { throw new IOException(error); } }