List of usage examples for org.apache.commons.compress.archivers.tar TarArchiveInputStream close
public void close() throws IOException
From source file:org.finra.herd.service.helper.TarHelper.java
/** * Logs contents of a TAR file./* w ww .j a v a2 s. c om*/ * * @param tarFile the TAR file * * @throws IOException on error */ public void logTarFileContents(File tarFile) throws IOException { TarArchiveInputStream tarArchiveInputStream = null; try { tarArchiveInputStream = new TarArchiveInputStream(new FileInputStream(tarFile)); TarArchiveEntry entry; LOGGER.info(String.format("Listing the contents of \"%s\" TAR archive file:", tarFile.getPath())); while (null != (entry = tarArchiveInputStream.getNextTarEntry())) { LOGGER.info(String.format(" %s", entry.getName())); } } finally { if (tarArchiveInputStream != null) { tarArchiveInputStream.close(); } } }
From source file:org.fuin.esmp.EventStoreDownloadMojo.java
private void unTarGz(final File archive, final File destDir) throws MojoExecutionException { try {//www . jav a 2 s . c o m final TarArchiveInputStream tarIn = new TarArchiveInputStream( new GzipCompressorInputStream(new BufferedInputStream(new FileInputStream(archive)))); try { TarArchiveEntry entry; while ((entry = (TarArchiveEntry) tarIn.getNextEntry()) != null) { LOG.info("Extracting: " + entry.getName()); final File file = new File(destDir, entry.getName()); if (entry.isDirectory()) { createIfNecessary(file); } else { int count; final byte[] data = new byte[MB]; final FileOutputStream fos = new FileOutputStream(file); final BufferedOutputStream dest = new BufferedOutputStream(fos, MB); try { while ((count = tarIn.read(data, 0, MB)) != -1) { dest.write(data, 0, count); } } finally { dest.close(); } entry.getMode(); } applyFileMode(file, new FileMode(entry.getMode())); } } finally { tarIn.close(); } } catch (final IOException ex) { throw new MojoExecutionException("Error uncompressing event store archive: " + archive, ex); } }
From source file:org.fuin.owndeb.commons.DebUtils.java
/** * Reads the first folder found from TAR.GZ file. * //from w ww. ja v a2 s. c om * @param tarGzFile * Archive file to peek the first folder from. * * @return The first directory name or <code>null</code> if the archive only * contains files (no folders). */ @Nullable public static String peekFirstTarGzFolderName(@NotNull final File tarGzFile) { Contract.requireArgNotNull("tarGzFile", tarGzFile); try { final TarArchiveInputStream tarIn = new TarArchiveInputStream( new GzipCompressorInputStream(new BufferedInputStream(new FileInputStream(tarGzFile)))); try { TarArchiveEntry entry; while ((entry = (TarArchiveEntry) tarIn.getNextEntry()) != null) { if (entry.isDirectory()) { return entry.getName(); } } } finally { tarIn.close(); } return null; } catch (final IOException ex) { throw new RuntimeException("Error uncompressing archive: " + tarGzFile, ex); } }
From source file:org.gradle.caching.internal.tasks.CommonsTarPacker.java
@Override public void unpack(DataSource input, DataTargetFactory targetFactory) throws IOException { TarArchiveInputStream tarInput = new TarArchiveInputStream(input.openInput()); while (true) { TarArchiveEntry entry = tarInput.getNextTarEntry(); if (entry == null) { break; }//from ww w. j av a 2 s .c om PackerUtils.unpackEntry(entry.getName(), tarInput, buffer, targetFactory); } tarInput.close(); }
From source file:org.jboss.tools.openshift.reddeer.utils.FileHelper.java
public static void extractTarGz(File archive, File outputDirectory) { InputStream inputStream = null; try {/*from w w w . j a v a2s . c om*/ logger.info("Opening stream to gzip archive"); inputStream = new GzipCompressorInputStream(new FileInputStream(archive)); } catch (IOException ex) { throw new OpenShiftToolsException( "Exception occured while processing tar.gz file.\n" + ex.getMessage()); } logger.info("Opening stream to tar archive"); BufferedOutputStream outputStream = null; TarArchiveInputStream tarArchiveInputStream = new TarArchiveInputStream(inputStream); TarArchiveEntry currentEntry = null; try { while ((currentEntry = tarArchiveInputStream.getNextTarEntry()) != null) { if (currentEntry.isDirectory()) { logger.info("Creating directory: " + currentEntry.getName()); createDirectory(new File(outputDirectory, currentEntry.getName())); } else { File outputFile = new File(outputDirectory, currentEntry.getName()); if (!outputFile.getParentFile().exists()) { logger.info("Creating directory: " + outputFile.getParentFile()); createDirectory(outputFile.getParentFile()); } outputStream = new BufferedOutputStream(new FileOutputStream(outputFile)); logger.info("Extracting file: " + currentEntry.getName()); copy(tarArchiveInputStream, outputStream, (int) currentEntry.getSize()); outputStream.close(); outputFile.setExecutable(true); outputFile.setReadable(true); outputFile.setWritable(true); } } } catch (IOException e) { throw new OpenShiftToolsException("Exception occured while processing tar.gz file.\n" + e.getMessage()); } finally { try { tarArchiveInputStream.close(); } catch (Exception ex) { } try { outputStream.close(); } catch (Exception ex) { } } }
From source file:org.jboss.tools.runtime.core.extract.internal.UntarUtility.java
public IStatus extract(File dest, IOverwrite overwriteQuery, IProgressMonitor monitor) throws CoreException { String possibleRoot = null;/* w w w . j av a 2 s. com*/ try { dest.mkdir(); TarArchiveInputStream tarIn = getTarArchiveInputStream(file); TarArchiveEntry tarEntry = tarIn.getNextTarEntry(); while (tarEntry != null) { String name = tarEntry.getName(); File destPath = new File(dest, name); if (tarEntry.isDirectory()) { destPath.mkdirs(); } else { destPath.createNewFile(); byte[] btoRead = new byte[1024]; try (BufferedOutputStream bout = new BufferedOutputStream(new FileOutputStream(destPath))) { int length = 0; while ((length = tarIn.read(btoRead)) != -1) { bout.write(btoRead, 0, length); } } } // Lets check for a possible root, to avoid scanning the archive again later possibleRoot = checkForPossibleRootEntry(possibleRoot, name); tarEntry = tarIn.getNextTarEntry(); } tarIn.close(); } catch (IOException ioe) { throw new CoreException(new Status(IStatus.ERROR, RuntimeCoreActivator.PLUGIN_ID, 0, NLS.bind("Error extracting runtime {0}", ioe.getLocalizedMessage()), ioe)); //$NON-NLS-1$ } this.discoveredRoot = possibleRoot; return Status.OK_STATUS; }
From source file:org.jclouds.docker.features.internal.ArchivesTest.java
private List<File> unTar(final File inputFile, final File outputDir) throws Exception { final List<File> untarredFiles = Lists.newArrayList(); final InputStream is = new FileInputStream(inputFile); final TarArchiveInputStream tarArchiveInputStream = (TarArchiveInputStream) new ArchiveStreamFactory() .createArchiveInputStream("tar", is); TarArchiveEntry entry;/*from w w w. j a va 2 s . c o m*/ while ((entry = (TarArchiveEntry) tarArchiveInputStream.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 { OutputStream outputFileStream = new FileOutputStream(outputFile); ByteStreams.copy(tarArchiveInputStream, outputFileStream); outputFileStream.close(); } untarredFiles.add(outputFile); } tarArchiveInputStream.close(); return untarredFiles; }
From source file:org.jwifisd.eyefi.EyeFiServer.java
/** * eyefi sends the files tarred w will have to untar it first. * /*from w w w .ja v a 2s . co m*/ * @param tarFile * the tar file * @param out * output stream to write the untarred contents. * @return true if successful * @throws Exception * if the file could not be untarred. */ private boolean extractTarFile(File tarFile, OutputStream out) throws Exception { boolean result = false; byte[] tarBytes = IOUtils.toByteArray(new FileInputStream(tarFile)); // This is stangege but posix was not correctly detected by the apache // lib, so we change some bytes so the detection will work. System.arraycopy(TarConstants.MAGIC_POSIX.getBytes("US-ASCII"), 0, tarBytes, TarConstants.MAGIC_OFFSET, TarConstants.MAGICLEN); TarArchiveInputStream tar = new TarArchiveInputStream(new ByteArrayInputStream(tarBytes)); final byte[] buffer = new byte[BUFFER_SIZE]; try { ArchiveEntry tarEntry = tar.getNextEntry(); if (tarEntry != null) { int n = 0; while ((n = tar.read(buffer)) >= 0) { out.write(buffer, 0, n); } result = true; } } finally { tar.close(); out.close(); } return result; }
From source file:org.killbill.billing.beatrix.integration.osgi.util.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 a 2 s . c om TarArchiveInputStream archiveInputStream = null; TarArchiveEntry entry; 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:org.lobid.lodmill.TarReader.java
@Override public void process(final Reader reader) { TarArchiveInputStream tarInputStream = null; try {// www. j a va 2 s .c o m tarInputStream = new TarArchiveInputStream(new ReaderInputStream(reader)); TarArchiveEntry entry = null; while ((entry = (TarArchiveEntry) tarInputStream.getNextEntry()) != null) { if (!entry.isDirectory()) { byte[] buffer = new byte[(int) entry.getSize()]; while ((tarInputStream.read(buffer)) > 0) { getReceiver().process(new StringReader(new String(buffer))); } } } tarInputStream.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { IOUtils.closeQuietly(tarInputStream); } }