Example usage for java.nio.file Files walkFileTree

List of usage examples for java.nio.file Files walkFileTree

Introduction

In this page you can find the example usage for java.nio.file Files walkFileTree.

Prototype

public static Path walkFileTree(Path start, FileVisitor<? super Path> visitor) throws IOException 

Source Link

Document

Walks a file tree.

Usage

From source file:company.gonapps.loghut.utils.FileUtils.java

public List<String> scan(String pathString) throws IOException {
    paths = new LinkedList<>();
    Files.walkFileTree(Paths.get(pathString), this);
    List<String> pathStrings = new LinkedList<>();
    for (Path path : paths) {
        pathStrings.add(path.toString());
    }/*from w  w w  .  j  a v a  2s. com*/
    return pathStrings;
}

From source file:com.ibm.streamsx.topology.internal.context.remote.ZippedToolkitRemoteContext.java

private static void addAllToZippedArchive(Map<Path, String> starts, Path zipFilePath) throws IOException {
    try (ZipArchiveOutputStream zos = new ZipArchiveOutputStream(zipFilePath.toFile())) {
        for (Path start : starts.keySet()) {
            final String rootEntryName = starts.get(start);
            Files.walkFileTree(start, new SimpleFileVisitor<Path>() {
                public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                    // Skip pyc files.
                    if (file.getFileName().toString().endsWith(".pyc"))
                        return FileVisitResult.CONTINUE;

                    String entryName = rootEntryName;
                    String relativePath = start.relativize(file).toString();
                    // If empty, file is the start file.
                    if (!relativePath.isEmpty()) {
                        entryName = entryName + "/" + relativePath;
                    }/*  w ww.ja va2  s.  c om*/
                    // Zip uses forward slashes
                    entryName = entryName.replace(File.separatorChar, '/');

                    ZipArchiveEntry entry = new ZipArchiveEntry(file.toFile(), entryName);
                    if (Files.isExecutable(file))
                        entry.setUnixMode(0100770);
                    else
                        entry.setUnixMode(0100660);

                    zos.putArchiveEntry(entry);
                    Files.copy(file, zos);
                    zos.closeArchiveEntry();
                    return FileVisitResult.CONTINUE;
                }

                public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs)
                        throws IOException {
                    final String dirName = dir.getFileName().toString();
                    // Don't include pyc files or .toolkit 
                    if (dirName.equals("__pycache__"))
                        return FileVisitResult.SKIP_SUBTREE;

                    ZipArchiveEntry dirEntry = new ZipArchiveEntry(dir.toFile(), rootEntryName + "/"
                            + start.relativize(dir).toString().replace(File.separatorChar, '/') + "/");
                    zos.putArchiveEntry(dirEntry);
                    zos.closeArchiveEntry();
                    return FileVisitResult.CONTINUE;
                }
            });
        }
    }
}

From source file:software.coolstuff.springframework.owncloud.service.impl.local.OwncloudLocalResourceChecksumServiceImpl.java

@PostConstruct
public void afterPropertiesSet() throws Exception {
    ResourceServiceProperties resourceProperties = properties.getResourceService();
    OwncloudLocalUtils.checkPrivilegesOnDirectory(resourceProperties.getLocation());
    setMessageDigest(resourceProperties.getMessageDigestAlgorithm());
    log.debug("Calculate the Checksum of all Files and Directories of Directory {}",
            resourceProperties.getLocation());
    Files.walkFileTree(resourceProperties.getLocation(), getFileVisitor());
}

From source file:com.sastix.cms.server.services.content.impl.ZipHandlerServiceImpl.java

@Override
public ResourceDTO handleZip(Resource zipResource) {

    final Path zipPath;
    try {//w w w.  ja va  2  s .  c o m
        zipPath = hashedDirectoryService.getDataByURI(zipResource.getUri(), zipResource.getResourceTenantId());
    } catch (IOException | URISyntaxException e) {
        throw new ResourceAccessError(e.getMessage());
    }

    FileSystem zipfs = null;
    try {
        zipfs = FileSystems.newFileSystem(zipPath, null);
        final Path root = zipfs.getPath("/");
        final int maxDepth = 1;

        // Search for specific files.
        final Map<String, Path> map = Files
                .find(root, maxDepth, (path_,
                        attr) -> path_.getFileName() != null && (path_.toString().endsWith(METADATA_JSON_FILE)
                                || path_.toString().endsWith(METADATA_XML_FILE)))
                .collect(Collectors.toMap(p -> p.toAbsolutePath().toString().substring(1), p -> p));

        final String zipType;
        final String startPage;

        // Check if it is a cms file
        if (map.containsKey(METADATA_JSON_FILE)) {
            zipType = METADATA_JSON_FILE;
            LOG.info("Found CMS Metadata File " + map.get(zipType).toString());
            startPage = findStartPage(map.get(zipType));
            // Check if it is a Scrom file
        } else if (map.containsKey(METADATA_XML_FILE)) {
            zipType = METADATA_XML_FILE;
            LOG.info("Found CMS Metadata File " + map.get(zipType).toString());
            startPage = findScormStartPage(map.get(zipType));

        } else {
            throw new ResourceAccessError("Zip " + zipResource.getName() + " is not supported. "
                    + METADATA_JSON_FILE + " and " + METADATA_XML_FILE + " are missing");
        }

        LOG.trace(startPage);

        final List<ResourceDTO> resourceDTOs = new LinkedList<>();

        /* Path inside ZIP File */
        Files.walkFileTree(root, new SimpleFileVisitor<Path>() {

            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {

                final String parentContext = zipResource.getUri().split("-")[0] + "-"
                        + zipResource.getResourceTenantId();
                final CreateResourceDTO createResourceDTO = new CreateResourceDTO();
                createResourceDTO.setResourceMediaType(tika.detect(file.toString()));
                createResourceDTO.setResourceAuthor(zipResource.getAuthor());
                createResourceDTO.setResourceExternalURI(file.toUri().toString());
                createResourceDTO.setResourceName(file.toString().substring(1));
                createResourceDTO.setResourceTenantId(zipResource.getResourceTenantId());

                final Resource resource = resourceService.insertChildResource(createResourceDTO, parentContext,
                        zipResource);

                distributedCacheService.cacheIt(resource.getUri(), resource.getResourceTenantId());

                if (file.toString().substring(1).equals(startPage)) {
                    resourceDTOs.add(0, crs.convertToDTO(resource));
                } else {
                    resourceDTOs.add(crs.convertToDTO(resource));
                }

                return FileVisitResult.CONTINUE;
            }

            @Override
            public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
                return FileVisitResult.CONTINUE;
            }
        });

        final ResourceDTO parentResourceDto = resourceDTOs.remove(0);
        parentResourceDto.setResourcesList(resourceDTOs);
        return parentResourceDto;

    } catch (IOException e) {
        throw new ResourceAccessError("Error while analyzing " + zipResource.toString());
    } finally {
        if (zipfs != null && zipfs.isOpen()) {
            try {
                LOG.info("Closing FileSystem");
                zipfs.close();
            } catch (IOException e) {
                LOG.error(e.getMessage());
                e.printStackTrace();
                throw new ResourceAccessError("Error while analyzing " + zipResource.toString());
            }
        }
    }
}

From source file:cz.etnetera.reesmo.writer.storage.RestApiStorage.java

protected void addResultAttachment(final Result result, Object attachment) throws StorageException {
    File file = null;// w w  w . j  a v  a2s.c  o m
    String path = null;
    String contentType = null;
    if (attachment instanceof File) {
        file = (File) attachment;
    } else if (attachment instanceof ExtendedFile) {
        ExtendedFile fileWithPath = (ExtendedFile) attachment;
        file = fileWithPath.getFile();
        path = fileWithPath.getPath();
        contentType = fileWithPath.getContentType();
    } else {
        throw new StorageException("Unsupported attachment type " + attachment.getClass());
    }

    if (path != null) {
        path = path.replaceAll("^/+", "").replaceAll("/+$", "");
    }

    if (file.isDirectory()) {
        Path root = file.toPath();
        final String rootPath = path == null ? file.getName() : path;
        try {
            Files.walkFileTree(root, new SimpleFileVisitor<Path>() {
                @Override
                public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                    FileVisitResult res = super.visitFile(file, attrs);
                    String relativePath = rootPath + "/" + root.relativize(file).normalize().toString();
                    try {
                        addResultAttachment(result, ExtendedFile.withPath(file.toFile(), relativePath));
                    } catch (StorageException e) {
                        throw new IOException(e);
                    }
                    return res;
                }
            });
        } catch (IOException e) {
            throw new StorageException(e);
        }
        // directory is not stored, just paths
        return;
    }

    MultipartBody body = Unirest
            .post(getUrl(METHOD_RESULT_ATTACHMENT_CREATE).replace("{resultId}", result.getId()))
            .basicAuth(username, password).header("Accept", "application/json").field("file", file);
    if (path != null) {
        body.field("path", path);
    }
    if (contentType != null) {
        body.field("contentType", contentType);
    }

    HttpResponse<String> response;
    try {
        response = body.asString();
    } catch (UnirestException e) {
        throw new StorageException("Unable to store result attachment", e);
    }

    if (response.getStatus() != 200) {
        throw new StorageException("Wrong status code when storing result attachment " + response.getStatus());
    }

    ResultAttachment resultAttachment = null;
    try {
        resultAttachment = new ObjectMapper().readValue(response.getBody(), ResultAttachment.class);
    } catch (UnsupportedOperationException | IOException e) {
        throw new StorageException("Unable to parse result attachment from response", e);
    }

    getLogger().info("Result attachment stored " + resultAttachment.getPath() + " " + resultAttachment.getId());
}

From source file:maspack.fileutil.SafeFileUtils.java

public static void copyDirectory(File srcDir, File destDir, int options) throws IOException {
    Files.walkFileTree(srcDir.toPath(), new CopyFileVisitor(destDir.toPath()));
}

From source file:edu.cwru.jpdg.Javac.java

static List<File> find_classes(String path) {
    File dir = new File(path);
    FindClasses fc = new FindClasses();
    try {//from   w  ww.ja va  2 s  .  c om
        Files.walkFileTree(Paths.get(path), fc);
    } catch (IOException e) {
        throw new RuntimeException("Couldn't walk dir tree.");
    }
    return fc.files;
}

From source file:io.syndesis.project.converter.DefaultProjectGeneratorTest.java

@After
public void tearDown() throws Exception {
    if (runtimeDir != null) {
        Files.walkFileTree(runtimeDir, new SimpleFileVisitor<Path>() {
            @Override//w  w w .  ja  v a2s.c  o  m
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                Files.delete(file);
                return FileVisitResult.CONTINUE;
            }

            @Override
            public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
                Files.delete(dir);
                return FileVisitResult.CONTINUE;
            }
        });
    }
}

From source file:com.expedia.tesla.compiler.Util.java

/**
 * Expand glob file patterns to path strings. Any path element that is not a glob pattern will be keep as it is.
 * //from   ww  w .  ja  v  a  2s  .c om
 * @param pathOrPatterns
 *       glob patterns.
 * 
 * @return
 *       The expanded paths.
 * 
 * @throws IOException
 *       On IO errors.
 */
public static Collection<String> expandWildcard(Collection<String> pathOrPatterns) throws IOException {
    final List<String> files = new ArrayList<String>();
    final List<PathMatcher> matchers = new ArrayList<PathMatcher>();
    for (String pattern : pathOrPatterns) {
        if (pattern.contains("*") || pattern.contains("?")) {
            PathMatcher matcher = FileSystems.getDefault().getPathMatcher("glob:" + pattern);
            matchers.add(matcher);
        } else {
            files.add(pattern);
        }
    }

    if (!matchers.isEmpty()) {
        Files.walkFileTree(new File(System.getProperty("user.dir")).toPath(), new SimpleFileVisitor<Path>() {
            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attr) {
                for (PathMatcher matcher : matchers) {
                    if (matcher.matches(file)) {
                        files.add(file.toString());
                    }
                }
                return FileVisitResult.CONTINUE;
            }
        });
    }

    return files;
}

From source file:de.bbe_consulting.mavento.MagentoInfoMojo.java

@Override
public void execute() throws MojoExecutionException, MojoFailureException {

    initMojo();//from ww  w  .j a v a2  s  .  co  m
    getLog().info("Scanning: " + magentoPath);
    getLog().info("");
    if (mVersion != null) {
        getLog().info("Version: Magento " + mVersion.toString());
    }

    // parse sql properties from local.xml
    final Path localXmlPath = Paths.get(magentoPath + "/app/etc/local.xml");
    Document localXml = null;
    if (Files.exists(localXmlPath)) {
        localXml = MagentoXmlUtil.readXmlFile(localXmlPath.toAbsolutePath().toString());
    } else {
        throw new MojoExecutionException(
                "Could not read or parse /app/etc/local.xml." + " Use -DmagentoPath= to set Magento dir.");
    }
    final Map<String, String> dbSettings = MagentoXmlUtil.getDbValues(localXml);
    final String jdbcUrl = MagentoSqlUtil.getJdbcUrl(dbSettings.get("host"), dbSettings.get("port"),
            dbSettings.get("dbname"));

    // fetch installdate
    final String magentoInstallDate = MagentoXmlUtil.getMagentoInstallData(localXml);
    getLog().info("Installed: " + magentoInstallDate);
    getLog().info("");

    // read baseUrl
    MagentoCoreConfig baseUrl = null;
    try {
        baseUrl = new MagentoCoreConfig("web/unsecure/base_url");
    } catch (Exception e) {
        throw new MojoExecutionException("Error creating config entry. " + e.getMessage(), e);
    }

    String sqlError = SQL_CONNECTION_VALID;
    try {
        baseUrl = MagentoSqlUtil.getCoreConfigData(baseUrl, dbSettings.get("user"), dbSettings.get("password"),
                jdbcUrl, getLog());
        getLog().info("URL: " + baseUrl.getValue());
        getLog().info("");
    } catch (MojoExecutionException e) {
        sqlError = e.getMessage();
    }

    getLog().info("Database: " + dbSettings.get("dbname") + " via " + dbSettings.get("user") + "@"
            + dbSettings.get("host") + ":" + dbSettings.get("port"));
    getLog().info("Connection: " + sqlError);
    getLog().info("");

    if (!skipSize) {
        MutableLong rootSizeTotal = new MutableLong();
        try {
            FileSizeVisitor fs = new FileSizeVisitor(rootSizeTotal);
            Files.walkFileTree(Paths.get(magentoPath), fs);
        } catch (IOException e) {
            throw new MojoExecutionException(e.getMessage(), e);
        }
        getLog().info(
                "Magento files total: " + String.format("%,8d", rootSizeTotal.toLong()).trim() + " bytes");
        if (SQL_CONNECTION_VALID.equals(sqlError)) {
            try {
                final Map<String, Integer> dbDetails = MagentoSqlUtil.getDbSize(dbSettings.get("dbname"),
                        dbSettings.get("user"), dbSettings.get("password"), jdbcUrl, getLog());
                final List<MysqlTable> logTableDetails = MagentoSqlUtil.getLogTablesSize(
                        dbSettings.get("dbname"), dbSettings.get("user"), dbSettings.get("password"), jdbcUrl,
                        getLog());
                getLog().info("Database total: " + String.format("%,8d", dbDetails.get("totalRows")).trim()
                        + " entries / " + String.format("%,8d", dbDetails.get("totalSize")).trim() + "mb");
                int logSizeTotal = 0;
                int logRowsTotal = 0;
                for (MysqlTable t : logTableDetails) {
                    logSizeTotal += t.getTableSizeInMb();
                    logRowsTotal += t.getTableRows();
                    if (showDetails) {
                        getLog().info(" " + t.getTableName() + ": " + t.getFormatedTableEntries()
                                + " entries / " + t.getFormatedTableSizeInMb() + "mb");
                    }
                }
                getLog().info("Log tables total: " + String.format("%,8d", logRowsTotal).trim() + " entries / "
                        + String.format("%,8d", logSizeTotal).trim() + "mb");
            } catch (MojoExecutionException e) {
                getLog().info("Error: " + e.getMessage());
            }
        }
        getLog().info("");
    }
    // parse modules
    final Path modulesXmlPath = Paths.get(magentoPath + "/app/etc/modules");
    if (!Files.exists(modulesXmlPath)) {
        throw new MojoExecutionException("Could not find /app/etc/modules directory.");
    }

    DirectoryStream<Path> files = null;
    final ArrayList<MagentoModule> localModules = new ArrayList<MagentoModule>();
    final ArrayList<MagentoModule> communityModules = new ArrayList<MagentoModule>();
    try {
        files = Files.newDirectoryStream(modulesXmlPath);
        for (Path path : files) {
            if (!path.getFileName().toString().startsWith("Mage")) {
                MagentoModule m = new MagentoModule(path);
                if (m.getCodePool().equals("local")) {
                    localModules.add(m);
                } else {
                    communityModules.add(m);
                }
            }
        }
    } catch (IOException e) {
        throw new MojoExecutionException("Could not read modules directory. " + e.getMessage(), e);
    } finally {
        try {
            files.close();
        } catch (IOException e) {
            throw new MojoExecutionException("Error closing directory stream. " + e.getMessage(), e);
        }
    }

    // print module sorted module list
    final MagentoModuleComperator mmc = new MagentoModuleComperator();
    Collections.sort(localModules, mmc);
    Collections.sort(communityModules, mmc);

    getLog().info("Installed modules in..");

    getLog().info("..local: ");
    for (MagentoModule m : localModules) {
        getLog().info(m.getNamespace() + "_" + m.getName() + " version: " + m.getVersion() + " active: "
                + m.isActive());
    }
    if (localModules.size() == 0) {
        getLog().info("--none--");
    }
    getLog().info("");

    getLog().info("..community: ");
    for (MagentoModule m : communityModules) {
        getLog().info(m.getNamespace() + "_" + m.getName() + " version: " + m.getVersion() + " active: "
                + m.isActive());
    }
    if (communityModules.size() == 0) {
        getLog().info("--none--");
    }
    getLog().info("");

    // check local overlays for content
    getLog().info("Overlay status..");
    int fileCount = -1;
    final File localMage = new File(magentoPath + "/app/code/local/Mage");
    if (localMage.exists()) {
        fileCount = localMage.list().length;
        if (fileCount > 0) {
            getLog().info("local/Mage: " + localMage.list().length + " file(s)");
        }
    }
    final File localVarien = new File(magentoPath + "/app/code/local/Varien");

    if (localVarien.exists()) {
        fileCount = localVarien.list().length;
        if (fileCount > 0) {
            getLog().info("local/Varien: " + localVarien.list().length + " file(s)");
        }
    }

    if (fileCount == -1) {
        getLog().info("..not in use.");
    }

}