List of usage examples for org.apache.commons.compress.archivers.tar TarArchiveEntry isDirectory
public boolean isDirectory()
From source file:at.illecker.sentistorm.commons.util.io.IOUtils.java
public static void extractTarGz(InputStream inputTarGzStream, String outDir, boolean logging) { try {//from ww w . j a va 2 s . co m GzipCompressorInputStream gzIn = new GzipCompressorInputStream(inputTarGzStream); TarArchiveInputStream tarIn = new TarArchiveInputStream(gzIn); // read Tar entries TarArchiveEntry entry = null; while ((entry = (TarArchiveEntry) tarIn.getNextEntry()) != null) { if (logging) { LOG.info("Extracting: " + outDir + File.separator + entry.getName()); } if (entry.isDirectory()) { // create directory File f = new File(outDir + File.separator + entry.getName()); f.mkdirs(); } else { // decompress file int count; byte data[] = new byte[EXTRACT_BUFFER_SIZE]; FileOutputStream fos = new FileOutputStream(outDir + File.separator + entry.getName()); BufferedOutputStream dest = new BufferedOutputStream(fos, EXTRACT_BUFFER_SIZE); while ((count = tarIn.read(data, 0, EXTRACT_BUFFER_SIZE)) != -1) { dest.write(data, 0, count); } dest.close(); } } // close input stream tarIn.close(); } catch (IOException e) { LOG.error("IOException: " + e.getMessage()); } }
From source file:com.linkedin.thirdeye.bootstrap.util.TarGzCompressionUtils.java
/** Untar an input file into an output file. //w w w .j a v a 2s .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 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.error("The directory already there. Deleting - " + outputFile.getAbsolutePath()); FileUtils.deleteDirectory(outputFile); } } 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.linkedin.pinot.common.utils.TarGzCompressionUtils.java
/** Untar an input file into an output file. //from w w w.j a v a 2 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 */ 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 BufferedInputStream(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.error("The directory already there. Deleting - " + outputFile.getAbsolutePath()); FileUtils.deleteDirectory(outputFile); } } 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:eu.eubrazilcc.lvl.core.io.FileCompressor.java
/** * Uncompress the content of a tarball file to the specified directory. * @param srcFile - the tarball to be uncompressed * @param outDir - directory where the files (and directories) extracted from the tarball will be written * @return the list of files (and directories) extracted from the tarball * @throws IOException when an exception occurs on uncompressing the tarball *///from w w w.ja v a 2 s.c om public static List<String> unGzipUnTar(final String srcFile, final String outDir) throws IOException { final List<String> uncompressedFiles = newArrayList(); FileInputStream fileInputStream = null; BufferedInputStream bufferedInputStream = null; GzipCompressorInputStream gzipCompressorInputStream = null; TarArchiveInputStream tarArchiveInputStream = null; BufferedInputStream bufferedInputStream2 = null; try { forceMkdir(new File(outDir)); fileInputStream = new FileInputStream(srcFile); bufferedInputStream = new BufferedInputStream(fileInputStream); gzipCompressorInputStream = new GzipCompressorInputStream(fileInputStream); tarArchiveInputStream = new TarArchiveInputStream(gzipCompressorInputStream); TarArchiveEntry entry = null; while ((entry = tarArchiveInputStream.getNextTarEntry()) != null) { final File outputFile = new File(outDir, entry.getName()); if (entry.isDirectory()) { // attempting to write output directory if (!outputFile.exists()) { // attempting to create output directory if (!outputFile.mkdirs()) { throw new IOException("Cannot create directory: " + outputFile.getCanonicalPath()); } } } else { // attempting to create output file final byte[] content = new byte[(int) entry.getSize()]; tarArchiveInputStream.read(content, 0, content.length); final FileOutputStream outputFileStream = new FileOutputStream(outputFile); write(content, outputFileStream); outputFileStream.close(); uncompressedFiles.add(outputFile.getCanonicalPath()); } } } finally { if (bufferedInputStream2 != null) { bufferedInputStream2.close(); } if (tarArchiveInputStream != null) { tarArchiveInputStream.close(); } if (gzipCompressorInputStream != null) { gzipCompressorInputStream.close(); } if (bufferedInputStream != null) { bufferedInputStream.close(); } if (fileInputStream != null) { fileInputStream.close(); } } return uncompressedFiles; }
From source file:com.jivesoftware.os.jive.utils.shell.utils.Untar.java
public static List<File> unTar(boolean verbose, final File outputDir, final File inputFile, boolean deleteOriginal) throws FileNotFoundException, IOException, ArchiveException { if (verbose) { System.out.println(String.format("untaring %s to dir %s.", inputFile.getAbsolutePath(), outputDir.getAbsolutePath())); }//from w w w . j a v a 2 s . c om final List<File> untaredFiles = new LinkedList<>(); final InputStream is = new FileInputStream(inputFile); final TarArchiveInputStream debInputStream = (TarArchiveInputStream) new ArchiveStreamFactory() .createArchiveInputStream("tar", is); TarArchiveEntry entry = null; while ((entry = (TarArchiveEntry) debInputStream.getNextEntry()) != null) { String entryName = entry.getName(); entryName = entryName.substring(entryName.indexOf("/") + 1); final File outputFile = new File(outputDir, entryName); if (entry.isDirectory()) { if (verbose) { System.out.println(String.format("Attempting to write output directory %s.", getRelativePath(outputDir, outputFile))); } if (!outputFile.exists()) { if (verbose) { System.out.println(String.format("Attempting to create output directory %s.", getRelativePath(outputDir, outputFile))); } if (!outputFile.mkdirs()) { throw new IllegalStateException(String.format("Couldn't create directory %s.", getRelativePath(outputDir, outputFile))); } } } else { try { if (verbose) { System.out.println( String.format("Creating output file %s.", getRelativePath(outputDir, outputFile))); } outputFile.getParentFile().mkdirs(); final OutputStream outputFileStream = new FileOutputStream(outputFile); IOUtils.copy(debInputStream, outputFileStream); outputFileStream.close(); if (getRelativePath(outputDir, outputFile).contains("bin/") || outputFile.getName().endsWith(".sh")) { // Hack! if (verbose) { System.out.println( String.format("chmod +x file %s.", getRelativePath(outputDir, outputFile))); } outputFile.setExecutable(true); } } catch (Exception x) { System.err.println("failed to untar " + getRelativePath(outputDir, outputFile) + " " + x); } } untaredFiles.add(outputFile); } debInputStream.close(); if (deleteOriginal) { FileUtils.forceDelete(inputFile); if (verbose) { System.out.println(String.format("deleted original file %s.", inputFile.getAbsolutePath())); } } return untaredFiles; }
From source file:msec.org.TarUtil.java
private static void dearchive(File destFile, TarArchiveInputStream tais) throws Exception { TarArchiveEntry entry = null; while ((entry = tais.getNextTarEntry()) != null) { // // w w w. j ava2 s. co m String dir = destFile.getPath() + File.separator + entry.getName(); File dirFile = new File(dir); // fileProber(dirFile); if (entry.isDirectory()) { dirFile.mkdirs(); } else { dearchiveFile(dirFile, tais); } } }
From source file:com.aliyun.odps.local.common.utils.ArchiveUtils.java
public static void unTar(File inFile, File untarDir) throws IOException, ArchiveException { final InputStream is = new FileInputStream(inFile); final TarArchiveInputStream in = (TarArchiveInputStream) new ArchiveStreamFactory() .createArchiveInputStream(ArchiveStreamFactory.TAR, is); TarArchiveEntry entry = null; untarDir.mkdirs();//from w ww . ja va2 s. c om while ((entry = (TarArchiveEntry) in.getNextEntry()) != null) { byte[] content = new byte[(int) entry.getSize()]; in.read(content); final File entryFile = new File(untarDir, entry.getName()); if (entry.isDirectory() && !entryFile.exists()) { if (!entryFile.mkdirs()) { throw new IOException("Create directory failed: " + entryFile.getAbsolutePath()); } } else { final OutputStream out = new FileOutputStream(entryFile); IOUtils.write(content, out); out.close(); } } in.close(); }
From source file:com.dubture.symfony.core.util.UncompressUtils.java
/** * Uncompress a tar archive entry to the specified output directory. * * @param tarInputStream The tar input stream associated with the entry * @param entry The tar archive entry to uncompress * @param outputDirectory The output directory where to put the uncompressed entry * * @throws IOException If an error occurs within the input or output stream *//* w ww. j av a 2s. c o m*/ public static void uncompressTarArchiveEntry(TarArchiveInputStream tarInputStream, TarArchiveEntry entry, File outputDirectory, EntryNameTranslator entryNameTranslator) throws IOException { String entryName = entryNameTranslator.translate(entry.getName()); if (entry.isDirectory()) { createDirectory(new File(outputDirectory, entryName)); return; } File outputFile = new File(outputDirectory, entryName); ensureDirectoryHierarchyExists(outputFile); BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(outputFile)); try { IOUtils.copy(tarInputStream, outputStream); addExecutableBit(outputFile, Permissions.fromMode(entry.getMode())); } finally { outputStream.close(); } }
From source file:com.hortonworks.amstore.view.AmbariStoreHelper.java
/** Untar an input file into an output file. /*w w w . j a va2s . c om*/ * 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.info(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.info(String.format("Attempting to write output directory %s.", outputFile.getAbsolutePath())); if (!outputFile.exists()) { LOG.info(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.info(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:com.zenome.bundlebus.Util.java
public static List<File> unTar(@NonNull final File tarFile, @NonNull final File outputDir) throws FileNotFoundException, IOException, ArchiveException { // Log.d(TAG, "tar filename : " + tarFile); // Log.d(TAG, "output folder : " + outputDir); final List<File> untaredFiles = new LinkedList<File>(); final InputStream is = new FileInputStream(tarFile); final TarArchiveInputStream debInputStream = (TarArchiveInputStream) new ArchiveStreamFactory() .createArchiveInputStream("tar", is); TarArchiveEntry entry = null; while ((entry = (TarArchiveEntry) debInputStream.getNextEntry()) != null) { String name = entry.getName(); if (name.startsWith("./")) { name = entry.getName().substring(2); }//from ww w. j a va2s. c o m final File outputFile = new File(outputDir, name); if (entry.isDirectory()) { // Log.d(TAG, "Attempting to write output directory " + outputFile.getAbsolutePath()); if (!outputFile.exists()) { Log.d(TAG, "Attempting to create output directory " + outputFile.getAbsolutePath()); if (!outputFile.mkdirs()) { throw new IllegalStateException( "Couldn't create directory " + outputFile.getAbsolutePath()); } } } else { // Log.d(TAG, "Creating output file " + outputFile.getAbsolutePath()); final OutputStream outputFileStream = new FileOutputStream(outputFile); Log.d(TAG, "IOUtils.copy : " + IOUtils.copy(debInputStream, outputFileStream)); outputFileStream.close(); } // Log.d(TAG, "Filename : " + outputFile); // Log.d(TAG, "is exist : " + outputFile.exists()); untaredFiles.add(outputFile); } debInputStream.close(); return untaredFiles; }