List of usage examples for org.apache.commons.compress.archivers.tar TarArchiveInputStream close
public void close() throws IOException
From source file:com.streamsets.datacollector.util.UntarUtility.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 .j a 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 */ private 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.ning.billing.beatrix.osgi.SetupBundleWithAssertion.java
private static void unTar(final File inputFile, final File outputDir) throws IOException, ArchiveException { InputStream is = null;//from www . java2s. c o m TarArchiveInputStream archiveInputStream = null; TarArchiveEntry entry = null; try { is = new FileInputStream(inputFile); archiveInputStream = (TarArchiveInputStream) new ArchiveStreamFactory().createArchiveInputStream("tar", is); while ((entry = (TarArchiveEntry) archiveInputStream.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); ByteStreams.copy(archiveInputStream, outputFileStream); outputFileStream.close(); } } } finally { if (archiveInputStream != null) { archiveInputStream.close(); } if (is != null) { is.close(); } } }
From source file:io.crate.testing.Utils.java
static void uncompressTarGZ(File tarFile, File dest) throws IOException { TarArchiveInputStream tarIn = new TarArchiveInputStream( new GzipCompressorInputStream(new BufferedInputStream(new FileInputStream(tarFile)))); TarArchiveEntry tarEntry = tarIn.getNextTarEntry(); // tarIn is a TarArchiveInputStream while (tarEntry != null) { Path entryPath = Paths.get(tarEntry.getName()); if (entryPath.getNameCount() == 1) { tarEntry = tarIn.getNextTarEntry(); continue; }/*from w ww .j a va2 s . c o m*/ Path strippedPath = entryPath.subpath(1, entryPath.getNameCount()); File destPath = new File(dest, strippedPath.toString()); if (tarEntry.isDirectory()) { destPath.mkdirs(); } else { destPath.createNewFile(); byte[] btoRead = new byte[1024]; BufferedOutputStream bout = new BufferedOutputStream(new FileOutputStream(destPath)); int len; while ((len = tarIn.read(btoRead)) != -1) { bout.write(btoRead, 0, len); } bout.close(); if (destPath.getParent().equals(dest.getPath() + "/bin")) { destPath.setExecutable(true); } } tarEntry = tarIn.getNextTarEntry(); } tarIn.close(); }
From source file:com.cloudbees.clickstack.util.Files2.java
/** * TODO recopy file permissions/*from ww w.java 2 s . co m*/ * <p/> * Uncompress the specified tgz file to the specified destination directory. * Replaces any files in the destination, if they already exist. * * @param tgzFile the name of the zip file to extract * @param destDir the directory to unzip to * @throws RuntimeIOException */ public static void untgz(@Nonnull Path tgzFile, @Nonnull Path destDir) throws RuntimeIOException { try { //if the destination doesn't exist, create it if (Files.notExists(destDir)) { logger.trace("Create dir: {}", destDir); Files.createDirectories(destDir); } TarArchiveInputStream in = new TarArchiveInputStream( new GzipCompressorInputStream(Files.newInputStream(tgzFile))); TarArchiveEntry entry; while ((entry = in.getNextTarEntry()) != null) { if (entry.isDirectory()) { Path dir = destDir.resolve(entry.getName()); logger.trace("Create dir {}", dir); Files.createDirectories(dir); } else { Path file = destDir.resolve(entry.getName()); logger.trace("Create file {}: {} bytes", file, entry.getSize()); OutputStream out = Files.newOutputStream(file); IOUtils.copy(in, out); out.close(); } } in.close(); } catch (IOException e) { throw new RuntimeIOException("Exception expanding " + tgzFile + " to " + destDir, e); } }
From source file:gdt.data.entity.ArchiveHandler.java
private static boolean hasEntitiesDirInTarStream(TarArchiveInputStream tis) { try {// ww w. j av a 2 s . co m // System.out.println("ArchiveHandler:hasEntitiesDirInTarStream:BEGIN:tis="+tis.getCount()); TarArchiveEntry entry = null; String entryName$; while ((entry = tis.getNextTarEntry()) != null) { entryName$ = entry.getName(); System.out.println("ArchiveHandler:hasEntitiesDirInTarStream:entry=" + entryName$); if (entryName$.startsWith(Entigrator.ENTITY_BASE)) { tis.close(); return true; } } tis.close(); return false; } catch (Exception e) { Logger.getLogger(ArchiveHandler.class.getName()).severe(e.toString()); return false; } }
From source file:com.cloudera.knittingboar.utils.Utils.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 */ 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:net.ovres.tuxcourser.TarStreamCourseInput.java
/** * Create a stream corresponding to the given file. * This stream should be closed when it is not needed * any more./*from ww w .j a va 2s . c o m*/ * * This file should be in the main directory, but this * is not validated. * * @returns stream corresponding to the given file. */ public InputStream getInputStream(String filename) throws FileNotFoundException, IOException { String altFilename = filename.replaceAll(".rgb$", ".png"); // First, get a stream we can use... InputStream in = new FileInputStream(source); // Next, decompress it if possible... in = new UncompressedInputStream(in); // Now the tar file... TarArchiveInputStream tar = new TarArchiveInputStream(in); ArchiveEntry entry = null; ArchiveEntry e; e = tar.getNextEntry(); while (entry == null && e != null) { if ((e.getName().endsWith(filename) || e.getName().endsWith(altFilename)) && e.getName().indexOf(".xvpics") == -1) { entry = e; } else { e = tar.getNextEntry(); } } if (entry == null) { tar.close(); throw new FileNotFoundException("File not found in " + source + ": " + filename); } logger.fine("Using: " + entry.getName()); // We could just return tar at this point... // But it's a bit nicer to clean up the tar file // immediately, and return a byte array stream instead. int size = (int) (entry.getSize()); byte[] b = new byte[size]; int num = tar.read(b, 0, size); if (num < size) { logger.warning("Tried to read " + size + " bytes, got " + num); } ByteArrayInputStream intemp; intemp = new ByteArrayInputStream(b); // finally, clean up the various input streams tar.close(); in.close(); return intemp; }
From source file:ezbake.deployer.publishers.mesos.MesosPublisher.java
/** * Gets the path including file name to the executable Jar file of the app by searching to the bin folder. * * @param artifact - the artifact to get the jar path from * @return - The jar path to the executable jar * @throws Exception/* w ww .ja v a2s. c o m*/ */ private String getJarPath(DeploymentArtifact artifact) throws Exception { TarArchiveInputStream tarIn = new TarArchiveInputStream( new GZIPInputStream(new ByteArrayInputStream(artifact.getArtifact()))); String jarPath = null; TarArchiveEntry entry = tarIn.getNextTarEntry(); while (entry != null) { if (entry.getName().equalsIgnoreCase("bin/")) { entry = tarIn.getNextTarEntry(); jarPath = entry.getName(); break; } entry = tarIn.getNextTarEntry(); } tarIn.close(); return jarPath; }
From source file:com.hortonworks.amstore.view.AmbariStoreHelper.java
/** Untar an input file into an output file. // w ww .j av 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 { 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:edu.mit.lib.bagit.Loader.java
private void inflate(InputStream in, String fmt) throws IOException { switch (fmt) { case "zip": ZipInputStream zin = new ZipInputStream(in); ZipEntry entry = null;//from ww w .j a v a 2 s. c o m while ((entry = zin.getNextEntry()) != null) { File outFile = new File(base.getParent(), entry.getName()); outFile.getParentFile().mkdirs(); Files.copy(zin, outFile.toPath()); } zin.close(); break; case "tgz": TarArchiveInputStream tin = new TarArchiveInputStream(new GzipCompressorInputStream(in)); TarArchiveEntry tentry = null; while ((tentry = tin.getNextTarEntry()) != null) { File outFile = new File(base.getParent(), tentry.getName()); outFile.getParentFile().mkdirs(); Files.copy(tin, outFile.toPath()); } tin.close(); break; default: throw new IOException("Unsupported archive format: " + fmt); } }