List of usage examples for org.apache.commons.compress.archivers.tar TarArchiveEntry isDirectory
public boolean isDirectory()
From source file:edu.stanford.epad.common.util.EPADFileUtils.java
/** Untar an input file into an output file. /*from w w w . j av a2s . c o m*/ * The output file is created in the output folder, having the same name * as the input file, minus the '.tar' extension. * * @param inputFile the input .tar file * @param outputDir the output directory file. * @throws IOException * @throws FileNotFoundException * * @return The {@link List} of {@link File}s with the untared content. * @throws ArchiveException */ public static List<File> unTar(final File inputFile, final File outputDir) throws Exception { log.debug( String.format("Untaring %s to dir %s.", inputFile.getAbsolutePath(), outputDir.getAbsolutePath())); final List<File> untaredFiles = new LinkedList<File>(); final InputStream is = new FileInputStream(inputFile); final TarArchiveInputStream debInputStream = (TarArchiveInputStream) new ArchiveStreamFactory() .createArchiveInputStream("tar", is); TarArchiveEntry entry = null; while ((entry = (TarArchiveEntry) debInputStream.getNextEntry()) != null) { final File outputFile = new File(outputDir, entry.getName()); if (entry.isDirectory()) { log.debug(String.format("Attempting to write output directory %s.", outputFile.getAbsolutePath())); if (!outputFile.exists()) { log.debug(String.format("Attempting to create output directory %s.", outputFile.getAbsolutePath())); if (!outputFile.mkdirs()) { throw new IllegalStateException( String.format("Couldn't create directory %s.", outputFile.getAbsolutePath())); } } } else { log.debug(String.format("Creating output file %s.", outputFile.getAbsolutePath())); final OutputStream outputFileStream = new FileOutputStream(outputFile); IOUtils.copy(debInputStream, outputFileStream); outputFileStream.close(); } untaredFiles.add(outputFile); } debInputStream.close(); return untaredFiles; }
From source file:indexing.WTDocIndexer.java
void indexTarArchive() throws Exception { GZIPInputStream gzipInputStream = new GZIPInputStream(new FileInputStream(new File(tarArchivePath))); TarArchiveInputStream tarArchiveInputStream = new TarArchiveInputStream(gzipInputStream); TarArchiveEntry tarEntry = tarArchiveInputStream.getNextTarEntry(); while (tarEntry != null) { if (!tarEntry.isDirectory()) { System.out.println("Extracting " + tarEntry.getName()); final OutputStream outputFileStream = new FileOutputStream(tarEntry.getName()); } else {//from w ww .j a v a2 s . c o m tarEntry = tarArchiveInputStream.getNextTarEntry(); } } }
From source file:edu.jhu.hlt.acute.iterators.tar.TarArchiveEntryByteIterator.java
@Override public byte[] next() { try {/*from w w w . j av a 2 s . c om*/ TarArchiveEntry entry = this.tis.getCurrentEntry(); if (entry.isDirectory()) { // Recurse. this.tis.getNextTarEntry(); this.next(); } byte[] bytes = IOUtils.toByteArray(this.tis); this.tis.getNextTarEntry(); return bytes; } catch (IOException e) { throw new RuntimeException(e); } }
From source file:edu.jhu.hlt.acute.iterators.tar.TarArchiveEntryByteIterator.java
@Override public boolean hasNext() { // possible states: // processed 1 file, and are now on a dir. // processed 1 file, and are now on another file. // done iterating (nothing left). // if any entry is null, done; return false. while (this.tis.getCurrentEntry() != null) { TarArchiveEntry entry = this.tis.getCurrentEntry(); if (!entry.isDirectory()) return true; else//from w ww . j a v a 2 s .c om try { this.tis.getNextEntry(); } catch (IOException ioe) { throw new RuntimeException(ioe); } } return this.tis.getCurrentEntry() != null; }
From source file:com.wenzani.maven.mongodb.TarUtils.java
private void unpackEntries(TarArchiveInputStream tis, TarArchiveEntry entry, File outputDir) throws IOException { if (entry.isDirectory()) { createDir(new File(outputDir, entry.getName())); File subDir = new File(outputDir, entry.getName()); for (TarArchiveEntry e : entry.getDirectoryEntries()) { unpackEntries(tis, e, subDir); }// w ww . j a v a 2s . c om return; } File outputFile = new File(outputDir, entry.getName()); if (!outputFile.getParentFile().exists()) { createDir(outputFile.getParentFile()); } BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(outputFile)); try { byte[] content = new byte[(int) entry.getSize()]; tis.read(content); if (content.length > 0) { IOUtils.copy(new ByteArrayInputStream(content), outputStream); } } finally { IOUtils.closeQuietly(outputStream); } }
From source file:com.comcast.cdn.traffic_control.traffic_router.neustar.data.TarExtractor.java
public boolean extractTo(File directory, InputStream inputStream) { try (TarArchiveInputStream tarArchiveInputStream = new TarArchiveInputStream(inputStream)) { TarArchiveEntry tarArchiveEntry; while ((tarArchiveEntry = tarArchiveInputStream.getNextTarEntry()) != null) { if (tarArchiveEntry.isDirectory()) { continue; }/*from ww w . jav a 2 s.com*/ File file = new File(directory, tarArchiveEntry.getName()); LOGGER.info("Extracting Tarfile entry " + tarArchiveEntry.getName() + " to temporary location " + file.getAbsolutePath()); if (!file.exists() && !file.createNewFile()) { LOGGER.warn("Failed to extract file to " + file.getAbsolutePath() + ", cannot create file, check permissions of " + directory.getAbsolutePath()); return false; } copyInputStreamToFile(tarArchiveInputStream, file); } } catch (IOException e) { LOGGER.error("Failed extracting tar archive to directory " + directory.getAbsolutePath() + " : " + e.getMessage()); return false; } return true; }
From source file:io.covert.binary.analysis.BuildTarBzSequenceFile.java
@Override public int run(String[] args) throws Exception { File inDir = new File(args[0]); Path name = new Path(args[1]); Text key = new Text(); BytesWritable val = new BytesWritable(); Configuration conf = getConf(); FileSystem fs = FileSystem.get(conf); if (!fs.exists(name)) { fs.mkdirs(name);//from w w w.j ava2 s. c o m } for (File file : inDir.listFiles()) { Path sequenceName = new Path(name, file.getName() + ".seq"); System.out.println("Writing to " + sequenceName); SequenceFile.Writer writer = SequenceFile.createWriter(fs, conf, sequenceName, Text.class, BytesWritable.class, CompressionType.RECORD); if (!file.isFile()) { System.out.println("Skipping " + file + " (not a file) ..."); continue; } final InputStream is = new FileInputStream(file); final TarArchiveInputStream debInputStream = (TarArchiveInputStream) new ArchiveStreamFactory() .createArchiveInputStream("tar", is); TarArchiveEntry entry = null; while ((entry = (TarArchiveEntry) debInputStream.getNextEntry()) != null) { if (!entry.isDirectory()) { final ByteArrayOutputStream outputFileStream = new ByteArrayOutputStream(); IOUtils.copy(debInputStream, outputFileStream); outputFileStream.close(); byte[] outputFile = outputFileStream.toByteArray(); val.set(outputFile, 0, outputFile.length); MessageDigest md = MessageDigest.getInstance("MD5"); md.update(outputFile); byte[] digest = md.digest(); String hexdigest = ""; for (int i = 0; i < digest.length; i++) { hexdigest += Integer.toString((digest[i] & 0xff) + 0x100, 16).substring(1); } key.set(hexdigest); writer.append(key, val); } } debInputStream.close(); writer.close(); } return 0; }
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 ww . j a v a 2s.co m*/ * 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:com.sshtools.common.vomanagementtool.common.VOHelper.java
private static void untar(InputStream in, String untarDir) throws IOException { TarArchiveInputStream tin = new TarArchiveInputStream(in); TarArchiveEntry entry = tin.getNextTarEntry(); //TarInputStream tin = new TarInputStream(in); //TarEntry tarEntry = tin.getNextEntry(); if (new File(untarDir).exists()) { //To handle the normal files (not symbolic files) while (entry != null) { if (!entry.isSymbolicLink()) { String filename = untarDir + File.separatorChar + entry.getName(); File destPath = new File(filename); if (!entry.isDirectory()) { FileOutputStream fout = new FileOutputStream(destPath); IOUtils.copy(tin, fout); //tin.copyEntryContents(fout); fout.close();/*from ww w. jav a 2s . c o m*/ } else { if (!destPath.exists()) { boolean success = destPath.mkdir(); if (!success) { throw new IOException("Couldn't create directory for VOMS support: " + destPath.getAbsolutePath()); } } } } entry = tin.getNextTarEntry(); //tarEntry = tin.getNextEntry(); } //tin.close(); //To handle the symbolic link files tin = new TarArchiveInputStream(in); entry = tin.getNextTarEntry(); while (entry != null) { if (entry.isSymbolicLink()) { File srcPath = new File(untarDir + File.separatorChar + entry.getLinkName()); File destPath = new File(untarDir + File.separatorChar + entry.getName()); copyFile(srcPath, destPath); //tarEntry = tin.getNextEntry(); } entry = tin.getNextTarEntry(); } tin.close(); } else { throw new IOException("Couldn't find directory: " + untarDir); } }
From source file:io.covert.binary.analysis.BuildSequenceFileFromTarball.java
public void load(FileSystem fs, Configuration conf, File inputTarball, Path outputDir) throws Exception { Text key = new Text(); BytesWritable val = new BytesWritable(); Path sequenceName = new Path(outputDir, inputTarball.getName() + ".seq"); System.out.println("Writing to " + sequenceName); SequenceFile.Writer writer = SequenceFile.createWriter(fs, conf, sequenceName, Text.class, BytesWritable.class, CompressionType.RECORD); InputStream is = new FileInputStream(inputTarball); if (inputTarball.toString().toLowerCase().endsWith(".gz")) { is = new GZIPInputStream(is); } else if (inputTarball.toString().toLowerCase().endsWith(".bz") || inputTarball.toString().endsWith(".bz2")) { is.read(); // read 'B' is.read(); // read 'Z' is = new CBZip2InputStream(is); }/*from w ww. j a v a 2 s . c om*/ final TarArchiveInputStream debInputStream = (TarArchiveInputStream) new ArchiveStreamFactory() .createArchiveInputStream("tar", is); TarArchiveEntry entry = null; while ((entry = (TarArchiveEntry) debInputStream.getNextEntry()) != null) { if (!entry.isDirectory()) { try { final ByteArrayOutputStream outputFileStream = new ByteArrayOutputStream(); IOUtils.copy(debInputStream, outputFileStream); outputFileStream.close(); byte[] outputFile = outputFileStream.toByteArray(); val.set(outputFile, 0, outputFile.length); MessageDigest md = MessageDigest.getInstance("MD5"); md.update(outputFile); byte[] digest = md.digest(); String hexdigest = ""; for (int i = 0; i < digest.length; i++) { hexdigest += Integer.toString((digest[i] & 0xff) + 0x100, 16).substring(1); } key.set(hexdigest); writer.append(key, val); } catch (IOException e) { System.err.println("Warning: tarball may be truncated: " + inputTarball); // Truncated Tarball break; } } } debInputStream.close(); writer.close(); }