List of usage examples for org.apache.commons.compress.archivers.tar TarArchiveInputStream getNextEntry
public ArchiveEntry getNextEntry() throws IOException
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 www. j av a 2s .c o m 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 {/*from w ww . j a v a2s. 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); } }
From source file:org.mcisb.subliminal.SubliminalUtils.java
/** * /*from w w w. java2 s . c o m*/ * @param url * @param destinationDirectory * @throws IOException */ public static void untar(final URL url, final File destinationDirectory) throws IOException { if (!destinationDirectory.exists()) { if (!destinationDirectory.mkdir()) { throw new IOException(); } } TarArchiveInputStream is = null; try { is = new TarArchiveInputStream(new GZIPInputStream(url.openStream())); ArchiveEntry tarEntry = null; while ((tarEntry = is.getNextEntry()) != null) { final File destination = new File(destinationDirectory, tarEntry.getName()); if (tarEntry.isDirectory()) { if (!destination.mkdir()) { // Take no action. // throw new IOException(); } } else { try (final OutputStream os = new FileOutputStream(destination)) { new StreamReader(is, os).read(); } } } } catch (IOException e) { if (!destinationDirectory.delete()) { throw new IOException(); } throw e; } finally { if (is != null) { is.close(); } } }
From source file:org.mcisb.util.io.TarUtils.java
/** * /*from w w w.j a va 2 s . c om*/ * @param tarInputStream * @param destinationDirectory * @throws IOException */ public static void untar(final InputStream tarInputStream, final File destinationDirectory) throws IOException { if (!destinationDirectory.exists()) { if (!destinationDirectory.mkdir()) { throw new IOException(); } } TarArchiveInputStream is = null; try { is = new TarArchiveInputStream(new GZIPInputStream(tarInputStream)); ArchiveEntry tarEntry = null; while ((tarEntry = is.getNextEntry()) != null) { final File destination = new File(destinationDirectory, tarEntry.getName()); if (tarEntry.isDirectory()) { if (!destination.mkdir()) { // Take no action. // throw new IOException(); } } else { try (final OutputStream os = new FileOutputStream(destination)) { new StreamReader(is, os).read(); } } } } catch (IOException e) { if (!destinationDirectory.delete()) { throw new IOException(); } throw e; } finally { if (is != null) { is.close(); } } }
From source file:org.mitre.xtext.converters.ArchiveNavigator.java
public File untar(File tarFile) throws IOException { String _working = tempDir.getAbsolutePath() + File.separator + FilenameUtils.getBaseName(tarFile.getPath()); File workingDir = new File(_working); workingDir.mkdir();/* w ww . j ava 2 s .co m*/ InputStream input = new BufferedInputStream(new FileInputStream(tarFile)); try { TarArchiveInputStream in = (TarArchiveInputStream) (new ArchiveStreamFactory() .createArchiveInputStream("tar", input)); TarArchiveEntry tarEntry; while ((tarEntry = (TarArchiveEntry) in.getNextEntry()) != null) { if (filterEntry(tarEntry)) { continue; } try { File tmpFile = saveArchiveEntry(tarEntry, in, _working); converter.convert(tmpFile); } catch (IOException err) { log.error("Unable to save item, FILE=" + tarFile.getName() + "!" + tarEntry.getName(), err); } } in.close(); } catch (ArchiveException ae) { throw new IOException(ae); } return workingDir; }
From source file:org.nd4j.util.ArchiveUtils.java
/** * Extracts files to the specified destination * * @param file the file to extract to/*ww w.j a va2s. com*/ * @param dest the destination directory * @throws IOException */ public static void unzipFileTo(String file, String dest) throws IOException { File target = new File(file); if (!target.exists()) throw new IllegalArgumentException("Archive doesnt exist"); FileInputStream fin = new FileInputStream(target); int BUFFER = 2048; byte data[] = new byte[BUFFER]; if (file.endsWith(".zip") || file.endsWith(".jar")) { try (ZipInputStream zis = new ZipInputStream(fin)) { //get the zipped file list entry ZipEntry ze = zis.getNextEntry(); while (ze != null) { String fileName = ze.getName(); File newFile = new File(dest + File.separator + fileName); if (ze.isDirectory()) { newFile.mkdirs(); zis.closeEntry(); ze = zis.getNextEntry(); continue; } FileOutputStream fos = new FileOutputStream(newFile); int len; while ((len = zis.read(data)) > 0) { fos.write(data, 0, len); } fos.close(); ze = zis.getNextEntry(); log.debug("File extracted: " + newFile.getAbsoluteFile()); } zis.closeEntry(); } } else if (file.endsWith(".tar.gz") || file.endsWith(".tgz")) { BufferedInputStream in = new BufferedInputStream(fin); GzipCompressorInputStream gzIn = new GzipCompressorInputStream(in); TarArchiveInputStream tarIn = new TarArchiveInputStream(gzIn); TarArchiveEntry entry; /* Read the tar entries using the getNextEntry method **/ while ((entry = (TarArchiveEntry) tarIn.getNextEntry()) != null) { log.info("Extracting: " + entry.getName()); /* If the entry is a directory, create the directory. */ if (entry.isDirectory()) { File f = new File(dest + File.separator + entry.getName()); f.mkdirs(); } /* * If the entry is a file,write the decompressed file to the disk * and close destination stream. */ else { int count; try (FileOutputStream fos = new FileOutputStream(dest + File.separator + entry.getName()); BufferedOutputStream destStream = new BufferedOutputStream(fos, BUFFER);) { while ((count = tarIn.read(data, 0, BUFFER)) != -1) { destStream.write(data, 0, count); } destStream.flush(); IOUtils.closeQuietly(destStream); } } } // Close the input stream tarIn.close(); } else if (file.endsWith(".gz")) { File extracted = new File(target.getParent(), target.getName().replace(".gz", "")); if (extracted.exists()) extracted.delete(); extracted.createNewFile(); try (GZIPInputStream is2 = new GZIPInputStream(fin); OutputStream fos = FileUtils.openOutputStream(extracted)) { IOUtils.copyLarge(is2, fos); fos.flush(); } } else { throw new IllegalStateException( "Unable to infer file type (compression format) from source file name: " + file); } target.delete(); }
From source file:org.ngrinder.common.util.CompressionUtil.java
/** * Untar an input file into an output file. * //from ww w . j a va 2 s .com * The output file is created in the output folder, having the same name as the input file, * minus the '.tar' extension. * * @param inFile * 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 inFile, final File outputDir) { final List<File> untaredFiles = new LinkedList<File>(); try { final InputStream is = new FileInputStream(inFile); 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 { File parentFile = outputFile.getParentFile(); if (!parentFile.exists()) { parentFile.mkdirs(); } final OutputStream outputFileStream = new FileOutputStream(outputFile); IOUtils.copy(debInputStream, outputFileStream); outputFileStream.close(); if (FilenameUtils.isExtension(outputFile.getName(), EXECUTABLE_EXTENSION)) { outputFile.setExecutable(true, true); } outputFile.setReadable(true); outputFile.setWritable(true, true); } untaredFiles.add(outputFile); } debInputStream.close(); } catch (Exception e) { LOGGER.error("Error while untar {} file by {}", inFile, e.getMessage()); LOGGER.debug("Trace is : ", e); throw new NGrinderRuntimeException("Error while untar file", e); } return untaredFiles; }
From source file:org.ngrinder.common.util.CompressionUtils.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 inFile the input .tar file/*from w ww . j a v a 2 s . c om*/ * @param outputDir the output directory file. * @return The {@link List} of {@link File}s with the untared content. */ @SuppressWarnings("resource") public static List<File> untar(final File inFile, final File outputDir) { final List<File> untaredFiles = new LinkedList<File>(); InputStream is = null; TarArchiveInputStream debInputStream = null; try { is = new FileInputStream(inFile); debInputStream = (TarArchiveInputStream) new ArchiveStreamFactory().createArchiveInputStream("tar", is); TarArchiveEntry entry; 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 { File parentFile = outputFile.getParentFile(); if (!parentFile.exists()) { parentFile.mkdirs(); } final OutputStream outputFileStream = new FileOutputStream(outputFile); try { IOUtils.copy(debInputStream, outputFileStream); } finally { IOUtils.closeQuietly(outputFileStream); } if (FilenameUtils.isExtension(outputFile.getName(), EXECUTABLE_EXTENSION)) { outputFile.setExecutable(true, true); } outputFile.setReadable(true); outputFile.setWritable(true, true); } untaredFiles.add(outputFile); } } catch (Exception e) { throw processException("Error while untar file", e); } finally { IOUtils.closeQuietly(is); IOUtils.closeQuietly(debInputStream); } return untaredFiles; }
From source file:org.onebusaway.util.FileUtility.java
/** * Untar an input file into an output file. * //from ww w .j a va 2 s. 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 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("CHUNouldn'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:org.opensextant.xtext.collectors.ArchiveNavigator.java
public File untar(File tarFile) throws IOException, ConfigException { String _working = FilenameUtils.concat(getWorkingDir(), FilenameUtils.getBaseName(tarFile.getPath())); if (_working == null) { throw new IOException("Invalid archive path for " + tarFile.getPath()); }/* www.java 2s . co m*/ File workingDir = new File(_working); workingDir.mkdir(); InputStream input = new BufferedInputStream(new FileInputStream(tarFile)); TarArchiveInputStream in = null; try { in = (TarArchiveInputStream) (new ArchiveStreamFactory().createArchiveInputStream("tar", input)); TarArchiveEntry tarEntry; while ((tarEntry = (TarArchiveEntry) in.getNextEntry()) != null) { if (filterEntry(tarEntry)) { continue; } try { File tmpFile = saveArchiveEntry(tarEntry, in, _working); converter.convert(tmpFile); } catch (IOException err) { log.error("Unable to save item, FILE=" + tarFile.getName() + "!" + tarEntry.getName(), err); } } } catch (ArchiveException ae) { throw new IOException(ae); } finally { in.close(); } return workingDir; }