Java Code Examples for org.apache.commons.compress.archivers.ArchiveEntry#isDirectory()
The following examples show how to use
org.apache.commons.compress.archivers.ArchiveEntry#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: ExtractionTools.java From aws-codepipeline-plugin-for-jenkins with Apache License 2.0 | 6 votes |
private static void extractArchive(final File destination, final ArchiveInputStream archiveInputStream) throws IOException { final int BUFFER_SIZE = 8192; ArchiveEntry entry = archiveInputStream.getNextEntry(); while (entry != null) { final File destinationFile = getDestinationFile(destination, entry.getName()); if (entry.isDirectory()) { destinationFile.mkdir(); } else { destinationFile.getParentFile().mkdirs(); try (final OutputStream fileOutputStream = new FileOutputStream(destinationFile)) { final byte[] buffer = new byte[BUFFER_SIZE]; int bytesRead; while ((bytesRead = archiveInputStream.read(buffer)) != -1) { fileOutputStream.write(buffer, 0, bytesRead); } } } entry = archiveInputStream.getNextEntry(); } }
Example 2
Source File: Tar.java From writelatex-git-bridge with MIT License | 6 votes |
public static void untar( InputStream tar, File parentDir ) throws IOException { TarArchiveInputStream tin = new TarArchiveInputStream(tar); ArchiveEntry e; while ((e = tin.getNextEntry()) != null) { File f = new File(parentDir, e.getName()); f.setLastModified(e.getLastModifiedDate().getTime()); f.getParentFile().mkdirs(); if (e.isDirectory()) { f.mkdir(); continue; } long size = e.getSize(); checkFileSize(size); try (OutputStream out = new FileOutputStream(f)) { /* TarInputStream pretends each entry's EOF is the stream's EOF */ IOUtils.copy(tin, out); } } }
Example 3
Source File: P4ExtFileUtils.java From p4ic4idea with Apache License 2.0 | 6 votes |
private static void extractArchive(ArchiveInputStream archiveInputStream, File outputDir) throws IOException { createDirectory(outputDir); try { ArchiveEntry entry = archiveInputStream.getNextEntry(); while (entry != null) { File node = new File(outputDir, entry.getName()); if (entry.isDirectory()) { if (!node.mkdirs() && !node.isDirectory()) { throw new IOException("Could not create directory " + node); } } else { extractFile(archiveInputStream, node); } entry = archiveInputStream.getNextEntry(); } } finally { archiveInputStream.close(); } }
Example 4
Source File: TarReader.java From metafacture-core with Apache License 2.0 | 6 votes |
@Override public void process(final Reader reader) { try ( InputStream stream = new ReaderInputStream(reader, Charset.defaultCharset()); ArchiveInputStream tarStream = new TarArchiveInputStream(stream); ) { ArchiveEntry entry; while ((entry = tarStream.getNextEntry()) != null) { if (!entry.isDirectory()) { processFileEntry(tarStream); } } } catch (IOException e) { throw new MetafactureException(e); } }
Example 5
Source File: RDescriptionUtils.java From nexus-repository-r with Eclipse Public License 1.0 | 6 votes |
private static Map<String, String> extractMetadataFromArchive(final String archiveType, final InputStream is) { final ArchiveStreamFactory archiveFactory = new ArchiveStreamFactory(); try (ArchiveInputStream ais = archiveFactory.createArchiveInputStream(archiveType, is)) { ArchiveEntry entry = ais.getNextEntry(); while (entry != null) { if (!entry.isDirectory() && DESCRIPTION_FILE_PATTERN.matcher(entry.getName()).matches()) { return parseDescriptionFile(ais); } entry = ais.getNextEntry(); } } catch (ArchiveException | IOException e) { throw new RException(null, e); } throw new IllegalStateException("No metadata file found"); }
Example 6
Source File: NpmPackageParser.java From nexus-public with Eclipse Public License 1.0 | 6 votes |
/** * Determines if the specified archive entry's name could represent a valid package.json file. Typically these would * be under {@code package/package.json}, but there are tarballs out there that have the package.json in a different * directory. */ @VisibleForTesting boolean isPackageJson(final ArchiveEntry entry) { if (entry.isDirectory()) { return false; } String name = entry.getName(); if (name == null) { return false; } else if (!name.endsWith(PACKAGE_JSON_SUBPATH)) { return false; // not a package.json file } else if (name.startsWith(PACKAGE_JSON_SUBPATH)) { return false; // should not be at the root, should be under the path containing the actual package, whatever it is } else { return name.indexOf(PACKAGE_JSON_SUBPATH) == name.indexOf(SEPARATOR); // path should only be one directory deep } }
Example 7
Source File: FuncotatorReferenceTestUtils.java From gatk with BSD 3-Clause "New" or "Revised" License | 5 votes |
/** * Extract a fasta tar.gz (should include only one fasta, the index, and a dict). Though this is not enforced. * * Much of this code taken from: http://commons.apache.org/proper/commons-compress/examples.html * * @param fastaTarGz Should include one fasta file, the associated dict file and the associated index, though this is not enforced. Never {@code null} * @param destDir Where to store te untarred files. Never {@code null} * @return a path (as string) to the fasta file. If multiple fasta files were in the tar gz, it will return that last one seen. */ private static String extractFastaTarGzToTemp(final File fastaTarGz, final Path destDir) { Utils.nonNull(fastaTarGz); Utils.nonNull(destDir); String result = null; try (final ArchiveInputStream i = new TarArchiveInputStream(new GzipCompressorInputStream(new FileInputStream(fastaTarGz)))) { ArchiveEntry entry = null; while ((entry = i.getNextEntry()) != null) { if (!i.canReadEntryData(entry)) { logger.warn("Could not read an entry in the archive: " + entry.getName() + " from " + fastaTarGz.getAbsolutePath()); } final String name = destDir + "/" + entry.getName(); if (entry.getName().endsWith(".fasta")) { result = name; } final File f = new File(name); if (entry.isDirectory()) { if (!f.isDirectory() && !f.mkdirs()) { throw new IOException("Failed to create directory " + f); } } else { File parent = f.getParentFile(); if (!parent.isDirectory() && !parent.mkdirs()) { throw new IOException("Failed to create directory " + parent); } try (OutputStream o = Files.newOutputStream(f.toPath())) { IOUtils.copy(i, o); } } } } catch (final IOException ioe) { throw new GATKException("Could not untar test reference: " + fastaTarGz, ioe); } return result; }
Example 8
Source File: ArchiveFileTree.java From gradle-plugins with MIT License | 5 votes |
@Override public void visit(FileVisitor visitor) { if (!archiveFile.exists()) { throw new InvalidUserDataException(String.format("Cannot expand %s as it does not exist.", getDisplayName())); } if (!archiveFile.isFile()) { throw new InvalidUserDataException(String.format("Cannot expand %s as it is not a file.", getDisplayName())); } AtomicBoolean stopFlag = new AtomicBoolean(); try { IS archiveInputStream = inputStreamProvider.openFile(archiveFile); try { ArchiveEntry archiveEntry; while (!stopFlag.get() && (archiveEntry = archiveInputStream.getNextEntry()) != null) { ArchiveEntryFileTreeElement details = createDetails(chmod); details.archiveInputStream = archiveInputStream; details.archiveEntry = (E) archiveEntry; details.stopFlag = stopFlag; try { if (archiveEntry.isDirectory()) { visitor.visitDir(details); } else { visitor.visitFile(details); } } finally { details.close(); } } } finally { archiveInputStream.close(); } } catch (Exception e) { throw new GradleException(String.format("Could not expand %s.", getDisplayName()), e); } }
Example 9
Source File: PyPiInfoUtils.java From nexus-public with Eclipse Public License 1.0 | 5 votes |
/** * Determines if an {@link ArchiveEntry} is a metadata file. */ private static boolean isMetadataFile(final ArchiveEntry entry) { checkNotNull(entry); if (entry.isDirectory()) { return false; } String[] parts = entry.getName().split("/"); return isEggInfoPath(parts) || isPackageInfoFilePath(parts) || isMetadataFilePath(parts); }
Example 10
Source File: CompressedApplicationInputStream.java From vespa with Apache License 2.0 | 5 votes |
private void decompressInto(File application) throws IOException { log.log(Level.FINE, "Application is in " + application.getAbsolutePath()); int entries = 0; ArchiveEntry entry; while ((entry = ais.getNextEntry()) != null) { log.log(Level.FINE, "Unpacking " + entry.getName()); File outFile = new File(application, entry.getName()); // FIXME/TODO: write more tests that break this logic. I have a feeling it is not very robust. if (entry.isDirectory()) { if (!(outFile.exists() && outFile.isDirectory())) { log.log(Level.FINE, "Creating dir: " + outFile.getAbsolutePath()); boolean res = outFile.mkdirs(); if (!res) { log.log(Level.WARNING, "Could not create dir " + entry.getName()); } } } else { log.log(Level.FINE, "Creating output file: " + outFile.getAbsolutePath()); // Create parent dir if necessary String parent = outFile.getParent(); new File(parent).mkdirs(); FileOutputStream fos = new FileOutputStream(outFile); ByteStreams.copy(ais, fos); fos.close(); } entries++; } if (entries == 0) { log.log(Level.WARNING, "Not able to read any entries from " + application.getName()); } }
Example 11
Source File: CompressedFileReference.java From vespa with Apache License 2.0 | 5 votes |
private static void decompress(ArchiveInputStream archiveInputStream, File outputFile) throws IOException { int entries = 0; ArchiveEntry entry; while ((entry = archiveInputStream.getNextEntry()) != null) { File outFile = new File(outputFile, entry.getName()); if (entry.isDirectory()) { if (!(outFile.exists() && outFile.isDirectory())) { log.log(Level.FINE, () -> "Creating dir: " + outFile.getAbsolutePath()); if (!outFile.mkdirs()) { log.log(Level.WARNING, "Could not create dir " + entry.getName()); } } } else { // Create parent dir if necessary File parent = new File(outFile.getParent()); if (!parent.exists() && !parent.mkdirs()) { log.log(Level.WARNING, "Could not create dir " + parent.getAbsolutePath()); } FileOutputStream fos = new FileOutputStream(outFile); ByteStreams.copy(archiveInputStream, fos); fos.close(); } entries++; } if (entries == 0) { throw new IllegalArgumentException("Not able to read any entries from stream (" + archiveInputStream.getBytesRead() + " bytes read from stream)"); } }
Example 12
Source File: ArchiveFileTree.java From gradle-plugins with MIT License | 5 votes |
@Override public void visit(FileVisitor visitor) { if (!archiveFile.exists()) { throw new InvalidUserDataException(String.format("Cannot expand %s as it does not exist.", getDisplayName())); } if (!archiveFile.isFile()) { throw new InvalidUserDataException(String.format("Cannot expand %s as it is not a file.", getDisplayName())); } AtomicBoolean stopFlag = new AtomicBoolean(); try { IS archiveInputStream = inputStreamProvider.openFile(archiveFile); try { ArchiveEntry archiveEntry; while (!stopFlag.get() && (archiveEntry = archiveInputStream.getNextEntry()) != null) { ArchiveEntryFileTreeElement details = createDetails(chmod); details.archiveInputStream = archiveInputStream; details.archiveEntry = (E) archiveEntry; details.stopFlag = stopFlag; try { if (archiveEntry.isDirectory()) { visitor.visitDir(details); } else { visitor.visitFile(details); } } finally { details.close(); } } } finally { archiveInputStream.close(); } } catch (Exception e) { throw new GradleException(String.format("Could not expand %s.", getDisplayName()), e); } }
Example 13
Source File: ArchiveExtractor.java From carbon-kernel with Apache License 2.0 | 5 votes |
/** * Extract the file and create directories accordingly. * * @param is pass the stream, because the stream is different for various file types * @param targetDir target directory * @throws IOException */ private static void extract(ArchiveInputStream is, File targetDir) throws IOException { try { if (targetDir.exists()) { FileUtils.forceDelete(targetDir); } createDirectory(targetDir); ArchiveEntry entry = is.getNextEntry(); while (entry != null) { String name = entry.getName(); name = name.substring(name.indexOf("/") + 1); File file = new File(targetDir, name); if (entry.isDirectory()) { createDirectory(file); } else { createDirectory(file.getParentFile()); OutputStream os = new FileOutputStream(file); try { IOUtils.copy(is, os); } finally { IOUtils.closeQuietly(os); } } entry = is.getNextEntry(); } } finally { try { is.close(); } catch (IOException e) { logger.warn("Error occurred while closing the stream", e); } } }
Example 14
Source File: ZipFileValidator.java From secure-data-service with Apache License 2.0 | 5 votes |
private static boolean isDirectory(ArchiveEntry zipEntry) { if (zipEntry.isDirectory()) { return true; } // UN: This check is to ensure that any zipping utility which does not pack a directory // entry // is verified by checking for a filename with '/'. Example: Windows Zipping Tool. if (zipEntry.getName().contains("/")) { return true; } return false; }
Example 15
Source File: UploadController.java From airsonic-advanced with GNU General Public License v3.0 | 5 votes |
private void copyEntry(Path file, ArchiveEntry entry, LambdaUtils.ThrowingConsumer<Path, IOException> copier, List<Path> unzippedFiles, List<Exception> exceptions) { final Path toPath = file.resolveSibling(entry.getName()); try { if (!toPath.normalize().startsWith(file.getParent())) { throw new IOException("Bad zip filename: " + toPath.toString()); } if (entry.isDirectory()) { Files.createDirectories(toPath); } else { Path parent = toPath.getParent(); Files.createDirectories(parent); if (!Files.isDirectory(parent)) { throw new IOException("Failed to create directory: " + parent); } securityService.checkUploadAllowed(toPath, true); copier.accept(toPath); unzippedFiles.add(toPath); LOG.debug("Unzipped {}", toPath); } } catch (IOException e) { exceptions.add(e); LOG.debug("Could not unzip {}", toPath, e); } LOG.info("Processed {}", toPath); }
Example 16
Source File: CompressionDataParser.java From datacollector with Apache License 2.0 | 4 votes |
private boolean isEligibleEntry(ArchiveEntry currentEntry) { return !currentEntry.isDirectory() && pathMatcher.matches(Paths.get(currentEntry.getName())); }
Example 17
Source File: UnZip.java From DataHubSystem with GNU Affero General Public License v3.0 | 4 votes |
public static void unCompress (String zip_file, String output_folder) throws IOException, CompressorException, ArchiveException { ArchiveInputStream ais = null; ArchiveStreamFactory asf = new ArchiveStreamFactory (); FileInputStream fis = new FileInputStream (new File (zip_file)); if (zip_file.toLowerCase ().endsWith (".tar")) { ais = asf.createArchiveInputStream ( ArchiveStreamFactory.TAR, fis); } else if (zip_file.toLowerCase ().endsWith (".zip")) { ais = asf.createArchiveInputStream ( ArchiveStreamFactory.ZIP, fis); } else if (zip_file.toLowerCase ().endsWith (".tgz") || zip_file.toLowerCase ().endsWith (".tar.gz")) { CompressorInputStream cis = new CompressorStreamFactory (). createCompressorInputStream (CompressorStreamFactory.GZIP, fis); ais = asf.createArchiveInputStream (new BufferedInputStream (cis)); } else { try { fis.close (); } catch (IOException e) { LOGGER.warn ("Cannot close FileInputStream:", e); } throw new IllegalArgumentException ( "Format not supported: " + zip_file); } File output_file = new File (output_folder); if (!output_file.exists ()) output_file.mkdirs (); // copy the existing entries ArchiveEntry nextEntry; while ((nextEntry = ais.getNextEntry ()) != null) { File ftemp = new File (output_folder, nextEntry.getName ()); if (nextEntry.isDirectory ()) { ftemp.mkdir (); } else { FileOutputStream fos = FileUtils.openOutputStream (ftemp); IOUtils.copy (ais, fos); fos.close (); } } ais.close (); fis.close (); }
Example 18
Source File: FileUnarchiveController.java From MyBox with Apache License 2.0 | 4 votes |
protected void unarchive() { try { if (selected == null || selected.isEmpty()) { return; } ArchiveStreamFactory aFactory = new ArchiveStreamFactory(); if (archiver == null || !aFactory.getInputStreamArchiveNames().contains(archiver)) { return; } try ( BufferedInputStream fileIn = new BufferedInputStream(new FileInputStream(sourceFile)); ArchiveInputStream in = aFactory.createArchiveInputStream(archiver, fileIn, encodeSelector.getValue())) { ArchiveEntry entry; while ((entry = in.getNextEntry()) != null) { if (!in.canReadEntryData(entry)) { archiveFail++; // logger.debug(message("CanNotReadEntryData" + ":" + entry.getName())); continue; } if (entry.isDirectory() || !selected.contains(entry.getName())) { continue; } File file = makeTargetFile(entry.getName(), targetPath); if (file == null) { continue; } File parent = file.getParentFile(); if (!parent.isDirectory() && !parent.mkdirs()) { archiveFail++; // logger.debug(message("FailOpenFile" + ":" + file)); continue; } try ( OutputStream o = Files.newOutputStream(file.toPath())) { IOUtils.copy(in, o); } archiveSuccess++; } } } catch (Exception e) { archiveFail++; error = e.toString(); if (error.contains("java.nio.charset.MalformedInputException") || error.contains("Illegal char")) { charsetIncorrect = true; } } }
Example 19
Source File: FilesArchiveCompressController.java From MyBox with Apache License 2.0 | 4 votes |
@Override public void donePost() { tableView.refresh(); if (miaoCheck.isSelected()) { FxmlControl.miao3(); } if (openCheck.isSelected()) { File path = targetFile.getParentFile(); browseURI(path.toURI()); recordFileOpened(path); } if (archive == null) { return; } StringBuilder s = new StringBuilder(); s.append("<h1 class=\"center\">").append(targetFile).append("</h1>\n"); s.append("<hr>\n"); int ratio; if (totalSize > 0) { ratio = (int) (100 - targetFile.length() * 100 / totalSize); } else { ratio = 0; } String compressInfo = message("TotalSize") + ":" + FileTools.showFileSize(totalSize) + " " + message("SizeAfterArchivedCompressed") + ":" + FileTools.showFileSize(targetFile.length()) + " " + message("CompressedRatio") + ":" + ratio + "%"; s.append("<P>").append(compressInfo).append("</P>\n"); List<String> names = new ArrayList<>(); names.addAll(Arrays.asList(message("ID"), message("Directory"), message("File"), message("Size"), message("ModifiedTime") )); StringTable table = new StringTable(names, message("ArchiveContents")); int id = 1; String dir, file, size; for (ArchiveEntry entry : archive) { List<String> row = new ArrayList<>(); if (entry.isDirectory()) { dir = entry.getName(); file = ""; } else { int pos = entry.getName().lastIndexOf('/'); if (pos < 0) { dir = ""; file = entry.getName(); } else { dir = entry.getName().substring(0, pos); file = entry.getName().substring(pos + 1, entry.getName().length()); } } if (entry.getSize() > 0) { size = FileTools.showFileSize(entry.getSize()); } else { size = ""; } row.addAll(Arrays.asList((id++) + "", dir, file, size, DateTools.datetimeToString(entry.getLastModifiedDate()) )); table.add(row); } s.append(StringTable.tableDiv(table)); HtmlTools.editHtml(message("ArchiveContents"), s.toString()); }
Example 20
Source File: TarDirectoryStream.java From CloverETL-Engine with GNU Lesser General Public License v2.1 | 4 votes |
@Override protected boolean isDirectory(ArchiveEntry entry) { return entry.isDirectory(); }