List of usage examples for org.apache.commons.compress.archivers.tar TarArchiveInputStream getNextEntry
public ArchiveEntry getNextEntry() throws IOException
From source file:com.impetus.ankush.common.controller.listener.StartupListener.java
/** * Sets the agent version.//from w ww . j a va 2 s . com */ private static void setAgentVersion() { // current agent version String agentBuildVersion = new String(); try { // Resource base path. String basePath = AppStoreWrapper.getResourcePath(); // Creating agent bundle path. String agentBundlePath = basePath + "scripts/agent/" + AgentDeployer.AGENT_BUNDLE_NAME; FileInputStream fileInputStream = new FileInputStream(agentBundlePath); BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream); GzipCompressorInputStream gzInputStream = new GzipCompressorInputStream(bufferedInputStream); TarArchiveInputStream tarInputStream = new TarArchiveInputStream(gzInputStream); TarArchiveEntry entry = null; while ((entry = (TarArchiveEntry) tarInputStream.getNextEntry()) != null) { if (entry.getName().equals(AgentDeployer.AGENT_VERSION_FILENAME)) { final int BUFFER = 10; byte data[] = new byte[BUFFER]; tarInputStream.read(data, 0, BUFFER); String version = new String(data); agentBuildVersion = version.trim(); // Set the agent version in the AppStore with key as // agentVersion AppStore.setObject(AppStoreWrapper.KEY_AGENT_VERISON, agentBuildVersion); } } } catch (Exception e) { // log error message. log.error(e.getMessage(), e); } }
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 w w w .j a v a2s . 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: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 ww.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 */ 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.linkedin.thirdeye.bootstrap.util.TarGzCompressionUtils.java
/** Untar an input file into an output file. /*from w ww . 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 */ 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 ww . j av a2 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:de.uka.ilkd.key.dl.gui.initialdialog.gui.ToolInstaller.java
/** * @param tmp//from ww w . ja va 2 s. co m * @param dir * @throws IOException * @throws ArchiveException */ private static void untar(File file, File dir, FileType ft, PropertySetter ps, JProgressBar bar) throws IOException, ArchiveException { FileInputStream fis = new FileInputStream(file); InputStream is; switch (ft) { case TARGZ: is = new GZIPInputStream(fis); break; case TARBZ2: is = new BZip2CompressorInputStream(fis); break; default: throw new IllegalArgumentException("Don't know how to handle filetype: " + ft); } final TarArchiveInputStream debInputStream = (TarArchiveInputStream) new ArchiveStreamFactory() .createArchiveInputStream("tar", is); TarArchiveEntry entry = null; int value = 0; while ((entry = (TarArchiveEntry) debInputStream.getNextEntry()) != null) { bar.setValue(value++); final File outputFile = new File(dir, 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); if (OSInfosDefault.INSTANCE.getOs() == OperatingSystem.OSX) { // FIXME: we need to make everything executable as somehow // the executable bit is not preserved in // OSX outputFile.setExecutable(true); } if (ps.filterFilename(outputFile)) { ps.setProperty(outputFile.getAbsolutePath()); } outputFileStream.flush(); outputFileStream.close(); } } debInputStream.close(); is.close(); file.delete(); }
From source file:bb.io.TarUtil.java
/** * Extracts the contents of tarFile to directoryExtraction. * <p>/*w w w . j a v a 2 s . com*/ * It is an error if tarFile does not exist, is not a normal file, or is not in the proper TAR format. * In contrast, directoryExtraction need not exist, since it (and any parent directories) will be created if necessary. * <p> * Optional GZIP decompression may also be done. * Normally, tarFile must be a path which ends in a ".tar" (case insensitive) extension. * However, this method will also accept either ".tar.gz" or ".tgz" extensions, * in which case it will perform GZIP decompression on tarFile as part of extracting. * <p> * @param tarFile the TAR archive file * @param directoryExtraction the directory that will extract the contents of tarFile into * @param overwrite specifies whether or not extraction is allowed to overwrite an existing normal file inside directoryExtraction * @throws IllegalArgumentException if tarFile is {@link Check#validFile not valid}; * if directoryExtraction fails {@link DirUtil#ensureExists DirUtil.ensureExists}; * tarFile has an invalid extension * @throws SecurityException if a security manager exists and its SecurityManager.checkRead method * denies read access to tarFile or directoryExtraction * @throws IllegalStateException if directoryExtraction failed to be created or is not an actual directory but is some other type of file * @throws IOException if an I/O problem occurs */ public static void extract(File tarFile, File directoryExtraction, boolean overwrite) throws IllegalArgumentException, SecurityException, IllegalStateException, IOException { Check.arg().validFile(tarFile); DirUtil.ensureExists(directoryExtraction); TarArchiveInputStream tais = null; try { tais = new TarArchiveInputStream(getInputStream(tarFile)); for (TarArchiveEntry entry = (TarArchiveEntry) tais .getNextEntry(); entry != null; entry = (TarArchiveEntry) tais.getNextEntry()) { File path = new File(directoryExtraction, entry.getName()); if (path.exists() && path.isFile() && !overwrite) throw new IllegalStateException( path.getPath() + " is an existing normal file, but overwrite == false"); if (entry.isDirectory()) { DirUtil.ensureExists(path); } else { DirUtil.ensureExists(path.getParentFile()); // CRITICAL: the TAR format does not necessarily store all the directories as entries, so must make sure that they are created http://forum.java.sun.com/thread.jspa?threadID=573800&messageID=3115774 writeOutFile(tais, path); } } } finally { StreamUtil.close(tais); } }
From source file:edu.stanford.epad.common.util.EPADFileUtils.java
/** Untar an input file into an output file. //from w ww. 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 */ 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:bb.io.TarUtil.java
/** * Returns all the {@link TarArchiveEntry}s that can next be read by tarArchiveInputStream. * <p>//from w w w . j ava 2s . c om * Nothing should have been previously read from tarArchiveInputStream if the full result is desired. * Nothing more can be read from tarArchiveInputStream when this method returns, * since the final action will be to close tarArchiveInputStream. * <p> * @param tarArchiveInputStream the TarArchiveInputStream to get the entries from * @param sortResult if true, then the result is first sorted by each entry's name before return; * otherwise the order is the sequence read from tarArchiveInputStream * @throws IllegalArgumentException if tarArchiveInputStream == null * @throws IOException if an I/O problem occurs */ public static TarArchiveEntry[] getEntries(TarArchiveInputStream tarArchiveInputStream, boolean sortResult) throws IllegalArgumentException, IOException { Check.arg().notNull(tarArchiveInputStream); try { List<TarArchiveEntry> entries = new LinkedList<TarArchiveEntry>(); while (true) { TarArchiveEntry entry = (TarArchiveEntry) tarArchiveInputStream.getNextEntry(); if (entry == null) break; else entries.add(entry); } if (sortResult) { Collections.sort(entries, new Comparator<TarArchiveEntry>() { public int compare(TarArchiveEntry entry1, TarArchiveEntry entry2) throws IllegalArgumentException { return entry1.getName().compareTo(entry2.getName()); } }); } return entries.toArray(new TarArchiveEntry[entries.size()]); } finally { StreamUtil.close(tarArchiveInputStream); } }
From source file:jetbrains.exodus.util.CompressBackupUtilTest.java
@Test public void testFileArchived() throws Exception { File src = new File(randName + ".txt"); FileWriter fw = new FileWriter(src); fw.write("12345"); fw.close();//from w ww . j av a2s. c o m CompressBackupUtil.tar(src, dest); Assert.assertTrue("No destination archive created", dest.exists()); TarArchiveInputStream tai = new TarArchiveInputStream( new GZIPInputStream(new BufferedInputStream(new FileInputStream(dest)))); ArchiveEntry entry = tai.getNextEntry(); Assert.assertNotNull("No entry found in destination archive", entry); Assert.assertEquals("Entry has wrong size", 5, entry.getSize()); }