Java Path Delete nio deleteEmptyParentDirs(Path pkgDirPath, Path repoPath)

Here you can find the source of deleteEmptyParentDirs(Path pkgDirPath, Path repoPath)

Description

Delete empty parent directories.

License

Open Source License

Parameter

Parameter Description
pkgDirPath package directory path
repoPath home repository path

Exception

Parameter Description
IOException throw an exception if an error occurs

Declaration

private static void deleteEmptyParentDirs(Path pkgDirPath, Path repoPath)
        throws IOException 

Method Source Code

//package com.java2s;
/*//from  ww w  . j  a va2  s.  c o m
 *  Copyright (c) 2018, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
 *
 *  WSO2 Inc. licenses this file to you under the Apache License,
 *  Version 2.0 (the "License"); you may not use this file except
 *  in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing,
 *  software distributed under the License is distributed on an
 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *  KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations
 *  under the License.
 */

import java.io.IOException;

import java.nio.file.Files;

import java.nio.file.Path;

public class Main {
    /**
     * Delete empty parent directories.
     * After deleting the package.zip from the home repository we should delete the empty directories as well. We need
     * to delete all folders from the package path to the home directory only if they are empty (going backwards).
     * If the directory path is empty i.e. does not contain any files we simply delete the folder else we don't delete.
     *
     * @param pkgDirPath package directory path
     * @param repoPath   home repository path
     * @throws IOException throw an exception if an error occurs
     */
    private static void deleteEmptyParentDirs(Path pkgDirPath, Path repoPath)
            throws IOException {
        Path pathsInBetween = repoPath.relativize(pkgDirPath);
        for (int i = pathsInBetween.getNameCount(); i > 0; i--) {
            Path toRemove = repoPath.resolve(pathsInBetween.subpath(0, i));
            if (!Files.list(toRemove).findAny().isPresent()) {
                Files.delete(toRemove);
            }
        }
    }
}

Related

  1. delete(String filePath, String fileName)
  2. delete(String targetFilePath)
  3. deleteAllFilesRecursively(Path path)
  4. deleteContent(Path directory)
  5. deleteEmptyDirsUpTo(Path from, Path to)
  6. deleteFile(Path p)
  7. deleteFile(Path path)
  8. deleteFile(Path path)
  9. deleteFile(Path path)