List of usage examples for org.apache.commons.compress.archivers.tar TarArchiveEntry isDirectory
public boolean isDirectory()
From source file:org.phoenicis.tools.archive.Tar.java
/** * Uncompress a tar//from ww w.j a v a2 s . c o m * * @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.queeg.hadoop.tar.TarExtractor.java
public void extract(ByteSource source) throws IOException { TarArchiveInputStream archiveInputStream = new TarArchiveInputStream(source.openStream()); TarArchiveEntry entry; while ((entry = archiveInputStream.getNextTarEntry()) != null) { if (entry.isFile()) { BoundedInputStream entryInputStream = new BoundedInputStream(archiveInputStream, entry.getSize()); ByteSink sink = new PathByteSink(conf, new Path(destination, entry.getName())); sink.writeFrom(entryInputStream); } else if (entry.isDirectory()) { ByteStreams.skipFully(archiveInputStream, entry.getSize()); fs.mkdirs(new Path(destination, entry.getName())); }/*ww w . j a va 2 s . co m*/ } archiveInputStream.close(); }
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. j a v a2 s . 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.robovm.compilerhelper.Archiver.java
public static void unarchive(File archiveFile, File destinationDirectory) throws IOException { TarArchiveInputStream tar = new TarArchiveInputStream( new GzipCompressorInputStream(new BufferedInputStream(new FileInputStream(archiveFile)))); destinationDirectory.mkdirs();//from w w w. j av a 2 s. c om TarArchiveEntry entry = tar.getNextTarEntry(); while (entry != null) { File f = new File(destinationDirectory, entry.getName()); if (entry.isDirectory()) { f.mkdirs(); entry = tar.getNextTarEntry(); continue; } // TODO make this a bit cleaner String parentDir = f.getPath(); if (parentDir != null) { new File(parentDir.substring(0, parentDir.lastIndexOf(File.separator))).mkdirs(); } f.createNewFile(); byte[] bytes = new byte[1024]; int count; BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(f)); while ((count = tar.read(bytes)) > 0) { out.write(bytes, 0, count); } out.flush(); out.close(); entry = tar.getNextTarEntry(); } }
From source file:org.rsna.ctp.stdstages.ArchiveImportService.java
private void expandTAR(File tar, File dir) { try {/*from w ww . ja v a 2 s . c o m*/ TarArchiveInputStream tais = new TarArchiveInputStream(new FileInputStream(tar)); TarArchiveEntry tae; while ((tae = tais.getNextTarEntry()) != null) { if (!tae.isDirectory()) { FileOutputStream fos = new FileOutputStream(new File(dir, tae.getName())); byte[] buf = new byte[4096]; long count = tae.getSize(); while (count > 0) { int n = tais.read(buf, 0, buf.length); fos.write(buf, 0, n); count -= n; } fos.flush(); fos.close(); } } tais.close(); } catch (Exception ex) { logger.warn("Unable to expand: \"" + tar + "\"", ex); } }
From source file:org.sakaiproject.vtlgen.api.PackageUtil.java
public static void untar(String fileName, String targetPath) throws IOException { File tarArchiveFile = new File(fileName); BufferedOutputStream dest = null; FileInputStream tarArchiveStream = new FileInputStream(tarArchiveFile); TarArchiveInputStream tis = new TarArchiveInputStream(new BufferedInputStream(tarArchiveStream)); TarArchiveEntry entry = null; try {//from w ww . j a v a 2s.co m while ((entry = tis.getNextTarEntry()) != null) { int count; File outputFile = new File(targetPath, entry.getName()); if (entry.isDirectory()) { // entry is a directory if (!outputFile.exists()) { outputFile.mkdirs(); } } else { // entry is a file byte[] data = new byte[BUFFER_MAX]; FileOutputStream fos = new FileOutputStream(outputFile); dest = new BufferedOutputStream(fos, BUFFER_MAX); while ((count = tis.read(data, 0, BUFFER_MAX)) != -1) { dest.write(data, 0, count); } dest.flush(); dest.close(); } } } catch (Exception e) { e.printStackTrace(); } finally { if (dest != null) { dest.flush(); dest.close(); } tis.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 ww w .ja v 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 w w w .j a va 2s. 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.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 ww w.j a v a 2 s. co m*/ assertEquals("user", "ebourg", entry.getUserName()); assertEquals("group", "ebourg", entry.getGroupName()); } }, Compression.GZIP); }
From source file:org.vafer.jdeb.DebMakerTestCase.java
public void testControlFilesPermissions() throws Exception { File deb = new File("target/test-classes/test-control.deb"); if (deb.exists() && !deb.delete()) { fail("Couldn't delete " + deb); }/*from ww w . j av a 2 s.co m*/ Collection<DataProducer> producers = Arrays.asList(new DataProducer[] { new EmptyDataProducer() }); DebMaker maker = new DebMaker(new NullConsole(), producers); maker.setDeb(deb); maker.setControl(new File("target/test-classes/org/vafer/jdeb/deb/control")); maker.createDeb(Compression.NONE); // now reopen the package and check the control files assertTrue("package not build", deb.exists()); boolean found = ArchiveWalker.walkControl(deb, new ArchiveVisitor<TarArchiveEntry>() { public void visit(TarArchiveEntry entry, byte[] content) throws IOException { assertFalse("directory found in the control archive", entry.isDirectory()); assertTrue("prefix", entry.getName().startsWith("./")); InformationInputStream infoStream = new InformationInputStream(new ByteArrayInputStream(content)); IOUtils.copy(infoStream, NullOutputStream.NULL_OUTPUT_STREAM); if (infoStream.isShell()) { assertTrue("Permissions on " + entry.getName() + " should be 755", entry.getMode() == 0755); } else { assertTrue("Permissions on " + entry.getName() + " should be 644", entry.getMode() == 0644); } assertTrue(entry.getName() + " doesn't have Unix line endings", infoStream.hasUnixLineEndings()); assertEquals("user", "root", entry.getUserName()); assertEquals("group", "root", entry.getGroupName()); } }); assertTrue("Control files not found in the package", found); }