List of usage examples for org.apache.commons.compress.archivers.tar TarArchiveEntry isDirectory
public boolean isDirectory()
From source file:com.twitter.heron.downloader.Extractor.java
static void extract(InputStream in, Path destination) throws IOException { try (final BufferedInputStream bufferedInputStream = new BufferedInputStream(in); final GzipCompressorInputStream gzipInputStream = new GzipCompressorInputStream( bufferedInputStream); final TarArchiveInputStream tarInputStream = new TarArchiveInputStream(gzipInputStream)) { final String destinationAbsolutePath = destination.toFile().getAbsolutePath(); TarArchiveEntry entry; while ((entry = (TarArchiveEntry) tarInputStream.getNextEntry()) != null) { if (entry.isDirectory()) { File f = Paths.get(destinationAbsolutePath, entry.getName()).toFile(); f.mkdirs();//w ww. j a va2 s .c o m } else { Path fileDestinationPath = Paths.get(destinationAbsolutePath, entry.getName()); Files.copy(tarInputStream, fileDestinationPath, StandardCopyOption.REPLACE_EXISTING); } } } }
From source file:azkaban.project.DirectoryFlowLoaderTest.java
private static File decompressTarBZ2(InputStream is) throws IOException { File outputDir = Files.createTempDir(); try (TarArchiveInputStream tais = new TarArchiveInputStream(new BZip2CompressorInputStream(is))) { TarArchiveEntry entry; while ((entry = tais.getNextTarEntry()) != null) { if (entry.isDirectory()) { continue; }// w w w .j a v a2 s . com File outputFile = new File(outputDir, entry.getName()); File parent = outputFile.getParentFile(); if (!parent.exists()) { parent.mkdirs(); } try (FileOutputStream os = new FileOutputStream(outputFile)) { IOUtils.copy(tais, os); } } return outputDir; } }
From source file:examples.utils.CifarReader.java
public static void downloadAndExtract() { if (new File("data", TEST_DATA_FILE).exists() == false) { try {/*from w w w.ja va 2s. co m*/ if (new File("data", ARCHIVE_BINARY_FILE).exists() == false) { URL website = new URL("http://www.cs.toronto.edu/~kriz/" + ARCHIVE_BINARY_FILE); FileOutputStream fos = new FileOutputStream("data/" + ARCHIVE_BINARY_FILE); fos.getChannel().transferFrom(Channels.newChannel(website.openStream()), 0, Long.MAX_VALUE); fos.close(); } TarArchiveInputStream tar = new TarArchiveInputStream( new GZIPInputStream(new FileInputStream("data/" + ARCHIVE_BINARY_FILE))); TarArchiveEntry entry = null; while ((entry = tar.getNextTarEntry()) != null) { if (entry.isDirectory()) { new File("data", entry.getName()).mkdirs(); } else { byte data[] = new byte[2048]; int count; BufferedOutputStream bos = new BufferedOutputStream( new FileOutputStream(new File("data/", entry.getName())), 2048); while ((count = tar.read(data, 0, 2048)) != -1) { bos.write(data, 0, count); } bos.close(); } } tar.close(); } catch (IOException e) { e.printStackTrace(); } } }
From source file:ezbake.deployer.utilities.VersionHelper.java
public static String getVersionFromArtifact(ByteBuffer artifact) throws IOException { String versionNumber = null;//from w ww . jav a 2 s .c o m try (CompressorInputStream uncompressedInput = new GzipCompressorInputStream( new ByteArrayInputStream(artifact.array()))) { ArchiveInputStream input = new TarArchiveInputStream(uncompressedInput); TarArchiveEntry entry; while ((entry = (TarArchiveEntry) input.getNextEntry()) != null) { if (!entry.isDirectory() && entry.getName().endsWith(VERSION_FILE)) { versionNumber = IOUtils.toString(input, Charsets.UTF_8).trim(); break; } } } return versionNumber; }
From source file:com.github.pemapmodder.pocketminegui.utils.Utils.java
private static File installWindowsPHP(File home, InstallProgressReporter progress) { progress.report(0.0);/*from w ww. ja va 2 s. co m*/ try { String link = System.getProperty("os.arch").contains("64") ? "https://bintray.com/artifact/download/pocketmine/PocketMine/PHP_7.0.3_x64_Windows.tar.gz" : "https://bintray.com/artifact/download/pocketmine/PocketMine/PHP_7.0.3_x86_Windows.tar.gz"; URL url = new URL(link); InputStream gz = url.openStream(); GzipCompressorInputStream tar = new GzipCompressorInputStream(gz); TarArchiveInputStream is = new TarArchiveInputStream(tar); TarArchiveEntry entry; while ((entry = is.getNextTarEntry()) != null) { if (!entry.isDirectory()) { String name = entry.getName(); byte[] buffer = new byte[(int) entry.getSize()]; IOUtils.read(is, buffer); File real = new File(home, name); real.getParentFile().mkdirs(); IOUtils.write(buffer, new FileOutputStream(real)); } } File output = new File(home, "bin/php/php.exe"); progress.completed(output); return output; } catch (IOException e) { e.printStackTrace(); progress.errored(); return null; } }
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; 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(); }//from w ww . ja v a 2 s . co m 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:co.cask.cdap.internal.app.runtime.LocalizationUtils.java
private static void extractTar(final TarArchiveInputStream tis, File targetDir) throws IOException { TarArchiveEntry entry = tis.getNextTarEntry(); while (entry != null) { File output = new File(targetDir, new File(entry.getName()).getName()); if (entry.isDirectory()) { //noinspection ResultOfMethodCallIgnored output.mkdirs();//from w ww . j a v a 2 s .c o m } else { //noinspection ResultOfMethodCallIgnored output.getParentFile().mkdirs(); ByteStreams.copy(tis, Files.newOutputStreamSupplier(output)); } entry = tis.getNextTarEntry(); } }
From source file:com.bahmanm.karun.Utils.java
/** * Extracts a gzip'ed tar archive.//w w w . j a va 2s .c om * * @param archivePath Path to archive * @param destDir Destination directory * @throws IOException */ public synchronized static void extractTarGz(String archivePath, File destDir) throws IOException, ArchiveException { // copy File tarGzFile = File.createTempFile("karuntargz", "", destDir); copyFile(archivePath, tarGzFile.getAbsolutePath()); // decompress File tarFile = File.createTempFile("karuntar", "", destDir); FileInputStream fin = new FileInputStream(tarGzFile); BufferedInputStream bin = new BufferedInputStream(fin); FileOutputStream fout = new FileOutputStream(tarFile); GzipCompressorInputStream gzIn = new GzipCompressorInputStream(bin); final byte[] buffer = new byte[1024]; int n = 0; while (-1 != (n = gzIn.read(buffer))) { fout.write(buffer, 0, n); } bin.close(); fin.close(); gzIn.close(); fout.close(); // extract final InputStream is = new FileInputStream(tarFile); ArchiveInputStream ain = new ArchiveStreamFactory().createArchiveInputStream("tar", is); TarArchiveEntry entry = null; while ((entry = (TarArchiveEntry) ain.getNextEntry()) != null) { OutputStream out; if (entry.isDirectory()) { File f = new File(destDir, entry.getName()); f.mkdirs(); continue; } else out = new FileOutputStream(new File(destDir, entry.getName())); IOUtils.copy(ain, out); out.close(); } ain.close(); is.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//from w w w. j ava2 s . c om * @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:deodex.tools.TarGzUtils.java
/** Untar an input file into an output file. /*from w w w . j a va 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; }