List of usage examples for org.apache.commons.compress.archivers.tar TarArchiveEntry getName
public String getName()
From source file:com.android.tradefed.util.FileUtil.java
public static void extractTarGzip(File tarGzipFile, File destDir) throws FileNotFoundException, IOException, ArchiveException { GZIPInputStream gzipIn = null; ArchiveInputStream archivIn = null;/*from ww w . j a va2 s. c o m*/ BufferedInputStream buffIn = null; BufferedOutputStream buffOut = null; try { gzipIn = new GZIPInputStream(new BufferedInputStream(new FileInputStream(tarGzipFile))); archivIn = new ArchiveStreamFactory().createArchiveInputStream("tar", gzipIn); buffIn = new BufferedInputStream(archivIn); TarArchiveEntry entry = null; while ((entry = (TarArchiveEntry) archivIn.getNextEntry()) != null) { String entryName = entry.getName(); String[] engtryPart = entryName.split("/"); StringBuilder fullPath = new StringBuilder(); fullPath.append(destDir.getAbsolutePath()); for (String e : engtryPart) { fullPath.append(File.separator); fullPath.append(e); } File destFile = new File(fullPath.toString()); if (entryName.endsWith("/")) { if (!destFile.exists()) destFile.mkdirs(); } else { if (!destFile.exists()) destFile.createNewFile(); buffOut = new BufferedOutputStream(new FileOutputStream(destFile)); byte[] buf = new byte[8192]; int len = 0; while ((len = buffIn.read(buf)) != -1) { buffOut.write(buf, 0, len); } buffOut.flush(); } } } finally { if (buffOut != null) { buffOut.close(); } if (buffIn != null) { buffIn.close(); } if (archivIn != null) { archivIn.close(); } if (gzipIn != null) { gzipIn.close(); } } }
From source file:com.spotify.docker.client.CompressedDirectoryTest.java
@Test public void testFile() throws Exception { // note: Paths.get(someURL.toUri()) is the platform-neutral way to convert a URL to a Path final URL dockerDirectory = Resources.getResource("dockerDirectory"); try (CompressedDirectory dir = CompressedDirectory.create(Paths.get(dockerDirectory.toURI())); BufferedInputStream fileIn = new BufferedInputStream(Files.newInputStream(dir.file())); GzipCompressorInputStream gzipIn = new GzipCompressorInputStream(fileIn); TarArchiveInputStream tarIn = new TarArchiveInputStream(gzipIn)) { final List<String> names = new ArrayList<>(); TarArchiveEntry entry; while ((entry = tarIn.getNextTarEntry()) != null) { final String name = entry.getName(); names.add(name);// w w w. j av a2 s . co m } assertThat(names, containsInAnyOrder("Dockerfile", "bin/date.sh", "innerDir/innerDockerfile")); } }
From source file:com.spotify.docker.client.CompressedDirectoryTest.java
@Test public void testFileWithIgnore() throws Exception { // note: Paths.get(someURL.toUri()) is the platform-neutral way to convert a URL to a Path final URL dockerDirectory = Resources.getResource("dockerDirectoryWithIgnore"); try (CompressedDirectory dir = CompressedDirectory.create(Paths.get(dockerDirectory.toURI())); BufferedInputStream fileIn = new BufferedInputStream(Files.newInputStream(dir.file())); GzipCompressorInputStream gzipIn = new GzipCompressorInputStream(fileIn); TarArchiveInputStream tarIn = new TarArchiveInputStream(gzipIn)) { final List<String> names = new ArrayList<>(); TarArchiveEntry entry; while ((entry = tarIn.getNextTarEntry()) != null) { final String name = entry.getName(); names.add(name);// ww w.ja v a 2 s. c o m } assertThat(names, containsInAnyOrder("Dockerfile", "bin/date.sh", "subdir2/keep-me", ".dockerignore")); } }
From source file:de.flapdoodle.embedmongo.extract.TgzExtractor.java
@Override public void extract(RuntimeConfig runtime, File source, File destination, Pattern file) throws IOException { IProgressListener progressListener = runtime.getProgressListener(); String progressLabel = "Extract " + source; progressListener.start(progressLabel); FileInputStream fin = new FileInputStream(source); BufferedInputStream in = new BufferedInputStream(fin); GzipCompressorInputStream gzIn = new GzipCompressorInputStream(in); TarArchiveInputStream tarIn = new TarArchiveInputStream(gzIn); try {//from w w w. ja v a 2s. com TarArchiveEntry entry; while ((entry = tarIn.getNextTarEntry()) != null) { if (file.matcher(entry.getName()).matches()) { // System.out.println("File: " + entry.getName()); if (tarIn.canReadEntryData(entry)) { // System.out.println("Can Read: " + entry.getName()); long size = entry.getSize(); Files.write(tarIn, size, destination); destination.setExecutable(true); // System.out.println("DONE"); progressListener.done(progressLabel); } break; } else { // System.out.println("SKIP File: " + entry.getName()); } } } finally { tarIn.close(); gzIn.close(); } }
From source file:cc.twittertools.corpus.data.TarJsonStatusCorpusReader.java
/** * Returns the next status, or <code>null</code> if no more statuses. */// w w w .j a v a 2s .c om public Status next() throws IOException { if (currentBlock == null) { // Move to next file. TarArchiveEntry entry = tarInput.getNextTarEntry(); if (entry != null) { if (entry.getName().endsWith(".bz2")) { currentBlock = new Bz2JsonStatusBlockReader(tarInput); } } else { return null; } } Status status = null; while (true) { if (currentBlock != null) { status = currentBlock.next(); } if (status != null) { return status; } // Move to next file. try { TarArchiveEntry entry = tarInput.getNextTarEntry(); if (entry != null) { if (entry.getName().endsWith(".bz2")) { currentBlock = new Bz2JsonStatusBlockReader(tarInput); } } else { return null; } } catch (IOException e) { currentBlock = null; } } }
From source file:com.amaze.filemanager.asynchronous.asynctasks.compress.TarHelperTask.java
@Override void addElements(ArrayList<CompressedObjectParcelable> elements) { TarArchiveInputStream tarInputStream = null; try {/*from www .j av a 2s .c o m*/ tarInputStream = new TarArchiveInputStream(new FileInputStream(filePath)); TarArchiveEntry entry; while ((entry = tarInputStream.getNextTarEntry()) != null) { String name = entry.getName(); if (!CompressedHelper.isEntryPathValid(name)) { continue; } if (name.endsWith(SEPARATOR)) name = name.substring(0, name.length() - 1); boolean isInBaseDir = relativePath.equals("") && !name.contains(SEPARATOR); boolean isInRelativeDir = name.contains(SEPARATOR) && name.substring(0, name.lastIndexOf(SEPARATOR)).equals(relativePath); if (isInBaseDir || isInRelativeDir) { elements.add(new CompressedObjectParcelable(entry.getName(), entry.getLastModifiedDate().getTime(), entry.getSize(), entry.isDirectory())); } } } catch (IOException e) { e.printStackTrace(); } }
From source file:gdt.data.entity.ArchiveHandler.java
private static boolean hasPropertyIndexInTarStream(TarArchiveInputStream tis) { try {/*from ww w . j ava 2 s .c o m*/ TarArchiveEntry entry = null; String entryName$; while ((entry = tis.getNextTarEntry()) != null) { entryName$ = entry.getName(); if (entryName$.equals(Entigrator.PROPERTY_INDEX)) { tis.close(); return true; } } tis.close(); return false; } catch (Exception e) { Logger.getLogger(ArchiveHandler.class.getName()).severe(e.toString()); return false; } }
From source file:gdt.data.entity.ArchiveHandler.java
private static boolean hasEntitiesDirInTarStream(TarArchiveInputStream tis) { try {/*from w w w. j a v a2s. c om*/ // System.out.println("ArchiveHandler:hasEntitiesDirInTarStream:BEGIN:tis="+tis.getCount()); TarArchiveEntry entry = null; String entryName$; while ((entry = tis.getNextTarEntry()) != null) { entryName$ = entry.getName(); System.out.println("ArchiveHandler:hasEntitiesDirInTarStream:entry=" + entryName$); if (entryName$.startsWith(Entigrator.ENTITY_BASE)) { tis.close(); return true; } } tis.close(); return false; } catch (Exception e) { Logger.getLogger(ArchiveHandler.class.getName()).severe(e.toString()); return false; } }
From source file:com.amaze.filemanager.asynchronous.asynctasks.compress.GzipHelperTask.java
@Override void addElements(ArrayList<CompressedObjectParcelable> elements) { TarArchiveInputStream tarInputStream = null; try {/*w w w.j av a 2s . c o m*/ tarInputStream = new TarArchiveInputStream( new GzipCompressorInputStream(new FileInputStream(filePath))); TarArchiveEntry entry; while ((entry = tarInputStream.getNextTarEntry()) != null) { String name = entry.getName(); if (!CompressedHelper.isEntryPathValid(name)) { AppConfig.toast(context.get(), context.get().getString(R.string.multiple_invalid_archive_entries)); continue; } if (name.endsWith(SEPARATOR)) name = name.substring(0, name.length() - 1); boolean isInBaseDir = relativePath.equals("") && !name.contains(SEPARATOR); boolean isInRelativeDir = name.contains(SEPARATOR) && name.substring(0, name.lastIndexOf(SEPARATOR)).equals(relativePath); if (isInBaseDir || isInRelativeDir) { elements.add(new CompressedObjectParcelable(entry.getName(), entry.getLastModifiedDate().getTime(), entry.getSize(), entry.isDirectory())); } } } catch (IOException e) { e.printStackTrace(); } }
From source file:indexing.WTDocIndexer.java
void indexTarArchive() throws Exception { GZIPInputStream gzipInputStream = new GZIPInputStream(new FileInputStream(new File(tarArchivePath))); TarArchiveInputStream tarArchiveInputStream = new TarArchiveInputStream(gzipInputStream); TarArchiveEntry tarEntry = tarArchiveInputStream.getNextTarEntry(); while (tarEntry != null) { if (!tarEntry.isDirectory()) { System.out.println("Extracting " + tarEntry.getName()); final OutputStream outputFileStream = new FileOutputStream(tarEntry.getName()); } else {/* ww w.j a v a2 s . c o m*/ tarEntry = tarArchiveInputStream.getNextTarEntry(); } } }