    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one or more
    003 * contributor license agreements.  See the NOTICE file distributed with
    004 * this work for additional information regarding copyright ownership.
    005 * The ASF licenses this file to You under the Apache License, Version 2.0
    006 * (the "License"); you may not use this file except in compliance with
    007 * the License.  You may obtain a copy of the License at
    008 *
    009 *   http://www.apache.org/licenses/LICENSE-2.0
    010 *
    011 * Unless required by applicable law or agreed to in writing, software
    012 * distributed under the License is distributed on an "AS IS" BASIS,
    013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014 * See the License for the specific language governing permissions and
    015 * limitations under the License.
    016 */
    017
    018package org.apache.commons.compress.utils;
    019
    020import java.io.File;
    021import java.nio.file.Path;
    022
    023/**
    024 * Generic file name utilities.
    025 * @since 1.20
    026 */
    027public class FileNameUtils {
    028
    029    private static String fileNameToBaseName(final String name) {
    030        final int extensionIndex = name.lastIndexOf('.');
    031        return extensionIndex < 0 ? name : name.substring(0, extensionIndex);
    032    }
    033
    034    private static String fileNameToExtension(final String name) {
    035        final int extensionIndex = name.lastIndexOf('.');
    036        return extensionIndex < 0 ? "" : name.substring(extensionIndex + 1);
    037    }
    038
    039    /**
    040     * Gets the base name (i.e. the part up to and not including the
    041     * last ".") of the last path segment of a file name.
    042     * <p>Will return the file name itself if it doesn't contain any
    043     * dots. All leading directories of the {@code file name} parameter
    044     * are skipped.</p>
    045     * @return the base name of file name
    046     * @param path the path of the file to obtain the base name of.
    047     * @since 1.22
    048     */
    049    public static String getBaseName(final Path path) {
    050        if (path == null) {
    051            return null;
    052        }
    053        final Path fileName = path.getFileName();
    054        return fileName != null ? fileNameToBaseName(fileName.toString()) : null;
    055    }
    056
    057    /**
    058     * Gets the base name (i.e. the part up to and not including the
    059     * last ".") of the last path segment of a file name.
    060     *
    061     * <p>Will return the file name itself if it doesn't contain any
    062     * dots. All leading directories of the {@code file name} parameter
    063     * are skipped.</p>
    064     *
    065     * @return the base name of file name
    066     * @param fileName the name of the file to obtain the base name of.
    067     */
    068    public static String getBaseName(final String fileName) {
    069        if (fileName == null) {
    070            return null;
    071        }
    072        return fileNameToBaseName(new File(fileName).getName());
    073    }
    074
    075    /**
    076     * Gets the extension (i.e. the part after the last ".") of a file.
    077     * <p>Will return an empty string if the file name doesn't contain
    078     * any dots. Only the last segment of a the file name is consulted
    079     * - i.e. all leading directories of the {@code file name}
    080     * parameter are skipped.</p>
    081     * @return the extension of file name
    082     * @param path the path of the file to obtain the extension of.
    083     * @since 1.22
    084     */
    085    public static String getExtension(final Path path) {
    086        if (path == null) {
    087            return null;
    088        }
    089        final Path fileName = path.getFileName();
    090        return fileName != null ? fileNameToExtension(fileName.toString()) : null;
    091    }
    092
    093    /**
    094     * Gets the extension (i.e. the part after the last ".") of a file.
    095     *
    096     * <p>Will return an empty string if the file name doesn't contain
    097     * any dots. Only the last segment of a the file name is consulted
    098     * - i.e. all leading directories of the {@code fileName}
    099     * parameter are skipped.</p>
    100     *
    101     * @return the extension of file name
    102     * @param fileName the name of the file to obtain the extension of.
    103     */
    104    public static String getExtension(final String fileName) {
    105        if (fileName == null) {
    106            return null;
    107        }
    108        return fileNameToExtension(new File(fileName).getName());
    109    }
    110}