List of usage examples for org.apache.commons.compress.archivers.tar TarArchiveEntry getName
public String getName()
From source file:com.android.tradefed.util.TarUtil.java
/** * Untar a tar file into a directory./*from ww w .j ava 2 s . c o m*/ * 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.bahmanm.karun.Utils.java
/** * Extracts a gzip'ed tar archive.// ww w. j a va 2s . c om * * @param archivePath Path to archive * @param destDir Destination directory * @throws IOException */ public synchronized static void extractTarGz(String archivePath, File destDir) throws IOException, ArchiveException { // copy File tarGzFile = File.createTempFile("karuntargz", "", destDir); copyFile(archivePath, tarGzFile.getAbsolutePath()); // decompress File tarFile = File.createTempFile("karuntar", "", destDir); FileInputStream fin = new FileInputStream(tarGzFile); BufferedInputStream bin = new BufferedInputStream(fin); FileOutputStream fout = new FileOutputStream(tarFile); GzipCompressorInputStream gzIn = new GzipCompressorInputStream(bin); final byte[] buffer = new byte[1024]; int n = 0; while (-1 != (n = gzIn.read(buffer))) { fout.write(buffer, 0, n); } bin.close(); fin.close(); gzIn.close(); fout.close(); // extract final InputStream is = new FileInputStream(tarFile); ArchiveInputStream ain = new ArchiveStreamFactory().createArchiveInputStream("tar", is); TarArchiveEntry entry = null; while ((entry = (TarArchiveEntry) ain.getNextEntry()) != null) { OutputStream out; if (entry.isDirectory()) { File f = new File(destDir, entry.getName()); f.mkdirs(); continue; } else out = new FileOutputStream(new File(destDir, entry.getName())); IOUtils.copy(ain, out); out.close(); } ain.close(); is.close(); }
From source file:com.cip.crane.agent.utils.FileExtractUtils.java
/** Untar an input file into an output file. /*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 { 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 w ww . j a v a 2s. 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:at.illecker.sentistorm.commons.util.io.IOUtils.java
public static void extractTarGz(InputStream inputTarGzStream, String outDir, boolean logging) { try {/*from w w w . j av a 2s . c o 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.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/* w w w . ja va2 s. c o 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. * /* ww w .j a va 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 */ 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; }
From source file:com.linkedin.thirdeye.bootstrap.util.TarGzCompressionUtils.java
/** Untar an input file into an output file. /* w w w.ja va 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 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. // w w w.java 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 */// w ww.ja va 2s . 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; }