List of usage examples for org.apache.commons.compress.archivers.tar TarArchiveEntry isDirectory
public boolean isDirectory()
From source file:com.google.cloud.public_datasets.nexrad2.GcsUntar.java
private static File[] unTar(final File inputFile, final File outputDir) throws FileNotFoundException, IOException, ArchiveException { // from// ww w. ja v a 2 s .c om // http://stackoverflow.com/questions/315618/how-do-i-extract-a-tar-file-in-java/7556307#7556307 log.info("tar xf " + inputFile + " to " + outputDir); final List<File> untaredFiles = new ArrayList<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()) { if (!outputFile.exists()) { if (!outputFile.mkdirs()) { throw new IllegalStateException( String.format("Couldn't create directory %s.", outputFile.getAbsolutePath())); } } } else { final OutputStream outputFileStream = new FileOutputStream(outputFile); IOUtils.copy(debInputStream, outputFileStream); outputFileStream.close(); } untaredFiles.add(outputFile); } debInputStream.close(); return untaredFiles.toArray(new File[0]); }
From source file:io.hightide.TemplateFetcher.java
public static Path extractTemplate(Path tempFile, Path targetDir) throws IOException { try (FileInputStream fin = new FileInputStream(tempFile.toFile()); BufferedInputStream in = new BufferedInputStream(fin); GzipCompressorInputStream gzIn = new GzipCompressorInputStream(in); TarArchiveInputStream tarIn = new TarArchiveInputStream(gzIn)) { TarArchiveEntry entry; Path rootDir = null;/*from w ww. jav a 2 s . co m*/ while ((entry = (TarArchiveEntry) tarIn.getNextEntry()) != null) { System.out.println("Extracting: " + entry.getName()); if (entry.isDirectory()) { if (isNull(rootDir)) { rootDir = targetDir.resolve(entry.getName()); } Files.createDirectory(targetDir.resolve(entry.getName())); } else { int count; byte data[] = new byte[BUFFER]; FileOutputStream fos = new FileOutputStream(targetDir.resolve(entry.getName()).toFile()); BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER); while ((count = tarIn.read(data, 0, BUFFER)) != -1) { dest.write(data, 0, count); } dest.close(); } } return rootDir; } }
From source file:com.codenvy.commons.lang.TarUtils.java
public static void untar(InputStream in, File targetDir) throws IOException { final TarArchiveInputStream tarIn = new TarArchiveInputStream(in); byte[] b = new byte[BUF_SIZE]; TarArchiveEntry tarEntry; while ((tarEntry = tarIn.getNextTarEntry()) != null) { final File file = new File(targetDir, tarEntry.getName()); if (tarEntry.isDirectory()) { file.mkdirs();/*from ww w .j ava 2 s . c o m*/ } else { final File parent = file.getParentFile(); if (!parent.exists()) { parent.mkdirs(); } try (FileOutputStream fos = new FileOutputStream(file)) { int r; while ((r = tarIn.read(b)) != -1) { fos.write(b, 0, r); } } } } }
From source file:it.geosolutions.tools.compress.file.reader.TarReader.java
/** * A method to read tar file://from ww w . j a v a 2 s . c o m * extract its content to the 'destDir' file (which could be * a directory or a file depending on the srcF file content) * @throws CompressorException */ public static void readTar(File srcF, File destDir) throws Exception { if (destDir == null) throw new IllegalArgumentException("Unable to extract to a null destination dir"); if (!destDir.canWrite() && !destDir.mkdirs()) throw new IllegalArgumentException("Unable to extract to a not writeable destination dir: " + destDir); FileInputStream fis = null; TarArchiveInputStream tis = null; BufferedInputStream bis = null; try { fis = new FileInputStream(srcF); bis = new BufferedInputStream(fis); tis = new TarArchiveInputStream(bis); TarArchiveEntry te = null; while ((te = tis.getNextTarEntry()) != null) { File curr_dest = new File(destDir, te.getName()); if (te.isDirectory()) { // create destination folder if (!curr_dest.exists()) curr_dest.mkdirs(); } else { writeFile(curr_dest, tis); } } } finally { if (tis != null) { try { tis.close(); } catch (IOException ioe) { } } if (bis != null) { try { bis.close(); } catch (IOException ioe) { } } if (fis != null) { try { fis.close(); } catch (IOException ioe) { } } } }
From source file:com.sangupta.jerry.util.ArchiveUtils.java
/** * Unpack the TAR file into the given output directory. * /* ww w. j a v a2s.c o m*/ * @param tarFile * the tar file that needs to be unpacked * * @param outputDir * the directory in which the entire file is unpacked * * @throws ArchiveException * if the TAR file is corrupt * * @throws IOException * if error occurs reading TAR file * * @return a list of {@link File} objects representing the files unpacked * from the TAR file * */ public static List<File> unpackTAR(final File tarFile, final File outputDir) throws ArchiveException, IOException { LOGGER.info("Untaring {} to dir {}", tarFile.getAbsolutePath(), outputDir.getAbsolutePath()); final List<File> untaredFiles = new LinkedList<File>(); InputStream fileInputStream = null; TarArchiveInputStream tarInputStream = null; try { fileInputStream = new FileInputStream(tarFile); tarInputStream = (TarArchiveInputStream) new ArchiveStreamFactory().createArchiveInputStream("tar", fileInputStream); TarArchiveEntry entry = null; while ((entry = (TarArchiveEntry) tarInputStream.getNextEntry()) != null) { final File outputFile = new File(outputDir, entry.getName()); if (entry.isDirectory()) { LOGGER.debug("Attempting to write output directory {}", outputFile.getAbsolutePath()); if (!outputFile.exists()) { LOGGER.debug("Attempting to create output directory {}", outputFile.getAbsolutePath()); if (!outputFile.mkdirs()) { throw new IllegalStateException( "Couldn't create directory: " + outputFile.getAbsolutePath()); } } // next file continue; } // write the plain file LOGGER.debug("Creating output file {}", outputFile.getAbsolutePath()); final OutputStream outputFileStream = new FileOutputStream(outputFile); IOUtils.copy(tarInputStream, outputFileStream); outputFileStream.close(); // add to the list of written files untaredFiles.add(outputFile); } } finally { org.apache.commons.io.IOUtils.closeQuietly(tarInputStream); org.apache.commons.io.IOUtils.closeQuietly(fileInputStream); } return untaredFiles; }
From source file:com.android.tradefed.util.TarUtil.java
/** * Untar a tar file into a directory.//from ww w .j a v a2 s. c om * tar.gz file need to up {@link #unGzip(File, File)} first. * * @param inputFile The tar file to extract * @param outputDir the directory where to put the extracted files. * @return The list of {@link File} untarred. * @throws FileNotFoundException * @throws IOException */ public static List<File> unTar(final File inputFile, final File outputDir) throws FileNotFoundException, IOException { CLog.i(String.format("Untaring %s to dir %s.", inputFile.getAbsolutePath(), outputDir.getAbsolutePath())); final List<File> untaredFiles = new LinkedList<File>(); final InputStream is = new FileInputStream(inputFile); TarArchiveInputStream debInputStream = null; try { 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()) { CLog.i(String.format("Attempting to write output directory %s.", outputFile.getAbsolutePath())); if (!outputFile.exists()) { CLog.i(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 { CLog.i(String.format("Creating output file %s.", outputFile.getAbsolutePath())); final OutputStream outputFileStream = new FileOutputStream(outputFile); IOUtils.copy(debInputStream, outputFileStream); StreamUtil.close(outputFileStream); } untaredFiles.add(outputFile); } } catch (ArchiveException ae) { // We rethrow the ArchiveException through a more generic one. throw new IOException(ae); } finally { StreamUtil.close(debInputStream); StreamUtil.close(is); } return untaredFiles; }
From source file:com.cip.crane.agent.utils.FileExtractUtils.java
/** Untar an input file into an output file. //from w ww . ja v a 2 s .com * 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 FileNotFoundException, IOException, ArchiveException { 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()) { if (!outputFile.exists()) { if (!outputFile.mkdirs()) { throw new IllegalStateException( String.format("Couldn't create directory %s.", outputFile.getAbsolutePath())); } } } else { final OutputStream outputFileStream = new FileOutputStream(outputFile); IOUtils.copy(debInputStream, outputFileStream); outputFileStream.close(); } untaredFiles.add(outputFile); } debInputStream.close(); return untaredFiles; }
From source file:net.orpiske.ssps.common.archive.TarArchiveUtils.java
/** * Unpacks a file/*from ww w .j a v a2s. c o m*/ * @param source source file * @param destination destination directory. If the directory does not * exists, it will be created * @param format archive format * @return the number of bytes processed * @throws SspsException * @throws ArchiveException * @throws IOException */ public static long unpack(File source, File destination, String format) throws SspsException, ArchiveException, IOException { if (!destination.exists()) { if (!destination.mkdirs()) { throw new IOException("Unable to create destination directory: " + destination.getPath()); } } else { if (!destination.isDirectory()) { throw new SspsException( "The provided destination " + destination.getPath() + " is not a directory"); } } ArchiveStreamFactory factory = new ArchiveStreamFactory(); FileInputStream inputFileStream = new FileInputStream(source); ArchiveInputStream archiveStream; try { archiveStream = factory.createArchiveInputStream(format, inputFileStream); } catch (ArchiveException e) { IOUtils.closeQuietly(inputFileStream); throw e; } OutputStream outStream = null; try { TarArchiveEntry entry = (TarArchiveEntry) archiveStream.getNextEntry(); while (entry != null) { File outFile = new File(destination, entry.getName()); if (entry.isDirectory()) { if (!outFile.exists()) { if (!outFile.mkdirs()) { throw new SspsException("Unable to create directory: " + outFile.getPath()); } } else { logger.warn("Directory " + outFile.getPath() + " already exists. Ignoring ..."); } } else { File parent = outFile.getParentFile(); if (!parent.exists()) { if (!parent.mkdirs()) { throw new IOException("Unable to create parent directories " + parent.getPath()); } } outStream = new FileOutputStream(outFile); IOUtils.copy(archiveStream, outStream); outStream.close(); } int mode = entry.getMode(); PermissionsUtils.setPermissions(mode, outFile); entry = (TarArchiveEntry) archiveStream.getNextEntry(); } inputFileStream.close(); archiveStream.close(); } finally { IOUtils.closeQuietly(outStream); IOUtils.closeQuietly(inputFileStream); IOUtils.closeQuietly(archiveStream); } return archiveStream.getBytesRead(); }
From source file:com.linkedin.thirdeye.impl.TarGzCompressionUtils.java
/** * Untar an input file into an output file. 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/*from w w w.j a v a 2 s .co m*/ * @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 FileNotFoundException, IOException, ArchiveException { LOGGER.debug( String.format("Untaring %s to dir %s.", inputFile.getAbsolutePath(), outputDir.getAbsolutePath())); TarArchiveInputStream debInputStream = null; InputStream is = null; final List<File> untaredFiles = new LinkedList<File>(); try { is = new GzipCompressorInputStream(new FileInputStream(inputFile)); 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()) { LOGGER.debug(String.format("Attempting to write output directory %s.", outputFile.getAbsolutePath())); if (!outputFile.exists()) { LOGGER.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 { LOGGER.debug(String.format("Creating output file %s.", outputFile.getAbsolutePath())); File directory = outputFile.getParentFile(); if (!directory.exists()) { directory.mkdirs(); } OutputStream outputFileStream = null; try { outputFileStream = new FileOutputStream(outputFile); IOUtils.copy(debInputStream, outputFileStream); } finally { IOUtils.closeQuietly(outputFileStream); } } untaredFiles.add(outputFile); } } finally { IOUtils.closeQuietly(debInputStream); IOUtils.closeQuietly(is); } return untaredFiles; }
From source file:com.cloudera.knittingboar.utils.Utils.java
/** * Untar an input file into an output file. * //from ww w . j ava2 s . 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 */ private static List<File> unTar(final File inputFile, final File outputDir) throws FileNotFoundException, IOException, ArchiveException { System.out.println( 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()) { System.out.println( String.format("Attempting to write output directory %s.", outputFile.getAbsolutePath())); if (!outputFile.exists()) { System.out.println(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 { System.out.println(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; }