Here you can find the source of deleteEmptyParentDirs(Path pkgDirPath, Path repoPath)
Parameter | Description |
---|---|
pkgDirPath | package directory path |
repoPath | home repository path |
Parameter | Description |
---|---|
IOException | throw an exception if an error occurs |
private static void deleteEmptyParentDirs(Path pkgDirPath, Path repoPath) throws IOException
//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); } } } }