List of usage examples for org.apache.commons.compress.archivers ArchiveStreamFactory ArchiveStreamFactory
ArchiveStreamFactory
From source file:fur.shadowdrake.minecraft.InstallPanel.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 is the input .tar stream.//from w w w.j av a2 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 */ @SuppressWarnings("ConvertToTryWithResources") private ArrayList<String> unTar(final InputStream is, final File outputDir) throws FileNotFoundException, IOException, ArchiveException { final ArrayList<String> untaredFiles = new ArrayList<>(); final TarArchiveInputStream archiveStream = (TarArchiveInputStream) new ArchiveStreamFactory() .createArchiveInputStream("tar", is); TarArchiveEntry entry; int bytesRead = 0; /*int x = 0;*/ byte[] buf = new byte[16384]; /*log.println("|---+----+----+----+----+----+----+----+----+----|"); log.print("/");*/ while ((entry = (TarArchiveEntry) archiveStream.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 FileOutputStream outputFileStream = new FileOutputStream(outputFile); /*int incr = Math.floorDiv(downloadSize, 50);*/ for (int n = archiveStream.read(buf); n > 0; n = archiveStream.read(buf)) { outputFileStream.write(buf, 0, n); log.advance(n); /*bytesRead += n; x++;*/ /*if (bytesRead >= incr) { log.backspace(); log.print("#"); switch (Math.floorDiv(x, 100)) { case 0: log.print("/"); break; case 1: log.print("-"); break; case 2: log.print("\\"); break; case 3: log.print("|"); break; } bytesRead -= incr; } if (x % 100 == 0) { log.backspace(); switch (Math.floorDiv(x, 100)) { case 0: case 4: log.print("/"); x = 0; break; case 1: log.print("-"); break; case 2: log.print("\\"); break; case 3: log.print("|"); break; } }*/ } outputFileStream.close(); } untaredFiles.add(entry.getName()); } archiveStream.close(); /*log.backspace(); log.println("#");*/ return untaredFiles; }
From source file:net.wouterdanes.docker.provider.RemoteApiBasedDockerProvider.java
private static byte[] getTgzArchiveForFiles(final ImageBuildConfiguration image) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); try (CompressorOutputStream gzipStream = new CompressorStreamFactory().createCompressorOutputStream("gz", baos);//w w w. ja v a2 s . c o m ArchiveOutputStream tar = new ArchiveStreamFactory().createArchiveOutputStream("tar", gzipStream)) { for (File file : image.getFiles()) { ArchiveEntry entry = tar.createArchiveEntry(file, file.getName()); tar.putArchiveEntry(entry); byte[] contents = Files.readAllBytes(Paths.get(file.getAbsolutePath())); tar.write(contents); tar.closeArchiveEntry(); } tar.flush(); gzipStream.flush(); } catch (CompressorException | ArchiveException | IOException e) { throw new IllegalStateException("Unable to create output archive", e); } return baos.toByteArray(); }
From source file:org.apache.camel.dataformat.tarfile.TarFileDataFormat.java
@Override public Object unmarshal(Exchange exchange, InputStream stream) throws Exception { if (usingIterator) { return new TarIterator(exchange.getIn(), stream); } else {/*w w w . j a v a2s .co m*/ InputStream is = exchange.getIn().getMandatoryBody(InputStream.class); TarArchiveInputStream tis = (TarArchiveInputStream) new ArchiveStreamFactory() .createArchiveInputStream(ArchiveStreamFactory.TAR, new BufferedInputStream(is)); ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { TarArchiveEntry entry = tis.getNextTarEntry(); if (entry != null) { exchange.getOut().setHeader(FILE_NAME, entry.getName()); IOHelper.copy(tis, baos); } entry = tis.getNextTarEntry(); if (entry != null) { throw new IllegalStateException("Tar file has more than 1 entry."); } return baos.toByteArray(); } finally { IOHelper.close(tis, baos); } } }
From source file:org.apache.camel.dataformat.tarfile.TarIterator.java
public TarIterator(Message inputMessage, InputStream inputStream) { this.inputMessage = inputMessage; //InputStream inputStream = inputMessage.getBody(InputStream.class); if (inputStream instanceof TarArchiveInputStream) { tarInputStream = (TarArchiveInputStream) inputStream; } else {/*from ww w .j av a 2 s .co m*/ try { ArchiveInputStream input = new ArchiveStreamFactory() .createArchiveInputStream(ArchiveStreamFactory.TAR, new BufferedInputStream(inputStream)); tarInputStream = (TarArchiveInputStream) input; } catch (ArchiveException e) { throw new RuntimeException(e.getMessage(), e); } } parent = null; }
From source file:org.apache.camel.processor.aggregate.tarfile.AggregationStrategyWithFilenameHeaderTest.java
@Test public void testSplitter() throws Exception { MockEndpoint mock = getMockEndpoint("mock:aggregateToTarEntry"); mock.expectedMessageCount(1);//from w ww. j a va 2 s. com template.setDefaultEndpointUri("direct:start"); template.sendBodyAndHeader("foo", Exchange.FILE_NAME, FILE_NAMES.get(0)); template.sendBodyAndHeader("bar", Exchange.FILE_NAME, FILE_NAMES.get(1)); assertMockEndpointsSatisfied(); Thread.sleep(500); File[] files = new File("target/out").listFiles(); assertTrue(files != null); assertTrue("Should be a file in target/out directory", files.length > 0); File resultFile = files[0]; final TarArchiveInputStream tis = (TarArchiveInputStream) new ArchiveStreamFactory() .createArchiveInputStream(ArchiveStreamFactory.TAR, new BufferedInputStream(new FileInputStream(resultFile))); try { int fileCount = 0; for (TarArchiveEntry entry = tis.getNextTarEntry(); entry != null; entry = tis.getNextTarEntry()) { fileCount++; assertTrue("Tar entry file name should be on of: " + FILE_NAMES, FILE_NAMES.contains(entry.getName())); } assertEquals("Tar file should contain " + FILE_NAMES.size() + " files", FILE_NAMES.size(), fileCount); } finally { IOHelper.close(tis); } }
From source file:org.apache.camel.processor.aggregate.tarfile.TarAggregationStrategy.java
private static void addFileToTar(File source, File file, String fileName) throws IOException, ArchiveException { File tmpTar = File.createTempFile(source.getName(), null); tmpTar.delete();//ww w. ja v a 2 s .c o m if (!source.renameTo(tmpTar)) { throw new IOException("Could not make temp file (" + source.getName() + ")"); } TarArchiveInputStream tin = (TarArchiveInputStream) new ArchiveStreamFactory() .createArchiveInputStream(ArchiveStreamFactory.TAR, new FileInputStream(tmpTar)); TarArchiveOutputStream tos = new TarArchiveOutputStream(new FileOutputStream(source)); tos.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX); tos.setBigNumberMode(TarArchiveOutputStream.BIGNUMBER_POSIX); InputStream in = new FileInputStream(file); // copy the existing entries ArchiveEntry nextEntry; while ((nextEntry = tin.getNextEntry()) != null) { tos.putArchiveEntry(nextEntry); IOUtils.copy(tin, tos); tos.closeArchiveEntry(); } // Add the new entry TarArchiveEntry entry = new TarArchiveEntry(fileName == null ? file.getName() : fileName); entry.setSize(file.length()); tos.putArchiveEntry(entry); IOUtils.copy(in, tos); tos.closeArchiveEntry(); IOHelper.close(in); IOHelper.close(tin); IOHelper.close(tos); }
From source file:org.apache.camel.processor.aggregate.tarfile.TarAggregationStrategy.java
private static void addEntryToTar(File source, String entryName, byte[] buffer, int length) throws IOException, ArchiveException { File tmpTar = File.createTempFile(source.getName(), null); tmpTar.delete();//from ww w . j a v a 2s . co m if (!source.renameTo(tmpTar)) { throw new IOException("Cannot create temp file: " + source.getName()); } TarArchiveInputStream tin = (TarArchiveInputStream) new ArchiveStreamFactory() .createArchiveInputStream(ArchiveStreamFactory.TAR, new FileInputStream(tmpTar)); TarArchiveOutputStream tos = new TarArchiveOutputStream(new FileOutputStream(source)); tos.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX); tos.setBigNumberMode(TarArchiveOutputStream.BIGNUMBER_POSIX); // copy the existing entries ArchiveEntry nextEntry; while ((nextEntry = tin.getNextEntry()) != null) { tos.putArchiveEntry(nextEntry); IOUtils.copy(tin, tos); tos.closeArchiveEntry(); } // Create new entry TarArchiveEntry entry = new TarArchiveEntry(entryName); entry.setSize(length); tos.putArchiveEntry(entry); tos.write(buffer, 0, length); tos.closeArchiveEntry(); IOHelper.close(tin); IOHelper.close(tos); }
From source file:org.apache.flex.utilities.converter.retrievers.BaseRetriever.java
protected void unpack(File inputArchive, File targetDirectory) throws RetrieverException { if (!targetDirectory.mkdirs()) { throw new RetrieverException( "Unable to create extraction directory " + targetDirectory.getAbsolutePath()); }/*from w w w.j ava 2s . c om*/ ArchiveInputStream archiveInputStream = null; ArchiveEntry entry; try { final CountingInputStream inputStream = new CountingInputStream(new FileInputStream(inputArchive)); final long inputFileSize = inputArchive.length(); if (inputArchive.getName().endsWith(".tbz2")) { archiveInputStream = new TarArchiveInputStream(new BZip2CompressorInputStream(inputStream)); } else { archiveInputStream = new ArchiveStreamFactory() .createArchiveInputStream(new BufferedInputStream(inputStream)); } final ProgressBar progressBar = new ProgressBar(inputFileSize); while ((entry = archiveInputStream.getNextEntry()) != null) { final File outputFile = new File(targetDirectory, entry.getName()); // Entry is a directory. if (entry.isDirectory()) { if (!outputFile.exists()) { if (!outputFile.mkdirs()) { throw new RetrieverException( "Could not create output directory " + outputFile.getAbsolutePath()); } } } // Entry is a file. else { final byte[] data = new byte[BUFFER_MAX]; final FileOutputStream fos = new FileOutputStream(outputFile); BufferedOutputStream dest = null; try { dest = new BufferedOutputStream(fos, BUFFER_MAX); int count; while ((count = archiveInputStream.read(data, 0, BUFFER_MAX)) != -1) { dest.write(data, 0, count); progressBar.updateProgress(inputStream.getBytesRead()); } } finally { if (dest != null) { dest.flush(); dest.close(); } } } progressBar.updateProgress(inputStream.getBytesRead()); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (ArchiveException e) { e.printStackTrace(); } finally { if (archiveInputStream != null) { try { archiveInputStream.close(); } catch (Exception e) { // Ignore... } } } }
From source file:org.apache.gobblin.service.modules.orchestration.AzkabanJobHelper.java
@edu.umd.cs.findbugs.annotations.SuppressWarnings(value = "OBL_UNSATISFIED_OBLIGATION", justification = "Lombok construct of @Cleanup is handing this, but not detected by FindBugs") private static void addFilesToZip(File zipFile, List<File> filesToAdd) throws IOException { try {/*from w w w. j ava 2s. c o m*/ @Cleanup OutputStream archiveStream = new FileOutputStream(zipFile); @Cleanup ArchiveOutputStream archive = new ArchiveStreamFactory() .createArchiveOutputStream(ArchiveStreamFactory.ZIP, archiveStream); for (File fileToAdd : filesToAdd) { ZipArchiveEntry entry = new ZipArchiveEntry(fileToAdd.getName()); archive.putArchiveEntry(entry); @Cleanup BufferedInputStream input = new BufferedInputStream(new FileInputStream(fileToAdd)); IOUtils.copy(input, archive); archive.closeArchiveEntry(); } archive.finish(); } catch (ArchiveException e) { throw new IOException("Issue with creating archive", e); } }
From source file:org.apache.hadoop.hive.common.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./* w ww .j ava 2 s . c o m*/ * * @param inputFileName the input .tar file * @param outputDirName 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 String inputFileName, final String outputDirName, boolean flatten) throws FileNotFoundException, IOException, ArchiveException { File inputFile = new File(inputFileName); File outputDir = new File(outputDirName); final List<File> untaredFiles = new LinkedList<File>(); final InputStream is; if (inputFileName.endsWith(".gz")) { is = new GzipCompressorInputStream(new FileInputStream(inputFile)); } else { 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()) { if (flatten) { // no sub-directories continue; } 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 { final OutputStream outputFileStream; if (flatten) { File flatOutputFile = new File(outputDir, outputFile.getName()); LOG.debug(String.format("Creating flat output file %s.", flatOutputFile.getAbsolutePath())); outputFileStream = new FileOutputStream(flatOutputFile); } else if (!outputFile.getParentFile().exists()) { LOG.debug(String.format("Attempting to create output directory %s.", outputFile.getParentFile().getAbsoluteFile())); if (!outputFile.getParentFile().getAbsoluteFile().mkdirs()) { throw new IllegalStateException(String.format("Couldn't create directory %s.", outputFile.getParentFile().getAbsolutePath())); } LOG.debug(String.format("Creating output file %s.", outputFile.getAbsolutePath())); outputFileStream = new FileOutputStream(outputFile); } else { outputFileStream = new FileOutputStream(outputFile); } IOUtils.copy(debInputStream, outputFileStream); outputFileStream.close(); } untaredFiles.add(outputFile); } debInputStream.close(); return untaredFiles; }