List of usage examples for org.apache.commons.compress.archivers.tar TarArchiveEntry getMode
public int getMode()
From source file:org.fuin.esmp.EventStoreDownloadMojo.java
private void unTarGz(final File archive, final File destDir) throws MojoExecutionException { try {/*from www . j av a 2 s .c om*/ final TarArchiveInputStream tarIn = new TarArchiveInputStream( new GzipCompressorInputStream(new BufferedInputStream(new FileInputStream(archive)))); try { TarArchiveEntry entry; while ((entry = (TarArchiveEntry) tarIn.getNextEntry()) != null) { LOG.info("Extracting: " + entry.getName()); final File file = new File(destDir, entry.getName()); if (entry.isDirectory()) { createIfNecessary(file); } else { int count; final byte[] data = new byte[MB]; final FileOutputStream fos = new FileOutputStream(file); final BufferedOutputStream dest = new BufferedOutputStream(fos, MB); try { while ((count = tarIn.read(data, 0, MB)) != -1) { dest.write(data, 0, count); } } finally { dest.close(); } entry.getMode(); } applyFileMode(file, new FileMode(entry.getMode())); } } finally { tarIn.close(); } } catch (final IOException ex) { throw new MojoExecutionException("Error uncompressing event store archive: " + archive, ex); } }
From source file:org.gradle.caching.internal.packaging.impl.TarBuildCacheEntryPacker.java
private void chmodUnpackedFile(TarArchiveEntry entry, File file) { fileSystem.chmod(file, entry.getMode() & UnixPermissions.PERM_MASK); }
From source file:org.gridgain.testsuites.bamboo.GridHadoopTestSuite.java
/** * Downloads and extracts an Apache product. * * @param appName Name of application for log messages. * @param homeVariable Pointer to home directory of the component. * @param downloadPath Relative download path of tar package. * @param destName Local directory name to install component. * @throws Exception If failed.// w w w.j a v a 2 s . c o m */ private static void download(String appName, String homeVariable, String downloadPath, String destName) throws Exception { String homeVal = GridSystemProperties.getString(homeVariable); if (!F.isEmpty(homeVal) && new File(homeVal).isDirectory()) { X.println(homeVariable + " is set to: " + homeVal); return; } List<String> urls = F.asList("http://apache-mirror.rbc.ru/pub/apache/", "http://www.eu.apache.org/dist/", "http://www.us.apache.org/dist/"); String tmpPath = System.getProperty("java.io.tmpdir"); X.println("tmp: " + tmpPath); File install = new File(tmpPath + File.separatorChar + "__hadoop"); File home = new File(install, destName); X.println("Setting " + homeVariable + " to " + home.getAbsolutePath()); System.setProperty(homeVariable, home.getAbsolutePath()); File successFile = new File(home, "__success"); if (home.exists()) { if (successFile.exists()) { X.println(appName + " distribution already exists."); return; } X.println(appName + " distribution is invalid and it will be deleted."); if (!U.delete(home)) throw new IOException("Failed to delete directory: " + install.getAbsolutePath()); } for (String url : urls) { if (!(install.exists() || install.mkdirs())) throw new IOException("Failed to create directory: " + install.getAbsolutePath()); URL u = new URL(url + downloadPath); X.println("Attempting to download from: " + u); try { URLConnection c = u.openConnection(); c.connect(); try (TarArchiveInputStream in = new TarArchiveInputStream( new GzipCompressorInputStream(new BufferedInputStream(c.getInputStream(), 32 * 1024)))) { TarArchiveEntry entry; while ((entry = in.getNextTarEntry()) != null) { File dest = new File(install, entry.getName()); if (entry.isDirectory()) { if (!dest.mkdirs()) throw new IllegalStateException(); } else { File parent = dest.getParentFile(); if (!(parent.exists() || parent.mkdirs())) throw new IllegalStateException(); X.print(" [" + dest); try (BufferedOutputStream out = new BufferedOutputStream( new FileOutputStream(dest, false), 128 * 1024)) { U.copy(in, out); out.flush(); } Files.setPosixFilePermissions(dest.toPath(), modeToPermissionSet(entry.getMode())); X.println("]"); } } } if (successFile.createNewFile()) return; } catch (Exception e) { e.printStackTrace(); U.delete(install); } } throw new IllegalStateException("Failed to install " + appName + "."); }
From source file:org.jboss.qa.jenkins.test.executor.utils.unpack.GUnZipper.java
@Override public void unpack(File archive, File destination) throws IOException { try (FileInputStream in = new FileInputStream(archive); GzipCompressorInputStream gzIn = new GzipCompressorInputStream(in); TarArchiveInputStream tarIn = new TarArchiveInputStream(gzIn)) { final Set<TarArchiveEntry> entries = getEntries(tarIn); if (ignoreRootFolders) { pathSegmentsToTrim = countRootFolders(entries); }/*w w w . ja va 2s . c o m*/ for (TarArchiveEntry entry : entries) { if (entry.isDirectory()) { continue; } final File file = new File(destination, trimPathSegments(entry.getName(), pathSegmentsToTrim)); if (!file.getParentFile().exists()) { file.getParentFile().mkdirs(); } try (FileOutputStream fos = new FileOutputStream(file)) { IOUtils.copy(tarIn, fos); // check for user-executable bit on entry and apply to file if ((entry.getMode() & 0100) != 0) { file.setExecutable(true); } } } } }
From source file:org.phoenicis.tools.archive.Tar.java
/** * Uncompress a tar//from w w w . jav a 2s . c om * * @param countingInputStream * to count the number of byte extracted * @param outputDir * The directory where files should be extracted * @return A list of extracted files * @throws ArchiveException * if the process fails */ private List<File> uncompress(final InputStream inputStream, CountingInputStream countingInputStream, final File outputDir, long finalSize, Consumer<ProgressEntity> stateCallback) { final List<File> uncompressedFiles = new LinkedList<>(); try (ArchiveInputStream debInputStream = new ArchiveStreamFactory().createArchiveInputStream("tar", inputStream)) { TarArchiveEntry entry; while ((entry = (TarArchiveEntry) debInputStream.getNextEntry()) != null) { final File outputFile = new File(outputDir, entry.getName()); if (entry.isDirectory()) { LOGGER.info(String.format("Attempting to write output directory %s.", outputFile.getAbsolutePath())); if (!outputFile.exists()) { LOGGER.info(String.format("Attempting to createPrefix output directory %s.", outputFile.getAbsolutePath())); Files.createDirectories(outputFile.toPath()); } } else { LOGGER.info(String.format("Creating output file %s (%s).", outputFile.getAbsolutePath(), entry.getMode())); if (entry.isSymbolicLink()) { Files.createSymbolicLink(Paths.get(outputFile.getAbsolutePath()), Paths.get(entry.getLinkName())); } else { try (final OutputStream outputFileStream = new FileOutputStream(outputFile)) { IOUtils.copy(debInputStream, outputFileStream); Files.setPosixFilePermissions(Paths.get(outputFile.getPath()), fileUtilities.octToPosixFilePermission(entry.getMode())); } } } uncompressedFiles.add(outputFile); stateCallback.accept(new ProgressEntity.Builder() .withPercent((double) countingInputStream.getCount() / (double) finalSize * (double) 100) .withProgressText("Extracting " + outputFile.getName()).build()); } return uncompressedFiles; } catch (IOException | org.apache.commons.compress.archivers.ArchiveException e) { throw new ArchiveException("Unable to extract the file", e); } }
From source file:org.rioproject.impl.util.DownloadManager.java
private static void unTar(File tarFile, File target) throws IOException { InputStream is = new FileInputStream(tarFile); ArchiveInputStream in;//from w w w .ja v a2s. c o m try { in = new ArchiveStreamFactory().createArchiveInputStream("tar", is); } catch (ArchiveException e) { IOException ioe = new IOException("Unarchving " + tarFile.getName()); ioe.initCause(e); throw ioe; } try { TarArchiveEntry entry; while ((entry = (TarArchiveEntry) in.getNextEntry()) != null) { File f = new File(target, entry.getName()); if (entry.isDirectory()) { if (f.mkdirs()) { logger.trace("Created directory {}", f.getPath()); } } else { if (!f.getParentFile().exists()) { if (f.getParentFile().mkdirs()) { logger.trace("Created {}", f.getParentFile().getPath()); } } if (f.createNewFile()) { logger.trace("Created {}", f.getName()); } OutputStream out = new FileOutputStream(f); IOUtils.copy(in, out); out.close(); } setPerms(f, entry.getMode()); } } finally { in.close(); } }
From source file:org.savantbuild.io.tar.TarBuilderTest.java
private static void assertTarContainsDirectory(Path tarFile, String entry, Integer mode, String userName, String groupName) throws IOException { InputStream is = Files.newInputStream(tarFile); if (tarFile.toString().endsWith(".gz")) { is = new GZIPInputStream(is); }//from w w w. j av a2s .c o m try (TarArchiveInputStream tis = new TarArchiveInputStream(is)) { TarArchiveEntry tarArchiveEntry = tis.getNextTarEntry(); while (tarArchiveEntry != null && !tarArchiveEntry.getName().equals(entry)) { tarArchiveEntry = tis.getNextTarEntry(); } if (tarArchiveEntry == null) { fail("Tar [" + tarFile + "] is missing entry [" + entry + "]"); } assertTrue(tarArchiveEntry.isDirectory()); if (mode != null) { assertEquals(tarArchiveEntry.getMode(), FileTools.toMode(mode)); } if (userName != null) { assertEquals(tarArchiveEntry.getUserName(), userName); } if (groupName != null) { assertEquals(tarArchiveEntry.getGroupName(), groupName); } } }
From source file:org.savantbuild.io.tar.TarTools.java
/** * Untars a TAR file. This also handles tar.gz files by checking the file extension. If the file extension ends in .gz * it will read the tarball through a GZIPInputStream. * * @param file The TAR file./*from ww w . j a v a 2 s.c o m*/ * @param to The directory to untar to. * @param useGroup Determines if the group name in the archive is used. * @param useOwner Determines if the owner name in the archive is used. * @throws IOException If the untar fails. */ public static void untar(Path file, Path to, boolean useGroup, boolean useOwner) throws IOException { if (Files.notExists(to)) { Files.createDirectories(to); } InputStream is = Files.newInputStream(file); if (file.toString().endsWith(".gz")) { is = new GZIPInputStream(is); } try (TarArchiveInputStream tis = new TarArchiveInputStream(is)) { TarArchiveEntry entry; while ((entry = tis.getNextTarEntry()) != null) { Path entryPath = to.resolve(entry.getName()); if (entry.isDirectory()) { // Skip directory entries that don't add any value if (entry.getMode() == 0 && entry.getGroupName() == null && entry.getUserName() == null) { continue; } if (Files.notExists(entryPath)) { Files.createDirectories(entryPath); } if (entry.getMode() != 0) { Set<PosixFilePermission> permissions = FileTools.toPosixPermissions(entry.getMode()); Files.setPosixFilePermissions(entryPath, permissions); } if (useGroup && entry.getGroupName() != null && !entry.getGroupName().trim().isEmpty()) { GroupPrincipal group = FileSystems.getDefault().getUserPrincipalLookupService() .lookupPrincipalByGroupName(entry.getGroupName()); Files.getFileAttributeView(entryPath, PosixFileAttributeView.class).setGroup(group); } if (useOwner && entry.getUserName() != null && !entry.getUserName().trim().isEmpty()) { UserPrincipal user = FileSystems.getDefault().getUserPrincipalLookupService() .lookupPrincipalByName(entry.getUserName()); Files.getFileAttributeView(entryPath, PosixFileAttributeView.class).setOwner(user); } } else { if (Files.notExists(entryPath.getParent())) { Files.createDirectories(entryPath.getParent()); } if (Files.isRegularFile(entryPath)) { if (Files.size(entryPath) == entry.getSize()) { continue; } else { Files.delete(entryPath); } } Files.createFile(entryPath); try (OutputStream os = Files.newOutputStream(entryPath)) { byte[] ba = new byte[1024]; int read; while ((read = tis.read(ba)) != -1) { if (read > 0) { os.write(ba, 0, read); } } } if (entry.getMode() != 0) { Set<PosixFilePermission> permissions = FileTools.toPosixPermissions(entry.getMode()); Files.setPosixFilePermissions(entryPath, permissions); } if (useGroup && entry.getGroupName() != null && !entry.getGroupName().trim().isEmpty()) { GroupPrincipal group = FileSystems.getDefault().getUserPrincipalLookupService() .lookupPrincipalByGroupName(entry.getGroupName()); Files.getFileAttributeView(entryPath, PosixFileAttributeView.class).setGroup(group); } if (useOwner && entry.getUserName() != null && !entry.getUserName().trim().isEmpty()) { UserPrincipal user = FileSystems.getDefault().getUserPrincipalLookupService() .lookupPrincipalByName(entry.getUserName()); Files.getFileAttributeView(entryPath, PosixFileAttributeView.class).setOwner(user); } } } } }
From source file:org.soulwing.credo.service.archive.TarGzipArchiveBuilderTest.java
@Test public void testBuildArchiveWithSingleEntry() throws Exception { byte[] archive = builder.beginEntry("file.txt", "UTF-8").addContent(new StringReader("content")).endEntry() .build();/* w ww .j av a2 s .com*/ TarArchiveInputStream tis = new TarArchiveInputStream( new GzipCompressorInputStream(new ByteArrayInputStream(archive))); TarArchiveEntry entry = tis.getNextTarEntry(); assertThat(entry.getMode(), is(equalTo(0100400))); assertThat(entry.getName(), is(equalTo("file.txt"))); assertThat(readContent(tis), is(equalTo("content"))); assertThat(tis.getNextEntry(), is(nullValue())); }
From source file:org.vafer.jdeb.ant.DebAntTaskTestCase.java
public void testTarFileSet() throws Exception { project.executeTarget("tarfileset"); File deb = new File("target/test-classes/test.deb"); assertTrue("package not build", deb.exists()); ArchiveWalker.walkData(deb, new ArchiveVisitor<TarArchiveEntry>() { public void visit(TarArchiveEntry entry, byte[] content) throws IOException { assertTrue("prefix: " + entry.getName(), entry.getName().startsWith("./foo/")); if (entry.isDirectory()) { assertEquals("directory mode (" + entry.getName() + ")", 040700, entry.getMode()); } else { assertEquals("file mode (" + entry.getName() + ")", 0100600, entry.getMode()); }/*from w w w . j a v a 2 s . c om*/ assertEquals("user", "ebourg", entry.getUserName()); assertEquals("group", "ebourg", entry.getGroupName()); } }, Compression.GZIP); }