Java Code Examples for org.apache.commons.compress.archivers.tar.TarArchiveEntry#isDirectory()
The following examples show how to use
org.apache.commons.compress.archivers.tar.TarArchiveEntry#isDirectory() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DatabaseMigrationAcceptanceTest.java From besu with Apache License 2.0 | 6 votes |
private static void extract(final Path path, final String destDirectory) throws IOException { try (TarArchiveInputStream fin = new TarArchiveInputStream( new GzipCompressorInputStream(new FileInputStream(path.toAbsolutePath().toString())))) { TarArchiveEntry entry; while ((entry = fin.getNextTarEntry()) != null) { if (entry.isDirectory()) { continue; } final File curfile = new File(destDirectory, entry.getName()); final File parent = curfile.getParentFile(); if (!parent.exists()) { parent.mkdirs(); } IOUtils.copy(fin, new FileOutputStream(curfile)); } } }
Example 2
Source File: TarUtils.java From zeppelin with Apache License 2.0 | 6 votes |
public static void decompress(String in, File out) throws IOException { FileInputStream fileInputStream = new FileInputStream(in); GzipCompressorInputStream gzipInputStream = new GzipCompressorInputStream(fileInputStream); try (TarArchiveInputStream fin = new TarArchiveInputStream(gzipInputStream)){ TarArchiveEntry entry; while ((entry = fin.getNextTarEntry()) != null) { if (entry.isDirectory()) { continue; } File curfile = new File(out, entry.getName()); File parent = curfile.getParentFile(); if (!parent.exists()) { parent.mkdirs(); } IOUtils.copy(fin, new FileOutputStream(curfile)); } } }
Example 3
Source File: DL4JSentimentAnalysisExample.java From Java-for-Data-Science with MIT License | 6 votes |
private static void extractTar(String dataIn, String dataOut) throws IOException { try (TarArchiveInputStream inStream = new TarArchiveInputStream( new GzipCompressorInputStream(new BufferedInputStream(new FileInputStream(dataIn))))) { TarArchiveEntry tarFile; while ((tarFile = (TarArchiveEntry) inStream.getNextEntry()) != null) { if (tarFile.isDirectory()) { new File(dataOut + tarFile.getName()).mkdirs(); } else { int count; byte data[] = new byte[BUFFER_SIZE]; FileOutputStream fileInStream = new FileOutputStream(dataOut + tarFile.getName()); BufferedOutputStream outStream= new BufferedOutputStream(fileInStream, BUFFER_SIZE); while ((count = inStream.read(data, 0, BUFFER_SIZE)) != -1) { outStream.write(data, 0, count); } } } } }
Example 4
Source File: HeliumBundleFactory.java From zeppelin with Apache License 2.0 | 6 votes |
private static List<String> unTgz(File tarFile, File directory) throws IOException { List<String> result = new ArrayList<>(); try (TarArchiveInputStream in = new TarArchiveInputStream( new GzipCompressorInputStream(new FileInputStream(tarFile)))) { TarArchiveEntry entry = in.getNextTarEntry(); while (entry != null) { if (entry.isDirectory()) { entry = in.getNextTarEntry(); continue; } File curfile = new File(directory, entry.getName()); File parent = curfile.getParentFile(); if (!parent.exists()) { parent.mkdirs(); } try (OutputStream out = new FileOutputStream(curfile)) { IOUtils.copy(in, out); } result.add(entry.getName()); entry = in.getNextTarEntry(); } } return result; }
Example 5
Source File: GeoLite2Geolocator.java From Plan with GNU Lesser General Public License v3.0 | 6 votes |
private void findAndCopyFromTar(TarArchiveInputStream tarIn, FileOutputStream fos) throws IOException { // Breadth first search Queue<TarArchiveEntry> entries = new ArrayDeque<>(); entries.add(tarIn.getNextTarEntry()); while (!entries.isEmpty()) { TarArchiveEntry entry = entries.poll(); if (entry.isDirectory()) { entries.addAll(Arrays.asList(entry.getDirectoryEntries())); } // Looking for this file if (entry.getName().endsWith("GeoLite2-Country.mmdb")) { IOUtils.copy(tarIn, fos); break; // Found it } TarArchiveEntry next = tarIn.getNextTarEntry(); if (next != null) entries.add(next); } }
Example 6
Source File: TarLeveledStructureProvider.java From tracecompass with Eclipse Public License 2.0 | 6 votes |
/** * Initializes this object's children table based on the contents of the * specified source file. */ protected void initialize() { children = new HashMap<>(1000); children.put(root, new ArrayList<>()); Enumeration<TarArchiveEntry> entries = tarFile.entries(); while (entries.hasMoreElements()) { TarArchiveEntry entry = entries.nextElement(); IPath path = new Path(entry.getName()).addTrailingSeparator(); if (entry.isDirectory()) { createContainer(path); } else { // Ensure the container structure for all levels above this is initialized // Once we hit a higher-level container that's already added we need go no further int pathSegmentCount = path.segmentCount(); if (pathSegmentCount > 1) { createContainer(path.uptoSegment(pathSegmentCount - 1)); } createFile(entry); } } }
Example 7
Source File: TarUtils.java From Xndroid with GNU General Public License v3.0 | 6 votes |
/** * 文件 解归档 * * @param destFile * 目标文件 * @param tais * ZipInputStream * @throws Exception */ private static void dearchive(File destFile, TarArchiveInputStream tais) throws Exception { TarArchiveEntry entry = null; while ((entry = tais.getNextTarEntry()) != null) { // 文件 String dir = destFile.getPath() + File.separator + entry.getName(); File dirFile = new File(dir); // 文件检查 fileProber(dirFile); if (entry.isDirectory()) { dirFile.mkdirs(); } else { dearchiveFile(dirFile, tais); } } }
Example 8
Source File: ExtractUtils.java From MSPaintIDE with MIT License | 6 votes |
public static void untar(TarArchiveInputStream tarIn, File targetDir) throws IOException { byte[] b = new byte[4096]; TarArchiveEntry tarEntry; while ((tarEntry = tarIn.getNextTarEntry()) != null) { final File file = new File(targetDir, tarEntry.getName()); if (tarEntry.isDirectory()) { if (!file.mkdirs()) { throw new IOException("Unable to create folder " + file.getAbsolutePath()); } } else { final File parent = file.getParentFile(); if (!parent.exists()) { if (!parent.mkdirs()) { throw new IOException("Unable to create folder " + parent.getAbsolutePath()); } } try (FileOutputStream fos = new FileOutputStream(file)) { int r; while ((r = tarIn.read(b)) != -1) { fos.write(b, 0, r); } } } } }
Example 9
Source File: DefaultArchiveExtractor.java From flow with Apache License 2.0 | 5 votes |
/** * Copy TarArchiveEntry file contents to target path. * Set file to executable if marked so in the entry. * * @param tarIn * tar archive input stream * @param tarEntry * tar archive entry * @param destinationFile * destination * @throws IOException * thrown if copying fails */ private void copyTarFileContents(TarArchiveInputStream tarIn, TarArchiveEntry tarEntry, File destinationFile) throws IOException { if (tarEntry.isDirectory()) { return; } destinationFile.createNewFile(); boolean isExecutable = (tarEntry.getMode() & 0100) > 0; destinationFile.setExecutable(isExecutable); try (FileOutputStream out = new FileOutputStream(destinationFile)) { IOUtils.copy(tarIn, out); } }
Example 10
Source File: FileUtil.java From big-c with Apache License 2.0 | 5 votes |
private static void unpackEntries(TarArchiveInputStream tis, TarArchiveEntry entry, File outputDir) throws IOException { if (entry.isDirectory()) { File subDir = new File(outputDir, entry.getName()); if (!subDir.mkdirs() && !subDir.isDirectory()) { throw new IOException("Mkdirs failed to create tar internal dir " + outputDir); } for (TarArchiveEntry e : entry.getDirectoryEntries()) { unpackEntries(tis, e, subDir); } return; } File outputFile = new File(outputDir, entry.getName()); if (!outputFile.getParentFile().exists()) { if (!outputFile.getParentFile().mkdirs()) { throw new IOException("Mkdirs failed to create tar internal dir " + outputDir); } } int count; byte data[] = new byte[2048]; BufferedOutputStream outputStream = new BufferedOutputStream( new FileOutputStream(outputFile)); while ((count = tis.read(data)) != -1) { outputStream.write(data, 0, count); } outputStream.flush(); outputStream.close(); }
Example 11
Source File: RNNodeService.java From react-native-node with MIT License | 5 votes |
private static List<File> unTar(final File inputFile, final File outputDir) throws FileNotFoundException, IOException, ArchiveException { Log.i(TAG, String.format("Untaring %s to dir %s", inputFile.getAbsolutePath(), outputDir.getAbsolutePath())); if (!outputDir.exists()) { outputDir.mkdirs(); } 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()) { 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; }
Example 12
Source File: TarUtils.java From che with Eclipse Public License 2.0 | 5 votes |
/** @apiNote Caller should close `in` after calling this method. */ public static void untar(InputStream in, File targetDir) throws IOException { final TarArchiveInputStream tarIn = new TarArchiveInputStream(in); byte[] b = new byte[BUF_SIZE]; TarArchiveEntry tarEntry; while ((tarEntry = tarIn.getNextTarEntry()) != null) { final File file = new File(targetDir, tarEntry.getName()); if (tarEntry.isDirectory()) { if (!file.mkdirs()) { throw new IOException("Unable to create folder " + file.getAbsolutePath()); } } else { final File parent = file.getParentFile(); if (!parent.exists()) { if (!parent.mkdirs()) { throw new IOException("Unable to create folder " + parent.getAbsolutePath()); } } try (FileOutputStream fos = new FileOutputStream(file)) { int r; while ((r = tarIn.read(b)) != -1) { fos.write(b, 0, r); } } } } }
Example 13
Source File: TarFileObject.java From commons-vfs with Apache License 2.0 | 5 votes |
/** * Sets the details for this file object. * * Consider this method package private. TODO Might be made package private in the next major version. * * @param entry Tar archive entry. */ protected void setTarEntry(final TarArchiveEntry entry) { if (this.entry != null) { return; } if (entry == null || entry.isDirectory()) { type = FileType.FOLDER; } else { type = FileType.FILE; } this.entry = entry; }
Example 14
Source File: IOUtils.java From senti-storm with Apache License 2.0 | 5 votes |
public static void extractTarGz(InputStream inputTarGzStream, String outDir, boolean logging) { try { 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()); } }
Example 15
Source File: FileUtils.java From netbeans with Apache License 2.0 | 5 votes |
@CheckForNull public static String decompressTarGz(File archive, File destination, boolean skipArchiveRoot) throws IOException { String archiveRoot = null; try (TarArchiveInputStream tarInputStream = new TarArchiveInputStream(new GZIPInputStream(new FileInputStream(archive)))) { int archiveRootLength = -1; TarArchiveEntry tarEntry = tarInputStream.getNextTarEntry(); if (tarEntry != null) { archiveRoot = tarEntry.getName(); if (skipArchiveRoot) { archiveRootLength = archiveRoot.length(); tarEntry = tarInputStream.getNextTarEntry(); } } while (tarEntry != null) { String name = tarEntry.getName(); if (skipArchiveRoot) { name = name.substring(archiveRootLength); } File destPath = new File(destination, name); if (tarEntry.isDirectory()) { if (!destPath.isDirectory() && !destPath.mkdirs()) { throw new IOException("Cannot create directory " + destPath); } } else { if (!destPath.isFile() && !destPath.createNewFile()) { throw new IOException("Cannot create new file " + destPath); } try (BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(destPath))) { FileUtil.copy(tarInputStream, outputStream); } } tarEntry = tarInputStream.getNextTarEntry(); } } return archiveRoot; }
Example 16
Source File: OmnisharpStreamConnectionProvider.java From aCute with Eclipse Public License 2.0 | 5 votes |
/** * * @return path to server, unzipping it if necessary. Can be null is fragment is missing. */ private @Nullable File getServer() throws IOException { File serverPath = new File(AcutePlugin.getDefault().getStateLocation().toFile(), "omnisharp-roslyn"); //$NON-NLS-1$ if (!serverPath.exists()) { serverPath.mkdirs(); try ( InputStream stream = FileLocator.openStream(AcutePlugin.getDefault().getBundle(), new Path("omnisharp-roslyn.tar"), true); //$NON-NLS-1$ TarArchiveInputStream tarStream = new TarArchiveInputStream(stream); ) { TarArchiveEntry entry = null; while ((entry = tarStream.getNextTarEntry()) != null) { if (!entry.isDirectory()) { File targetFile = new File(serverPath, entry.getName()); targetFile.getParentFile().mkdirs(); InputStream in = new BoundedInputStream(tarStream, entry.getSize()); // mustn't be closed try ( FileOutputStream out = new FileOutputStream(targetFile); ) { IOUtils.copy(in, out); if (!Platform.OS_WIN32.equals(Platform.getOS())) { int xDigit = entry.getMode() % 10; targetFile.setExecutable(xDigit > 0, (xDigit & 1) == 1); int wDigit = (entry.getMode() / 10) % 10; targetFile.setWritable(wDigit > 0, (wDigit & 1) == 1); int rDigit = (entry.getMode() / 100) % 10; targetFile.setReadable(rDigit > 0, (rDigit & 1) == 1); } } } } } } return serverPath; }
Example 17
Source File: CompressionFileUtil.java From Jpom with MIT License | 5 votes |
private static List<String> unTar(InputStream inputStream, File destDir) throws Exception { List<String> fileNames = new ArrayList<>(); TarArchiveInputStream tarIn = new TarArchiveInputStream(inputStream, BUFFER_SIZE, CharsetUtil.GBK); TarArchiveEntry entry; try { while ((entry = tarIn.getNextTarEntry()) != null) { fileNames.add(entry.getName()); if (entry.isDirectory()) { //是目录 FileUtil.mkdir(new File(destDir, entry.getName())); //创建空目录 } else { //是文件 File tmpFile = new File(destDir, entry.getName()); //创建输出目录 FileUtil.mkParentDirs(destDir); OutputStream out = null; try { out = new FileOutputStream(tmpFile); int length; byte[] b = new byte[2048]; while ((length = tarIn.read(b)) != -1) { out.write(b, 0, length); } } finally { IOUtils.closeQuietly(out); } } } } finally { IOUtils.closeQuietly(tarIn); } return fileNames; }
Example 18
Source File: TarGzs.java From tomee with Apache License 2.0 | 4 votes |
public static void untargz(final InputStream read, final File destination, final boolean noparent, final FileFilter fileFilter) throws IOException { Objects.requireNonNull(fileFilter, "'fileFilter' is required."); Files.dir(destination); Files.writable(destination); try { GzipCompressorInputStream gzipIn = new GzipCompressorInputStream(read); TarArchiveInputStream tarIn = new TarArchiveInputStream(gzipIn); TarArchiveEntry entry; while ((entry = (TarArchiveEntry) tarIn.getNextEntry()) != null) { String path = entry.getName(); if (noparent) { path = path.replaceFirst("^[^/]+/", ""); } File file = new File(destination, path); if (!fileFilter.accept(file)) continue; if (entry.isDirectory()) { Files.mkdir(file); } else { Files.mkdir(file.getParentFile()); IO.copy(tarIn, file); long lastModified = entry.getLastModifiedDate().getTime(); if (lastModified > 0L) { file.setLastModified(lastModified); } // if (entry.getMode() != 420) System.out.printf("%s %s%n", entry.getMode(), entry.getName()); // DMB: I have no idea how to respect the mod. // Elasticsearch tar has entries with 33261 that are executable if (33261 == entry.getMode()) { file.setExecutable(true); } // DMB: I have no idea how to respect the mod. // Kibana tar has entries with 493 that are executable if (493 == entry.getMode()) { file.setExecutable(true); } } } tarIn.close(); } catch (IOException var9) { throw new IOException("Unable to unzip " + read, var9); } }
Example 19
Source File: ProjectArchives.java From digdag with Apache License 2.0 | 4 votes |
private static void extractArchive(Path destDir, TarArchiveInputStream archive, ExtractListener listener) throws IOException { String prefix = destDir.toString(); TarArchiveEntry entry; while (true) { entry = archive.getNextTarEntry(); if (entry == null) { break; } Path path = destDir.resolve(entry.getName()).normalize(); if (!path.toString().startsWith(prefix)) { throw new RuntimeException("Archive includes an invalid entry: " + entry.getName()); } if (entry.isDirectory()) { Files.createDirectories(path); } else if (entry.isSymbolicLink()) { Files.createDirectories(path.getParent()); String dest = entry.getLinkName(); Path destAbsPath = path.getParent().resolve(dest).normalize(); if (!destAbsPath.normalize().toString().startsWith(prefix)) { throw new RuntimeException("Archive includes an invalid symlink: " + entry.getName() + " -> " + dest); } if (listener != null) { listener.symlink(destDir.relativize(path), dest); } Files.createSymbolicLink(path, Paths.get(dest)); } else { Files.createDirectories(path.getParent()); if (listener != null) { listener.file(destDir.relativize(path)); } try (OutputStream out = Files.newOutputStream(path)) { ByteStreams.copy(archive, out); } } if (!Files.isSymbolicLink(path) && isPosixCompliant()) { // Files.setPosixFilePermissions doesn't work on Windows: java.lang.UnsupportedOperationException Files.setPosixFilePermissions(path, getPosixFilePermissions(entry)); } } }
Example 20
Source File: TarGzExtractorProvider.java From appengine-plugins-core with Apache License 2.0 | 4 votes |
@Override public void extract(Path archive, Path destination, ProgressListener progressListener) throws IOException { progressListener.start( "Extracting archive: " + archive.getFileName(), ProgressListener.UNKNOWN); String canonicalDestination = destination.toFile().getCanonicalPath(); GzipCompressorInputStream gzipIn = new GzipCompressorInputStream(Files.newInputStream(archive)); try (TarArchiveInputStream in = new TarArchiveInputStream(gzipIn)) { TarArchiveEntry entry; while ((entry = in.getNextTarEntry()) != null) { Path entryTarget = destination.resolve(entry.getName()); String canonicalTarget = entryTarget.toFile().getCanonicalPath(); if (!canonicalTarget.startsWith(canonicalDestination + File.separator)) { throw new IOException("Blocked unzipping files outside destination: " + entry.getName()); } progressListener.update(1); logger.fine(entryTarget.toString()); if (entry.isDirectory()) { if (!Files.exists(entryTarget)) { Files.createDirectories(entryTarget); } } else if (entry.isFile()) { if (!Files.exists(entryTarget.getParent())) { Files.createDirectories(entryTarget.getParent()); } try (OutputStream out = new BufferedOutputStream(Files.newOutputStream(entryTarget))) { IOUtils.copy(in, out); PosixFileAttributeView attributeView = Files.getFileAttributeView(entryTarget, PosixFileAttributeView.class); if (attributeView != null) { attributeView.setPermissions(PosixUtil.getPosixFilePermissions(entry.getMode())); } } } else { // we don't know what kind of entry this is (we only process directories and files). logger.warning("Skipping entry (unknown type): " + entry.getName()); } } progressListener.done(); } }