List of usage examples for org.apache.commons.compress.archivers ArchiveStreamFactory ArchiveStreamFactory
ArchiveStreamFactory
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 ww .j a va2 s . 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.pinot.common.utils.TarGzCompressionUtils.java
public static InputStream unTarOneFile(InputStream tarGzInputStream, final String filename) throws FileNotFoundException, IOException, ArchiveException { TarArchiveInputStream debInputStream = null; InputStream is = null;//from ww w. j a v a 2 s . c o m try { is = new GzipCompressorInputStream(tarGzInputStream); debInputStream = (TarArchiveInputStream) new ArchiveStreamFactory().createArchiveInputStream("tar", is); TarArchiveEntry entry = null; while ((entry = (TarArchiveEntry) debInputStream.getNextEntry()) != null) { if (entry.getName().contains(filename)) { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); IOUtils.copy(debInputStream, byteArrayOutputStream); return new ByteArrayInputStream(byteArrayOutputStream.toByteArray()); } } } finally { IOUtils.closeQuietly(debInputStream); IOUtils.closeQuietly(is); } return null; }
From source file:com.qwazr.tools.ArchiverTool.java
public void extract(File sourceFile, File destDir) throws IOException, ArchiveException { final InputStream is = new BufferedInputStream(new FileInputStream(sourceFile)); try {/*from ww w . j av a2 s.c om*/ ArchiveInputStream in = new ArchiveStreamFactory().createArchiveInputStream(is); try { ArchiveEntry entry; while ((entry = in.getNextEntry()) != null) { if (!in.canReadEntryData(entry)) continue; if (entry.isDirectory()) { new File(destDir, entry.getName()).mkdir(); continue; } if (entry instanceof ZipArchiveEntry) if (((ZipArchiveEntry) entry).isUnixSymlink()) continue; File destFile = new File(destDir, entry.getName()); File parentDir = destFile.getParentFile(); if (!parentDir.exists()) parentDir.mkdirs(); IOUtils.copy(in, destFile); } } catch (IOException e) { throw new IOException("Unable to extract the archive: " + sourceFile.getPath(), e); } finally { IOUtils.closeQuietly(in); } } catch (ArchiveException e) { throw new ArchiveException("Unable to extract the archive: " + sourceFile.getPath(), e); } finally { IOUtils.closeQuietly(is); } }
From source file:com.hortonworks.amstore.view.AmbariStoreHelper.java
/** Untar an input file into an output file. //from ww w .j a v a 2 s . co 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:com.silverpeas.util.ZipManager.java
/** * Extract the content of an archive into a directory. * * @param source the archive./*from www . j av a2 s. co m*/ * @param dest the destination directory. * @throws UtilException */ /*public static void extract(File source, File dest) throws UtilException { if (source == null) { throw new UtilException("Expand.execute()", SilverpeasException.ERROR, "util.EXE_SOURCE_FILE_ATTRIBUTE_MUST_BE_SPECIFIED"); } if (dest == null) { throw new UtilException("Expand.execute()", SilverpeasException.ERROR, "util.EXE_DESTINATION_FILE_ATTRIBUTE_MUST_BE_SPECIFIED"); } ZipFile zf = null; try { zf = new ZipFile(source); @SuppressWarnings("unchecked") Enumeration<ZipArchiveEntry> entries = (Enumeration<ZipArchiveEntry>) zf.getEntries(); while (entries.hasMoreElements()) { ZipArchiveEntry ze = entries.nextElement(); File currentFile = new File(dest, ze.getName()); try { currentFile.getParentFile().mkdirs(); if (ze.isDirectory()) { currentFile.mkdirs(); } else { currentFile.getParentFile().mkdirs(); InputStream zis = zf.getInputStream(ze); FileOutputStream fos = new FileOutputStream(currentFile); IOUtils.copy(zis, fos); IOUtils.closeQuietly(zis); IOUtils.closeQuietly(fos); } } catch (FileNotFoundException ex) { SilverTrace.warn("util", "ZipManager.extractFile()", "root.EX_FILE_NOT_FOUND", "file = " + currentFile.getPath(), ex); } } } catch (IOException ioe) { SilverTrace.warn("util", "ZipManager.extractFile()", "util.EXE_ERROR_WHILE_EXTRACTING_FILE", "sourceFile = " + source.getPath(), ioe); } finally { if (zf != null) { ZipFile.closeQuietly(zf); } } } */ public static void extract(File source, File dest) throws UtilException { if (source == null) { throw new UtilException("Expand.execute()", SilverpeasException.ERROR, "util.EXE_SOURCE_FILE_ATTRIBUTE_MUST_BE_SPECIFIED"); } if (dest == null) { throw new UtilException("Expand.execute()", SilverpeasException.ERROR, "util.EXE_DESTINATION_FILE_ATTRIBUTE_MUST_BE_SPECIFIED"); } InputStream in = null; ArchiveInputStream archiveStream = null; try { in = new BufferedInputStream(new FileInputStream(source)); try { archiveStream = new ArchiveStreamFactory().createArchiveInputStream(in); } catch (ArchiveException aex) { if (FilenameUtils.getExtension(source.getName()).toLowerCase().endsWith("gz")) { archiveStream = new TarArchiveInputStream(new GzipCompressorInputStream(in)); } else { archiveStream = new TarArchiveInputStream(new BZip2CompressorInputStream(in)); } } ArchiveEntry archiveEntry; while ((archiveEntry = archiveStream.getNextEntry()) != null) { File currentFile = new File(dest, archiveEntry.getName()); try { currentFile.getParentFile().mkdirs(); if (archiveEntry.isDirectory()) { currentFile.mkdirs(); } else { currentFile.getParentFile().mkdirs(); FileOutputStream fos = new FileOutputStream(currentFile); IOUtils.copy(archiveStream, fos); IOUtils.closeQuietly(fos); } } catch (FileNotFoundException ex) { SilverTrace.warn("util", "ZipManager.extractFile()", "root.EX_FILE_NOT_FOUND", "file = " + currentFile.getPath(), ex); } } } catch (IOException ioe) { SilverTrace.warn("util", "ZipManager.extractFile()", "util.EXE_ERROR_WHILE_EXTRACTING_FILE", "sourceFile = " + source.getPath(), ioe); } finally { IOUtils.closeQuietly(in); IOUtils.closeQuietly(archiveStream); } }
From source file:com.geewhiz.pacify.test.TestUtil.java
private static void archiveDoesNotContainAdditionEntries(File replacedArchive, File expectedArchive) throws ArchiveException, IOException { ArchiveStreamFactory factory = new ArchiveStreamFactory(); FileInputStream replacedIS = new FileInputStream(replacedArchive); ArchiveInputStream replacedAIS = factory.createArchiveInputStream(new BufferedInputStream(replacedIS)); ArchiveEntry replacedEntry = null;//ww w. ja v a 2s . c o m while ((replacedEntry = replacedAIS.getNextEntry()) != null) { FileInputStream expectedIS = new FileInputStream(expectedArchive); ArchiveInputStream expectedAIS = factory.createArchiveInputStream(new BufferedInputStream(expectedIS)); ArchiveEntry expectedEntry = null; boolean entryFound = false; while ((expectedEntry = expectedAIS.getNextEntry()) != null) { Assert.assertNotNull("We expect an entry.", expectedEntry); if (!replacedEntry.getName().equals(expectedEntry.getName())) { continue; } entryFound = true; if (ArchiveUtils.isArchiveAndIsSupported(expectedEntry.getName())) { Assert.assertTrue("we expect a archive", ArchiveUtils.isArchiveAndIsSupported(replacedEntry.getName())); File replacedChildArchive = ArchiveUtils.extractFile(replacedArchive, ArchiveUtils.getArchiveType(replacedArchive), replacedEntry.getName()); File expectedChildArchive = ArchiveUtils.extractFile(expectedArchive, ArchiveUtils.getArchiveType(expectedArchive), expectedEntry.getName()); archiveDoesNotContainAdditionEntries(replacedChildArchive, expectedChildArchive); replacedChildArchive.delete(); expectedChildArchive.delete(); } break; } expectedIS.close(); Assert.assertTrue("Entry [" + replacedEntry.getName() + "] is not in the expected archive. This file shouldn't exist.", entryFound); } replacedIS.close(); }
From source file:com.qwazr.library.archiver.ArchiverTool.java
public void extract(final Path sourceFile, final Path destDir) throws IOException, ArchiveException { try (final InputStream is = new BufferedInputStream(Files.newInputStream(sourceFile))) { try (final ArchiveInputStream in = new ArchiveStreamFactory().createArchiveInputStream(is)) { ArchiveEntry entry;/*from w w w . j a va2s . c o m*/ while ((entry = in.getNextEntry()) != null) { if (!in.canReadEntryData(entry)) continue; if (entry.isDirectory()) { final Path newDir = destDir.resolve(entry.getName()); if (!Files.exists(newDir)) Files.createDirectory(newDir); continue; } if (entry instanceof ZipArchiveEntry) if (((ZipArchiveEntry) entry).isUnixSymlink()) continue; final Path destFile = destDir.resolve(entry.getName()); final Path parentDir = destFile.getParent(); if (!Files.exists(parentDir)) Files.createDirectories(parentDir); final long entryLastModified = entry.getLastModifiedDate().getTime(); if (Files.exists(destFile) && Files.isRegularFile(destFile) && Files.getLastModifiedTime(destFile).toMillis() == entryLastModified && entry.getSize() == Files.size(destFile)) continue; IOUtils.copy(in, destFile); Files.setLastModifiedTime(destFile, FileTime.fromMillis(entryLastModified)); } } catch (IOException e) { throw new IOException("Unable to extract the archive: " + sourceFile.toAbsolutePath(), e); } } catch (ArchiveException e) { throw new ArchiveException("Unable to extract the archive: " + sourceFile.toAbsolutePath(), e); } }
From source file:es.uvigo.ei.sing.gc.view.ZKUtils.java
public static Data loadDataFromMedia(Media media) throws ArchiveException, IOException, AbortException, Exception { final Reader reader; if (media.isBinary()) { // It seems that, in Windows, big text files are uploaded as binary. final String fileName = media.getName().toLowerCase(); if (fileName.endsWith(".csv") || fileName.endsWith(".txt")) { reader = new InputStreamReader(media.getStreamData()); } else {// w w w .jav a 2 s. c o m InputStream is; try { final CompressorStreamFactory factory = new CompressorStreamFactory(); is = factory.createCompressorInputStream(new BufferedInputStream(media.getStreamData())); } catch (CompressorException ce) { final ArchiveStreamFactory factory = new ArchiveStreamFactory(); is = factory.createArchiveInputStream(new BufferedInputStream(media.getStreamData())); if (((ArchiveInputStream) is).getNextEntry().isDirectory()) throw new IOException("Invalid archive file format"); } reader = new InputStreamReader(is); } } else { reader = media.getReaderData(); } return LoadClassificationData.loadData(reader, media.getName(), null, null, true, null); }
From source file:com.geewhiz.pacify.utils.ArchiveUtils.java
private static Map<String, File> extractFiles(File archive, String archiveType, String searchFor, Boolean isRegExp) {//from w w w. j a va 2 s . c om Map<String, File> result = new HashMap<String, File>(); ArchiveInputStream ais = null; try { ArchiveStreamFactory factory = new ArchiveStreamFactory(); ais = factory.createArchiveInputStream(archiveType, new FileInputStream(archive)); ArchiveEntry entry; while ((entry = ais.getNextEntry()) != null) { if (isRegExp) { if (!matches(entry.getName(), searchFor)) { continue; } } else if (!searchFor.equals(entry.getName())) { continue; } File physicalFile = FileUtils.createEmptyFileWithSamePermissions(archive, archive.getName() + "!" + Paths.get(entry.getName()).getFileName().toString() + "_"); BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(physicalFile)); byte[] content = new byte[2048]; int len; while ((len = ais.read(content)) != -1) { bos.write(content, 0, len); } bos.close(); content = null; result.put(entry.getName(), physicalFile); } } catch (ArchiveException e) { throw new RuntimeException(e); } catch (IOException e) { throw new RuntimeException(e); } finally { IOUtils.closeQuietly(ais); } return result; }
From source file:de.uka.ilkd.key.dl.gui.initialdialog.gui.ToolInstaller.java
/** * @param tmp//w w w. j av a 2s . 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(); }