List of usage examples for org.apache.commons.compress.archivers.tar TarArchiveInputStream getNextEntry
public ArchiveEntry getNextEntry() throws IOException
From source file:com.openshift.client.utils.TarFileTestUtils.java
/** * Returns all paths within the given archive. * /* w w w . j av a2 s . c o m*/ * @param in * the archive * @return all paths * @throws IOException * @throws CompressorException */ public static List<String> getAllPaths(InputStream in) throws IOException { Assert.notNull(in); List<String> paths = new ArrayList<String>(); TarArchiveInputStream archiveIn = new TarArchiveInputStream(new GZIPInputStream(in)); try { for (ArchiveEntry nextEntry = null; (nextEntry = archiveIn.getNextEntry()) != null;) { paths.add(nextEntry.getName()); } return paths; } finally { StreamUtils.close(archiveIn); } }
From source file:ie.pars.bnc.preprocess.MainBNCProcess.java
private static void getZippedFile() throws IOException, ArchiveException, Exception { String taggerPath = "edu/stanford/nlp/models/pos-tagger/english-left3words/english-left3words-distsim.tagger"; String parseModel = LexicalizedParser.DEFAULT_PARSER_LOC; InputStream is = new FileInputStream(pathInput); TarArchiveInputStream tarStream = (TarArchiveInputStream) new ArchiveStreamFactory() .createArchiveInputStream("tar", is); TarArchiveEntry entry = null;/* w w w . j a va 2s. c o m*/ int countfiles = 0; while ((entry = (TarArchiveEntry) tarStream.getNextEntry()) != null) { // for(File lf: listFiles){ if (!entry.isDirectory()) { byte[] content = new byte[(int) entry.getSize()]; int offset = 0; tarStream.read(content, offset, content.length - offset); String id = entry.getName().split("/")[entry.getName().split("/").length - 1].split(".xml")[0]; if (!filesProcesed.contains(id) && id.startsWith(letter.toUpperCase())) { if (countfiles++ % 10 == 0) { tagger = new MaxentTagger(taggerPath); m = new Morphology(); parser = ParserGrammar.loadModel(parseModel); parser.loadTagger(); } System.out.print("Entry " + entry.getName()); InputStream bis = new ByteArrayInputStream(content); StringBuilder parseBNCXML = ProcessNLP.parseBNCXML(bis, m, tagger, parser); bis.close(); OutputStream out = new FileOutputStream(pathOutput + File.separatorChar + id + ".vert"); Writer writer = new OutputStreamWriter(out, "UTF-8"); writer.write("<text id=\"" + id + "\">\n"); writer.write(parseBNCXML.toString()); writer.write("</text>\n"); writer.close(); out.close(); } else { System.out.println(">> Bypass Entry " + entry.getName()); } //break; } } is.close(); System.out.println("There are " + countfiles); // tarStream.close(); }
From source file:com.streamsets.datacollector.util.UntarUtility.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 w w.j a va 2 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 */ private 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.goldmansachs.kata2go.tools.utils.TarGz.java
public static void decompressFromStream2(InputStream inputStream) throws IOException { GzipCompressorInputStream gzipStream = new GzipCompressorInputStream(inputStream); TarArchiveInputStream tarInput = new TarArchiveInputStream(gzipStream); TarArchiveEntry entry;/* w w w . j a v a 2 s. co m*/ int bufferSize = 1024; while ((entry = (TarArchiveEntry) tarInput.getNextEntry()) != null) { String entryName = entry.getName(); // strip out the leading directory like the --strip tar argument String entryNameWithoutLeadingDir = entryName.substring(entryName.indexOf("/") + 1); if (entryNameWithoutLeadingDir.isEmpty()) { continue; } if (entry.isDirectory()) { System.out.println("found dir " + entry.getName()); } else { int count; byte data[] = new byte[bufferSize]; ByteArrayOutputStream baos = new ByteArrayOutputStream(); while ((count = tarInput.read(data, 0, bufferSize)) != -1) { baos.write(data, 0, count); } JarOutputStream jarOutputStream = new JarOutputStream(baos); System.out.println(new String(baos.toByteArray())); } } tarInput.close(); gzipStream.close(); }
From source file:com.goldmansachs.kata2go.tools.utils.TarGz.java
public static void decompress(InputStream tarGzInputStream, Path outDir) throws IOException { GzipCompressorInputStream gzipStream = new GzipCompressorInputStream(tarGzInputStream); TarArchiveInputStream tarInput = new TarArchiveInputStream(gzipStream); TarArchiveEntry entry;/*from w ww.j ava2 s. co m*/ int bufferSize = 1024; while ((entry = (TarArchiveEntry) tarInput.getNextEntry()) != null) { String entryName = entry.getName(); // strip out the leading directory like the --strip tar argument String entryNameWithoutLeadingDir = entryName.substring(entryName.indexOf("/") + 1); if (entryNameWithoutLeadingDir.isEmpty()) { continue; } Path outFile = outDir.resolve(entryNameWithoutLeadingDir); if (entry.isDirectory()) { outFile.toFile().mkdirs(); continue; } int count; byte data[] = new byte[bufferSize]; BufferedOutputStream fios = new BufferedOutputStream(new FileOutputStream(outFile.toFile()), bufferSize); while ((count = tarInput.read(data, 0, bufferSize)) != -1) { fios.write(data, 0, count); } fios.close(); } tarInput.close(); gzipStream.close(); }
From source file:com.audiveris.installer.Expander.java
/** * Untar an input file into an output directory. * * @param inFile the input .tar file//from ww w.j av a 2 s . co m * @param outDir the output directory * @throws IOException * @throws FileNotFoundException * @throws ArchiveException * * @return The list of files with the untared content. */ public static List<File> unTar(final File inFile, final File outDir) throws FileNotFoundException, IOException, ArchiveException { logger.debug("Untaring {} to dir {}", inFile, outDir); assert inFile.getName().endsWith(TAR_EXT); final List<File> untaredFiles = new ArrayList<File>(); final InputStream is = new FileInputStream(inFile); final TarArchiveInputStream tis = (TarArchiveInputStream) new ArchiveStreamFactory() .createArchiveInputStream("tar", is); ArchiveEntry entry; while ((entry = tis.getNextEntry()) != null) { final File outFile = new File(outDir, entry.getName()); if (entry.isDirectory()) { logger.debug("Attempting to write output dir {}", outFile); if (!outFile.exists()) { logger.debug("Attempting to create output dir {}", outFile); if (!outFile.mkdirs()) { throw new IllegalStateException( String.format("Couldn't create directory %s", outFile.getAbsolutePath())); } } } else { logger.debug("Creating output file {}", outFile); streamToFile(tis, outFile); } untaredFiles.add(outFile); } tis.close(); return untaredFiles; }
From source file:deodex.tools.TarGzUtils.java
/** Untar an input file into an output file. //w ww . jav a 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 { 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()) { Logger.appendLog("Attempting to write output directory . " + outputFile.getAbsolutePath()); if (!outputFile.exists()) { Logger.appendLog("Attempting to create output directory ." + outputFile.getAbsolutePath()); if (!outputFile.mkdirs()) { throw new IllegalStateException( String.format("Couldn't create directory %s.", outputFile.getAbsolutePath())); } } } else { outputFile.getParentFile().mkdirs(); final OutputStream outputFileStream = new FileOutputStream(outputFile); IOUtils.copy(debInputStream, outputFileStream); outputFileStream.close(); } untaredFiles.add(outputFile); } debInputStream.close(); return untaredFiles; }
From source file:com.google.cloud.public_datasets.nexrad2.GcsUntar.java
private static File[] unTar(final File inputFile, final File outputDir) throws FileNotFoundException, IOException, ArchiveException { // from/*from ww w.ja v a 2 s . c o m*/ // http://stackoverflow.com/questions/315618/how-do-i-extract-a-tar-file-in-java/7556307#7556307 log.info("tar xf " + inputFile + " to " + outputDir); final List<File> untaredFiles = new ArrayList<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()) { 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); outputFileStream.close(); } untaredFiles.add(outputFile); } debInputStream.close(); return untaredFiles.toArray(new File[0]); }
From source file:at.illecker.sentistorm.commons.util.io.IOUtils.java
public static void extractTarGz(InputStream inputTarGzStream, String outDir, boolean logging) { try {/* w w w . j a v a 2 s . co m*/ GzipCompressorInputStream gzIn = new GzipCompressorInputStream(inputTarGzStream); TarArchiveInputStream tarIn = new TarArchiveInputStream(gzIn); // read Tar entries TarArchiveEntry entry = null; while ((entry = (TarArchiveEntry) tarIn.getNextEntry()) != null) { if (logging) { LOG.info("Extracting: " + outDir + File.separator + entry.getName()); } if (entry.isDirectory()) { // create directory File f = new File(outDir + File.separator + entry.getName()); f.mkdirs(); } else { // decompress file int count; byte data[] = new byte[EXTRACT_BUFFER_SIZE]; FileOutputStream fos = new FileOutputStream(outDir + File.separator + entry.getName()); BufferedOutputStream dest = new BufferedOutputStream(fos, EXTRACT_BUFFER_SIZE); while ((count = tarIn.read(data, 0, EXTRACT_BUFFER_SIZE)) != -1) { dest.write(data, 0, count); } dest.close(); } } // close input stream tarIn.close(); } catch (IOException e) { LOG.error("IOException: " + e.getMessage()); } }
From source file:com.openshift.client.utils.TarFileTestUtils.java
/** * Replaces the given file(-name), that might exist anywhere nested in the * given archive, by a new entry with the given content. The replacement is * faked by adding a new entry into the archive which will overwrite the * existing (older one) on extraction./*from w ww . j a v a 2 s . c o m*/ * * @param name * the name of the file to replace (no path required) * @param newContent * the content of the replacement file * @param in * @return * @throws IOException * @throws ArchiveException * @throws CompressorException */ public static File fakeReplaceFile(String name, String newContent, InputStream in) throws IOException { Assert.notNull(name); Assert.notNull(in); File newArchive = FileUtils.createRandomTempFile(".tar.gz"); newArchive.deleteOnExit(); TarArchiveOutputStream newArchiveOut = new TarArchiveOutputStream( new GZIPOutputStream(new FileOutputStream(newArchive))); newArchiveOut.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU); TarArchiveInputStream archiveIn = new TarArchiveInputStream(new GZIPInputStream(in)); String pathToReplace = null; try { // copy the existing entries for (ArchiveEntry nextEntry = null; (nextEntry = archiveIn.getNextEntry()) != null;) { if (nextEntry.getName().endsWith(name)) { pathToReplace = nextEntry.getName(); } newArchiveOut.putArchiveEntry(nextEntry); IOUtils.copy(archiveIn, newArchiveOut); newArchiveOut.closeArchiveEntry(); } if (pathToReplace == null) { throw new IllegalStateException("Could not find file " + name + " in the given archive."); } TarArchiveEntry newEntry = new TarArchiveEntry(pathToReplace); newEntry.setSize(newContent.length()); newArchiveOut.putArchiveEntry(newEntry); IOUtils.copy(new ByteArrayInputStream(newContent.getBytes()), newArchiveOut); newArchiveOut.closeArchiveEntry(); return newArchive; } finally { newArchiveOut.finish(); newArchiveOut.flush(); StreamUtils.close(archiveIn); StreamUtils.close(newArchiveOut); } }